|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Assetic package, an OpenSky project. |
|
|
5 |
* |
|
|
6 |
* (c) 2010-2011 OpenSky Project Inc |
|
|
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 Assetic\Util; |
|
|
13 |
|
|
|
14 |
/** |
|
|
15 |
* Process builder. |
|
|
16 |
* |
|
|
17 |
* @author Kris Wallsmith <kris.wallsmith@gmail.com> |
|
|
18 |
*/ |
|
|
19 |
class ProcessBuilder |
|
|
20 |
{ |
|
|
21 |
private $parts = array(); |
|
|
22 |
private $cwd; |
|
|
23 |
private $env; |
|
|
24 |
private $stdin; |
|
|
25 |
private $timeout = 60; |
|
|
26 |
private $options = array(); |
|
|
27 |
private $inheritEnv = false; |
|
|
28 |
|
|
|
29 |
public function add($part) |
|
|
30 |
{ |
|
|
31 |
$this->parts[] = $part; |
|
|
32 |
|
|
|
33 |
return $this; |
|
|
34 |
} |
|
|
35 |
|
|
|
36 |
public function setWorkingDirectory($cwd) |
|
|
37 |
{ |
|
|
38 |
$this->cwd = $cwd; |
|
|
39 |
|
|
|
40 |
return $this; |
|
|
41 |
} |
|
|
42 |
|
|
|
43 |
public function inheritEnvironmentVariables($inheritEnv = true) |
|
|
44 |
{ |
|
|
45 |
$this->inheritEnv = $inheritEnv; |
|
|
46 |
|
|
|
47 |
return $this; |
|
|
48 |
} |
|
|
49 |
|
|
|
50 |
public function setEnv($name, $value) |
|
|
51 |
{ |
|
|
52 |
if (null === $this->env) { |
|
|
53 |
$this->env = array(); |
|
|
54 |
} |
|
|
55 |
|
|
|
56 |
$this->env[$name] = $value; |
|
|
57 |
|
|
|
58 |
return $this; |
|
|
59 |
} |
|
|
60 |
|
|
|
61 |
public function setInput($stdin) |
|
|
62 |
{ |
|
|
63 |
$this->stdin = $stdin; |
|
|
64 |
|
|
|
65 |
return $this; |
|
|
66 |
} |
|
|
67 |
|
|
|
68 |
public function setTimeout($timeout) |
|
|
69 |
{ |
|
|
70 |
$this->timeout = $timeout; |
|
|
71 |
|
|
|
72 |
return $this; |
|
|
73 |
} |
|
|
74 |
|
|
|
75 |
public function setOption($name, $value) |
|
|
76 |
{ |
|
|
77 |
$this->options[$name] = $value; |
|
|
78 |
|
|
|
79 |
return $this; |
|
|
80 |
} |
|
|
81 |
|
|
|
82 |
public function getProcess() |
|
|
83 |
{ |
|
|
84 |
if (!count($this->parts)) { |
|
|
85 |
throw new \LogicException('You must add() command parts before calling getProcess().'); |
|
|
86 |
} |
|
|
87 |
|
|
|
88 |
$parts = $this->parts; |
|
|
89 |
$cmd = array_shift($parts); |
|
|
90 |
$script = escapeshellcmd($cmd).' '.implode(' ', array_map('escapeshellarg', $parts)); |
|
|
91 |
|
|
|
92 |
$env = $this->inheritEnv ? ($this->env ?: array()) + $_ENV : $this->env; |
|
|
93 |
|
|
|
94 |
return new Process($script, $this->cwd, $env, $this->stdin, $this->timeout, $this->options); |
|
|
95 |
} |
|
|
96 |
} |