From 0082ced6ef1ec7ffef54c0086b197e2a3d29ce33 Mon Sep 17 00:00:00 2001 From: Marc Garcia Date: Tue, 24 Feb 2015 15:08:16 +0100 Subject: [PATCH] removed dependency on symfony process, replaced with proc_open function --- src/Psy/Command/ComposerCommand.php | 64 +++++++++++++++++++++++++---- 1 file changed, 55 insertions(+), 9 deletions(-) diff --git a/src/Psy/Command/ComposerCommand.php b/src/Psy/Command/ComposerCommand.php index 529aef9c2..c33abf9e4 100644 --- a/src/Psy/Command/ComposerCommand.php +++ b/src/Psy/Command/ComposerCommand.php @@ -6,7 +6,6 @@ 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,6 +13,19 @@ */ 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 */ @@ -71,6 +83,9 @@ protected function execute(InputInterface $input, OutputInterface $output) return $app->doRun($input, $this->output); } + /** + * @param $path + */ public function setComposerPath($path) { $this->composerPath = $path; @@ -95,28 +110,59 @@ protected function callComposerBootstrap() */ protected function shellCommand($command) { - /** 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"); + $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; } - }); - return $process->getOutput(); + $return = stream_get_contents($pipes[self::PIPE_READ]); + fclose($pipes[self::PIPE_READ]); + + if (proc_close($process) === 0) { + return trim($return); + } + } } + /** + * @return string + */ + protected function getSystemShell() + { + if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { + return 'cmd.exe'; + } + + return 'bash'; + } + + /** + * @return string + */ protected function getLocalComposerFile() { return getcwd() . DIRECTORY_SEPARATOR . 'composer.phar'; } + /** + * @return bool + */ protected function checkComposerInstallation() { return @file_exists($this->getLocalComposerFile()) or !is_null($this->composerPath); } + /** + * + */ protected function installComposer() { $this->output->writeln("Composer not found, downloading.");