[Process] added ProcessBuilder
This class was copied from Assetic.
This commit is contained in:
parent
9a0aefd192
commit
d7712a3e2a
@ -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
|
||||
|
124
src/Symfony/Component/Process/ProcessBuilder.php
Normal file
124
src/Symfony/Component/Process/ProcessBuilder.php
Normal file
@ -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
tests/Symfony/Tests/Component/Process/ProcessBuilderTest.php
Normal file
56
tests/Symfony/Tests/Component/Process/ProcessBuilderTest.php
Normal file
@ -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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user