Skip to content

Commit

Permalink
removed dependency on symfony process, replaced with proc_open function
Browse files Browse the repository at this point in the history
  • Loading branch information
Markcial committed Feb 24, 2015
1 parent 72ea2e6 commit 0082ced
Showing 1 changed file with 55 additions and 9 deletions.
64 changes: 55 additions & 9 deletions src/Psy/Command/ComposerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,26 @@
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
* @package Psy\Command
*/
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
*/
Expand Down Expand Up @@ -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;
Expand All @@ -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("<error>$buffer</error>");
$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("<error>$err</error>");
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("<info>Composer not found, downloading.</info>");
Expand Down

0 comments on commit 0082ced

Please sign in to comment.