run cmd.

@param DriverInterface $driver
@param string          $cmd
@param array           $args
@param Response        $response

@return Response
424363
private function runCmd(DriverInterface $driver, $cmd, array $args, Response $response)
    {
        $data = null;
        try {
            if ($driver->isAllowedCommand($cmd)) {
                $data = call_user_func_array(
                    array($driver, $cmd),
                    $this->getArgs($args, $cmd, $response, $driver->getDriverId())
                );
            } else {
                $this->error(sprintf('command "%s" not allowed', $cmd));
            }
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }

        return $data instanceof HttpResponse ? $data : $response;
    }
1343464
public function execute($cmd, $data)
    {
        // Use pipes for STDIN, STDOUT, and STDERR
        $descr = array(
          0 => array(
            'pipe',
            'r'
          ) ,
          1 => array(
            'pipe',
            'w'
          ) ,
          2 => array(
            'pipe',
            'w'
          )
        );
        $pipes = [];

        // Start process, telling it to use STDIN for input and STDOUT for output.
        $cmd = escapeshellcmd($cmd);
        $process = proc_open($cmd, $descr, $pipes);

        // Get the data into pipe only if data is resource
        if (gettype($data) == "resource") {
            // Stream input to STDIN
            while (!feof($data)) {
                fwrite($pipes[0], fread($data, 1024));
            }

            // Close STDIN and the source data.
            fclose($pipes[0]);
            fclose($data);
        }

        // Wait for process to finish while reading STDOUT to a temp stream.
        // Otherwise the process can block indefinitely if STODUT gets bigger
        // than 4kb.
        $this->output = fopen("php://temp", 'w+');
        $exit_code = null;
        while ($exit_code === null) {
            $status = proc_get_status($process);
            if ($status['running'] === false) {
                $exit_code = $status['exitcode'];
            }

            $chunk = stream_get_contents($pipes[1]);
            if ($chunk !== false) {
                fwrite($this->output, $chunk);
            }
        }

        // Close STDOUT
        fclose($pipes[1]);

        // On error, extract message from STDERR and throw an exception.
        if ($exit_code != 0) {
            $msg = stream_get_contents($pipes[2]);
            $this->cleanup($pipes, $this->output, $process);
            if ($this->log) {
                $this->log->error('Process exited with non-zero code.', [
                  'exit_code' => $exit_code,
                  'stderr' => $msg,
                ]);
            }
            throw new \RuntimeException($msg, 500);
        }

        // Return a function that streams the output.
        return function () use ($pipes, $process) {
            rewind($this->output);
            while (!feof($this->output)) {
                echo fread($this->output, 1024);
                ob_flush();
                flush();
            }
            $this->cleanup($pipes, $this->output, $process);
        };
    }
761114
public function executeCommand(CommandInterface $command, callable $callback)
    {
        $this->connection->executeCommand($command, $this->wrapCallback($callback));
    }
1371831
private function call($cmd, $arguments = array())
	{
		$command  = "{$this->baseCmd} {$cmd}" . ((!empty($arguments)) ? ' ' . implode(' ', $arguments) : '') . ' 2>&1';
		$response = shell_exec($command);

		return $response;
	}
1026245
public function callHandler(array $command, $method, $args = array())
    {
        try {
            $handlers = $this->getHandlers();
            return Handler::call($handlers, $command['command_id'], $method, $args);
        } catch (Exception $ex) {
            return $ex->getMessage();
        }
    }
83856
protected static function exec($cmd, $args)
    {
        $command = self::which($cmd) . ' ' . join(' ', array_map('escapeshellarg', $args));
        
        $descriptorspec = array(
           1 => array("pipe", "w"),  // stdout
           2 => array("pipe", "w")   // stderr
        );

        $handle = proc_open($command, $descriptorspec, $pipes);
        if (!$handle) throw new \Exception("Failed to run sox command");
        
        $out = stream_get_contents($pipes[1]);
        $err = stream_get_contents($pipes[2]);
        
        $ret = proc_close($handle);
        if ($ret != 0) throw new \Exception("$cmd command failed. " . trim($err));

        return $out ?: $err;
    }
914566
protected function runCommand(InputInterface $input, OutputInterface $output)
    {
        $repository = $this->getRepository($input, $output);
        $parameters = $input->getOption('parameter');
        self::makeQueryAndPrintResults(
            $input,
            $output,
            function (Query $query) use ($repository, $parameters) {
                return $repository->query($query, $parameters);
            }
        );
    }
124337
public function execute($js, $args = [])
    {
        $params = ['script' => $js, 'args' => $args];
        $result = $this->getDriver()->curl(
            $this->getDriver()->factoryCommand('execute', WebDriver_Command::METHOD_POST, $params)
        );
        return isset($result['value'])?$result['value']:false;
    }
165367
public function start(CommandInterface $cmd, RequestInterface $req)
    {
        $ticket = uniqid();
        $this->entries[$ticket] = [
            'command'   => $cmd,
            'request'   => $req,
            'result'    => null,
            'exception' => null,
        ];

        return $ticket;
    }
1120516
protected function executeCommand(Command $command, array $arguments, OutputInterface $output)
    {
        $output = clone $output;
        if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
            $output->setVerbosity($output->getVerbosity());
        } else {
            $output->setVerbosity(OutputInterface::VERBOSITY_QUIET);
        }

        return $command->run(new ArrayInput($arguments), $output);
    }
475278
final public static function exec(string $cmd, array $args=[]) {
		foreach ($args as $key => $val)
			$args[$key] = escapeshellarg($val);
		$cmd = vsprintf($cmd, $args);
		exec($cmd, $output, $retcode);
		if ($retcode !== 0)
			return false;
		return $output;
	}
1077376
function runCommand(\Plasma\CommandInterface $command) {
        if($this->goingAway) {
            throw new \Plasma\Exception('Client is closing all connections');
        }
        
        $connection = $this->getOptimalConnection();
        
        try {
            return $connection->runCommand($this, $command);
        } catch (\Throwable $e) {
            $this->checkinConnection($connection);
            throw $e;
        }
    }
933426
protected function execute(CommandInterface $command)
    {
        $runner = $this->getCommandRunner();

        $this->lastReturnValue = $runner->run($command);

        $this->checkReturnValue($runner);
    }
666237
public function mustRun($cmd, $error = null)
    {
        return $this->helper->mustRun($this->output, $cmd, $error);
    }
370565
public function run(InputInterface $input = null, OutputInterface $output = null)
    {
        $input  = $input ?: new ArgvInput();
        $output = $output ?: new ConsoleOutput(OutputInterface::VERBOSITY_NORMAL, null, $this->formatter());

        return parent::run($input, $output);
    }
657504
public function run($args)
    {
        if(!file_exists($this->cwd . '/bootstrap.php')) {
            Index::error('This command requires ' . $this->cwd . '/bootstrap.php to be present.');
        }

        //expecting cradle install cradle/address
        if(count($args) < 3) {
            Index::error('Not enough arguments. Usage: cradle package vendor/package command');
        }

        $cradle = cradle();
        if(file_exists($this->cwd . '/bootstrap.php')) {
            include($this->cwd . '/bootstrap.php');
        }

        try {
            $cradle->package($args[1]);
        } catch(Exception $e) {
            //it means that the package wasn't registered
            $cradle->register($args[1]);
        }

        //Setup a default error handler
        $cradle->error(function($request, $response, $error) {
            Index::error($error->getMessage() . PHP_EOL . $error->getTraceAsString());
        });

        //prepare data
        $data = Index::parseArgs(array_slice($args, 3));

        //case for root packages
        if(strpos($args[1], '/') === 0) {
            $args[1] = substr($args[1], 1);
        }

        list($author, $package) = explode('/', $args[1], 2);

        $event = $author . '-' . $package . '-' . $args[2];

        //set the the request and response
        $request = $cradle->getRequest();
        $response = $cradle->getResponse();

        $request->setStage($data);

        //see HttpTrait->render() for similar implementation
        //if prepared returned false
        if (!$cradle->prepare()) {
            //dont do anything else
            return $this;
        }

        if ($response->getStatus() == 200) {
            $continue = $cradle
                ->trigger($event, $request, $response)
                ->getEventHandler()
                ->getMeta();

            if(!$continue) {
                return $this;
            }
        }

        if (!$response->hasContent() && $response->hasJson()) {
            $json = json_encode($response->get('json'));
            $response->setContent($json);
        }

        if ($response->hasContent()) {
            echo $response->getContent();
        } else {
            Index::info($args[2] .' has successfully completed.');
        }

        //the connection is already closed
        //also remember there are no more sessions
        $cradle->shutdown();
    }
896584
public function command($command, $args = [], $id = null)
    {
        $target = Router::getInstance()->getTarget($command);
        $fn = function () use ($target) {
            return $target->run();
        };

        return $this->call($fn, $args, $id);
    }