| 1 |
<?php |
| 2 |
|
| 3 |
namespace Shellbox\RPC; |
| 4 |
|
| 5 |
use Shellbox\ShellboxError; |
| 6 |
use Throwable; |
| 7 |
|
| 8 |
/** |
| 9 |
* An implementation of RPCClient that executes the provided commands locally. |
| 10 |
*/ |
| 11 |
class LocalRpcClient implements RpcClient { |
| 12 |
|
| 13 |
public function call( |
| 14 |
string $routeName, |
| 15 |
$functionName, |
| 16 |
array $params = [], |
| 17 |
array $options = [] |
| 18 |
) { |
| 19 |
$binary = !empty( $options['binary'] ); |
| 20 |
|
| 21 |
if ( $binary ) { |
| 22 |
$params = array_map( static function ( $param ) { |
| 23 |
return (string)$param; |
| 24 |
}, $params ); |
| 25 |
} |
| 26 |
foreach ( $options['sources' ] ?? [] as $source ) { |
| 27 |
require_once $source; |
| 28 |
} |
| 29 |
|
| 30 |
// We don't need to require $options['classes'] sources - local client |
| 31 |
// is run in the same environment as the calling code, so classes |
| 32 |
// be autoloaded. |
| 33 |
|
| 34 |
try { |
| 35 |
$result = call_user_func_array( $functionName, $params ); |
| 36 |
} catch ( Throwable $e ) { |
| 37 |
throw new ShellboxError( $e->getMessage(), $e->getCode(), $e ); |
| 38 |
} |
| 39 |
|
| 40 |
if ( $binary ) { |
| 41 |
$result = (string)$result; |
| 42 |
} |
| 43 |
|
| 44 |
return $result; |
| 45 |
} |
| 46 |
|
| 47 |
} |