Skip to content

Commit

Permalink
[Process] added ProcessBuilder
Browse files Browse the repository at this point in the history
This class was copied from Assetic.
  • Loading branch information
kriswallsmith committed Dec 16, 2011
1 parent 9a0aefd commit d7712a3
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG-2.1.md
Expand Up @@ -162,6 +162,10 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c

* added Locale::getIcuVersion() and Locale::getIcuDataVersion()

### Process

* added ProcessBuilder

### Routing

* added a TraceableUrlMatcher
Expand Down
124 changes: 124 additions & 0 deletions src/Symfony/Component/Process/ProcessBuilder.php
@@ -0,0 +1,124 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Process;

/**
* Process builder.
*
* @author Kris Wallsmith <kris@symfony.com>
*/
class ProcessBuilder
{
private $arguments;
private $cwd;
private $env;
private $stdin;
private $timeout;
private $options;
private $inheritEnv;

public function __construct(array $arguments = array())
{
$this->arguments = $arguments;

$this->timeout = 60;
$this->options = array();
$this->inheritEnv = false;
}

/**
* Adds an unescaped argument to the command string.
*
* @param string $argument A command argument
*/
public function add($argument)
{
$this->arguments[] = $argument;

return $this;
}

public function setWorkingDirectory($cwd)
{
$this->cwd = $cwd;

return $this;
}

public function inheritEnvironmentVariables($inheritEnv = true)
{
$this->inheritEnv = $inheritEnv;

return $this;
}

public function setEnv($name, $value)
{
if (null === $this->env) {
$this->env = array();
}

$this->env[$name] = $value;

return $this;
}

public function setInput($stdin)
{
$this->stdin = $stdin;

return $this;
}

public function setTimeout($timeout)
{
$this->timeout = $timeout;

return $this;
}

public function setOption($name, $value)
{
$this->options[$name] = $value;

return $this;
}

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

$options = $this->options;

if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
$options['bypass_shell'] = true;

$arguments = $this->arguments;
$command = array_shift($arguments);

$script = '"'.$command.'"';
if ($arguments) {
$script .= ' '.implode(' ', array_map('escapeshellarg', $arguments));
}

$script = 'cmd /V:ON /E:ON /C "'.$script.'"';
} else {
$script = implode(' ', array_map('escapeshellarg', $this->arguments));
}

$env = $this->inheritEnv && $_ENV ? ($this->env ?: array()) + $_ENV : $this->env;

return new Process($script, $this->cwd, $env, $this->stdin, $this->timeout, $options);
}
}
56 changes: 56 additions & 0 deletions tests/Symfony/Tests/Component/Process/ProcessBuilderTest.php
@@ -0,0 +1,56 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Tests\Component\Process;

use Symfony\Component\Process\ProcessBuilder;

class ProcessBuilderTest extends \PHPUnit_Framework_TestCase
{
/**
* @test
*/
public function shouldInheritEnvironmentVars()
{
$snapshot = $_ENV;
$_ENV = $expected = array('foo' => 'bar');

$pb = new ProcessBuilder();
$pb->add('foo')->inheritEnvironmentVariables();
$proc = $pb->getProcess();

$this->assertEquals($expected, $proc->getEnv(), '->inheritEnvironmentVariables() copies $_ENV');

$_ENV = $snapshot;
}

/**
* @test
*/
public function shouldNotReplaceExplicitlySetVars()
{
$snapshot = $_ENV;
$_ENV = array('foo' => 'bar');
$expected = array('foo' => 'baz');

$pb = new ProcessBuilder();
$pb
->setEnv('foo', 'baz')
->inheritEnvironmentVariables()
->add('foo')
;
$proc = $pb->getProcess();

$this->assertEquals($expected, $proc->getEnv(), '->inheritEnvironmentVariables() copies $_ENV');

$_ENV = $snapshot;
}
}

0 comments on commit d7712a3

Please sign in to comment.