Skip to content

Commit

Permalink
refactoring of the arguments for the commands, replaced proc_open in …
Browse files Browse the repository at this point in the history
…deferral for process symfony library
  • Loading branch information
Markcial committed Feb 17, 2015
1 parent 74fe66b commit 72ea2e6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 92 deletions.
94 changes: 23 additions & 71 deletions src/Psy/Command/ComposerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,23 @@

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
* @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
*/
protected $composerPath;

/**
* @var string
*/
protected $installationType;

/**
* @var InputInterface
*/
Expand All @@ -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.')
Expand Down Expand Up @@ -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);
}
Expand All @@ -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;
}

/**
Expand All @@ -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("<error>$err</error>");

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("<error>$buffer</error>");
}
});

$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()
Expand Down
26 changes: 5 additions & 21 deletions src/Psy/Command/SandboxCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
),
Expand All @@ -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);
}
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 72ea2e6

Please sign in to comment.