Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
56
tests/Symfony/Tests/Component/Process/ProcessBuilderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |