/**
  * Create JSON executor
  *
  * @param string $executablePath Executable path (eg "php", "node")
  * @param string $resultType Result type will be passed to "deserialize" as type (eg. "array<string>")
  * @param array $commandPrefixes Fixed command arguments (eg ["--verbose", "--no-interactive"])
  * @param array $environmentVariables Environment variables (eg ["TZ" => "UTC"])
  * @return JsonCommandExecutor
  */
 public function createJsonExecutor($executablePath, $resultType, array $commandPrefixes = [], array $environmentVariables = [])
 {
     $command = new JsonCommandExecutor($executablePath, $this->serializer, $resultType);
     foreach ($commandPrefixes as $prefix) {
         $command->addCommandPrefix($prefix);
     }
     foreach ($environmentVariables as $name => $value) {
         $command->addEnvironmentVariable($name, $value);
     }
     return $command;
 }
Esempio n. 2
0
class Result
{
    private $key;
    private $result;
    public function getKey()
    {
        return $this->key;
    }
    public function setKey($key)
    {
        $this->key = $key;
    }
    public function getResult()
    {
        return $this->result;
    }
    public function setResult($result)
    {
        $this->result = $result;
    }
}
$serializer = SerializerBuilder::create()->addDefaultHandlers()->addDefaultSerializationVisitors()->addDefaultDeserializationVisitors()->setDebug(true)->addMetadataDir(__DIR__ . '/serializer', 'Xiag\\JsonCommand\\Examples\\NodeJs')->setCacheDir(sys_get_temp_dir())->build();
$executor = new JsonCommandExecutor('node', $serializer, 'array<Xiag\\JsonCommand\\Examples\\NodeJs\\Result>');
$executor->addCommandPrefix(__DIR__ . '/calculator.js');
$executor->addEnvironmentVariable('TZ', 'UTC');
/** @var Result[] $results */
$results = $executor->executeCommand([new Argument('abc', 1000), new Argument('def', 2000)]);
foreach ($results as $result) {
    printf('Key:    %s' . PHP_EOL, $result->getKey());
    printf('Result: %f' . PHP_EOL, $result->getResult());
}