From 72ea2e68696760e6a2b32a5bbce2cf1edcf91fd1 Mon Sep 17 00:00:00 2001 From: Marc Garcia Date: Tue, 17 Feb 2015 14:53:57 +0100 Subject: [PATCH] refactoring of the arguments for the commands, replaced proc_open in deferral for process symfony library --- src/Psy/Command/ComposerCommand.php | 94 +++++++---------------------- src/Psy/Command/SandboxCommand.php | 26 ++------ 2 files changed, 28 insertions(+), 92 deletions(-) diff --git a/src/Psy/Command/ComposerCommand.php b/src/Psy/Command/ComposerCommand.php index 253a1cc72..529aef9c2 100644 --- a/src/Psy/Command/ComposerCommand.php +++ b/src/Psy/Command/ComposerCommand.php @@ -2,11 +2,11 @@ namespace Psy\Command; -use Composer\Console\Application; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\StringInput; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Process\Process; /** * Class ComposerCommand @@ -14,29 +14,11 @@ */ class ComposerCommand extends Command { - const PIPE_WRITE = 0; - const PIPE_READ = 1; - const PIPE_ERR = 2; - - /** - * @var array - */ - protected $specification = array( - self::PIPE_WRITE => array("pipe", "r"), - self::PIPE_READ => array("pipe", "w"), - self::PIPE_ERR => array("pipe", "w"), - ); - /** * @var string */ protected $composerPath; - /** - * @var string - */ - protected $installationType; - /** * @var InputInterface */ @@ -55,7 +37,6 @@ protected function configure() $this ->setName('composer') ->setDefinition(array( - new InputArgument('command-name', InputArgument::REQUIRED, ''), new InputArgument('args', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, ''), )) ->setDescription('Composer installation.') @@ -83,25 +64,9 @@ protected function execute(InputInterface $input, OutputInterface $output) $this->callComposerBootstrap(); - // extract real command name - $tokens = preg_split('{\s+}', $input->__toString()); - $args = array(); - foreach ($tokens as $token) { - if ($token && $token[0] !== '-') { - $args[] = $token; - if (count($args) >= 2) { - break; - } - } - } - // show help for this command if no command was found - if (count($args) < 2) { - return parent::run($input, $output); - } - - $app = new Application(); + $app = new \Composer\Console\Application(); $app->setAutoExit(false); - $input = new StringInput(implode(' ', array_slice($tokens, 1, count($tokens)))); + $input = new StringInput(trim(preg_replace(sprintf('#^%s#', $this->getName()), '', $input->__toString()))); return $app->doRun($input, $this->output); } @@ -112,21 +77,16 @@ public function setComposerPath($path) } /** - * @return string + * */ - protected function getSystemShell() - { - if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { - return 'cmd.exe'; - } - - return 'bash'; - } - protected function callComposerBootstrap() { - // TODO if the path is provided as paramater use it instead - require_once 'phar://composer.phar/src/bootstrap.php'; + $dependency = sprintf( + 'phar://%s/src/bootstrap.php', + !is_null($this->composerPath) ? $this->composerPath : $this->getLocalComposerFile() + ); + + require_once $dependency; } /** @@ -135,34 +95,26 @@ protected function callComposerBootstrap() */ protected function shellCommand($command) { - $process = proc_open($this->getSystemShell(), $this->specification, $pipes); - stream_set_blocking($pipes[self::PIPE_ERR], 0); - - if (is_resource($process)) { - fwrite($pipes[self::PIPE_WRITE], $command); - fclose($pipes[self::PIPE_WRITE]); - - if ($err = stream_get_contents($pipes[self::PIPE_ERR])) { - $err = trim($err); - $this->output->writeln("$err"); - - return false; + /** PHP 5.4 compatibility, closures have no this scope in 5.4 versions */ + $output = $this->output; + $process = new Process($command); + $process->run(function ($type, $buffer) use ($output) { + if (Process::ERR === $type) { + $this->output->writeln("$buffer"); } + }); - $return = stream_get_contents($pipes[self::PIPE_READ]); - fclose($pipes[self::PIPE_READ]); - - if (proc_close($process) === 0) { - return trim($return); - } - } + return $process->getOutput(); + } - return false; + protected function getLocalComposerFile() + { + return getcwd() . DIRECTORY_SEPARATOR . 'composer.phar'; } protected function checkComposerInstallation() { - return @file_exists('composer.phar') or is_null($this->composerPath); + return @file_exists($this->getLocalComposerFile()) or !is_null($this->composerPath); } protected function installComposer() diff --git a/src/Psy/Command/SandboxCommand.php b/src/Psy/Command/SandboxCommand.php index 22c0c2e21..8d9ff0890 100644 --- a/src/Psy/Command/SandboxCommand.php +++ b/src/Psy/Command/SandboxCommand.php @@ -70,10 +70,10 @@ protected function configure() InputArgument::OPTIONAL, 'Action to perform : add|create, list, which, use|switch, clear|delete, exit|reset' ), - new InputArgument('args', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, ''), + new InputArgument('name', InputArgument::OPTIONAL), new InputOption( 'grep', - null, + 'g', InputOption::VALUE_OPTIONAL, 'grep parameter' ), @@ -99,24 +99,8 @@ protected function execute(InputInterface $input, OutputInterface $output) $this->input = $input; $this->output = $output; - // extract real command name - $tokens = preg_split('{\s+}', $input->__toString()); - $args = array(); - foreach ($tokens as $token) { - if ($token && $token[0] !== '-') { - $args[] = $token; - if (count($args) >= 3) { - break; - } - } - } - - $name = null; - array_shift($args); // command name - $action = array_shift($args); - if (count($args) > 0) { - $name = array_shift($args); - } + $name = $input->getArgument('name'); + $action = $input->getArgument('action'); $this->performAction($action, $name); } @@ -190,7 +174,7 @@ protected function getSandboxPath($name) protected function createSandbox($name = null) { if (is_null($this->restoreFolder)) { - $this->restoreFolder = exec('pwd'); + $this->restoreFolder = getcwd(); } $name = !is_null($name) ? $name : uniqid();