8000 [Process] added ProcessBuilder · symfony/symfony@d7712a3 · GitHub
[go: up one dir, main page]

Skip to content

Commit d7712a3

Browse files
committed
[Process] added ProcessBuilder
This class was copied from Assetic.
1 parent 9a0aefd commit d7712a3

File tree

3 files changed

+184
-0
lines changed

3 files changed

+184
-0
lines changed

CHANGELOG-2.1.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ To get the diff between two versions, go to https://github.com/symfony/symfony/c
162162

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

165+
### Process
166+
167+
* added ProcessBuilder
168+
165169
### Routing
166170

167171
* added a TraceableUrlMatcher
Lines changed: 124 additions & 0 deletions
< 10000 td data-grid-cell-id="diff-acb8984a1ee3494f19e6d5d013079e7f0989a19237e03be85b3613b3a676cb34-empty-13-1" data-selected="false" role="gridcell" style="background-color:var(--diffBlob-additionNum-bgColor, var(--diffBlob-addition-bgColor-num));text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative left-side">13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Process;
+
14+
/**
15+
* Process builder.
16+
*
17+
* @author Kris Wallsmith <kris@symfony.com>
18+
*/
19+
class ProcessBuilder
20+
{
21+
private $arguments;
22+
private $cwd;
23+
private $env;
24+
private $stdin;
25+
private $timeout;
26+
private $options;
27+
private $inheritEnv;
28+
29+
public function __construct(array $arguments = array())
30+
{
31+
$this->arguments = $arguments;
32+
33+
$this->timeout = 60;
34+
$this->options = array();
35+
$this->inheritEnv = false;
36+
}
37+
38+
/**
39+
* Adds an unescaped argument to the command string.
40+
*
41+
* @param string $argument A command argument
42+
*/
43+
public function add($argument)
44+
{
45+
$this->arguments[] = $argument;
46+
47+
return $this;
48+
}
49+
50+
public function setWorkingDirectory($cwd)
51+
{
52+
$this->cwd = $cwd;
53+
54+
return $this;
55+
}
56+
57+
public function inheritEnvironmentVariables($inheritEnv = true)
58+
{
59+
$this->inheritEnv = $inheritEnv;
60+
61+
return $this;
62+
}
63+
64+
public function setEnv($name, $value)
65+
{
66+
if (null === $this->env) {
67+
$this->env = array();
68+
}
69+
70+
$this->env[$name] = $value;
71+
72+
return $this;
73+
}
74+
75+
public function setInput($stdin)
76+
{
77+
$this->stdin = $stdin;
78+
79+
return $this;
80+
}
81+
82+
public function setTimeout($timeout)
83+
{
84+
$this->timeout = $timeout;
85+
86+
return $this;
87+
}
88+
89+
public function setOption($name, $value)
90+
{
91+
$this->options[$name] = $value;
92+
93+
return $this;
94+
}
95+
96+
public function getProcess()
97+
{
98+
if (!count($this->arguments)) {
99+
throw new \LogicException('You must add() command arguments before calling getProcess().');
100+
}
101+
102+
$options = $this->options;
103+
104+
if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
105+
$options['bypass_shell'] = true;
106+
107+
$arguments = $this->arguments;
108+
$command = array_shift($arguments);
109+
110+
$script = '"'.$command.'"';
111+
if ($arguments) {
112+
$script .= ' '.implode(' ', array_map('escapeshellarg', $arguments));
113+
}
114+
115+
$script = 'cmd /V:ON /E:ON /C "'.$script.'"';
116+
} else {
117+
$script = implode(' ', array_map('escapeshellarg', $this->arguments));
118+
}
119+
120+
$env = $this->inheritEnv && $_ENV ? ($this->env ?: array()) + $_ENV : $this->env;
121+
122+
return new Process($script, $this->cwd, $env, $this->stdin, $this->timeout, $options);
123+
}
124+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Tests\Component\Process;
13+
14+
use Symfony\Component\Process\ProcessBuilder;
15+
16+
class ProcessBuilderTest extends \PHPUnit_Framework_TestCase
17+
{
18+
/**
19+
* @test
20+
*/
21+
public function shouldInheritEnvironmentVars()
22+
{
23+
$snapshot = $_ENV;
24+
$_ENV = $expected = array('foo' => 'bar');
25+
26+
$pb = new ProcessBuilder();
27+
$pb->add('foo')->inheritEnvironmentVariables();
28+
$proc = $pb->getProcess();
29+
30+
$this->assertEquals($expected, $proc->getEnv(), '->inheritEnvironmentVariables() copies $_ENV');
31+
32+
$_ENV = $snapshot;
33+
}
34+
35+
/**
36+
* @test
37+
*/
38+
public function shouldNotReplaceExplicitlySetVars()
39+
{
40+
$snapshot = $_ENV;
41+
$_ENV = array('foo' => 'bar');
42+
$expected = array('foo' => 'baz');
43+
44+
$pb = new ProcessBuilder();
45+
$pb
46+
->setEnv('foo', 'baz')
47+
->inheritEnvironmentVariables()
48+
->add('foo')
49+
;
50+
$proc = $pb->getProcess();
51+
52+
$this->assertEquals($expected, $proc->getEnv(), '->inheritEnvironmentVariables() copies $_ENV');
53+
54+
$_ENV = $snapshot;
55+
}
56+
}

0 commit comments

Comments
 (0)
0