Skip to content

Commit

Permalink
Add possibility to use array prefixes
Browse files Browse the repository at this point in the history
This can be useful to use /opt/custom-php/bin/php composer.phar as a prefix, it is not possible with the current implementation
  • Loading branch information
romainneutron authored and fabpot committed Aug 9, 2013
1 parent 2f34c05 commit b28d280
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/Symfony/Component/Process/ProcessBuilder.php
Expand Up @@ -28,7 +28,7 @@ class ProcessBuilder
private $timeout;
private $options;
private $inheritEnv;
private $prefix;
private $prefix = array();

public function __construct(array $arguments = array())
{
Expand Down Expand Up @@ -64,13 +64,13 @@ public function add($argument)
*
* The prefix is preserved when reseting arguments.
*
* @param string $prefix A command prefix
* @param string|array $prefix A command prefix or an array of command prefixes
*
* @return ProcessBuilder
*/
public function setPrefix($prefix)
{
$this->prefix = $prefix;
$this->prefix = is_array($prefix) ? $prefix : array($prefix);

return $this;
}
Expand Down Expand Up @@ -154,13 +154,13 @@ public function setOption($name, $value)

public function getProcess()
{
if (!$this->prefix && !count($this->arguments)) {
if (0 === count($this->prefix) && 0 === count($this->arguments)) {
throw new LogicException('You must add() command arguments before calling getProcess().');
}

$options = $this->options;

$arguments = $this->prefix ? array_merge(array($this->prefix), $this->arguments) : $this->arguments;
$arguments = array_merge($this->prefix, $this->arguments);
$script = implode(' ', array_map(array(__NAMESPACE__.'\\ProcessUtils', 'escapeArgument'), $arguments));

if ($this->inheritEnv) {
Expand Down
20 changes: 20 additions & 0 deletions src/Symfony/Component/Process/Tests/ProcessBuilderTest.php
Expand Up @@ -140,6 +140,26 @@ public function testPrefixIsPrependedToAllGeneratedProcess()
}
}

public function testArrayPrefixesArePrependedToAllGeneratedProcess()
{
$pb = new ProcessBuilder();
$pb->setPrefix(array('/usr/bin/php', 'composer.phar'));

$proc = $pb->setArguments(array('-v'))->getProcess();
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
$this->assertEquals('"/usr/bin/php" "composer.phar" "-v"', $proc->getCommandLine());
} else {
$this->assertEquals("'/usr/bin/php' 'composer.phar' '-v'", $proc->getCommandLine());
}

$proc = $pb->setArguments(array('-i'))->getProcess();
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
$this->assertEquals('"/usr/bin/php" "composer.phar" "-i"', $proc->getCommandLine());
} else {
$this->assertEquals("'/usr/bin/php' 'composer.phar' '-i'", $proc->getCommandLine());
}
}

public function testShouldEscapeArguments()
{
$pb = new ProcessBuilder(array('%path%', 'foo " bar'));
Expand Down

0 comments on commit b28d280

Please sign in to comment.