1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of tenside/core. |
5
|
|
|
* |
6
|
|
|
* (c) Christian Schiffler <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* This project is provided in good faith and hope to be usable by anyone. |
12
|
|
|
* |
13
|
|
|
* @package tenside/core |
14
|
|
|
* @author Christian Schiffler <[email protected]> |
15
|
|
|
* @copyright 2015 Christian Schiffler <[email protected]> |
16
|
|
|
* @license https://github.com/tenside/core/blob/master/LICENSE MIT |
17
|
|
|
* @link https://github.com/tenside/core |
18
|
|
|
* @filesource |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace Tenside\Core\Util; |
22
|
|
|
|
23
|
|
|
use Symfony\Component\Process\Process; |
24
|
|
|
use Tenside\Core\Config\TensideJsonConfig; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* This class implements a php process spawner. |
28
|
|
|
*/ |
29
|
|
|
class PhpProcessSpawner |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* The configuration in use. |
33
|
|
|
* |
34
|
|
|
* @var TensideJsonConfig |
35
|
|
|
*/ |
36
|
|
|
private $config; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The home directory to run the process in. |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $homePath; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Create a new instance. |
47
|
|
|
* |
48
|
|
|
* @param TensideJsonConfig $config The configuration in use. |
49
|
|
|
* |
50
|
|
|
* @param string $homePath The directory to use as home directory. |
51
|
|
|
*/ |
52
|
|
|
public function __construct(TensideJsonConfig $config, $homePath) |
53
|
|
|
{ |
54
|
|
|
$this->config = $config; |
55
|
|
|
$this->homePath = $homePath; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Create a new instance. |
60
|
|
|
* |
61
|
|
|
* @param TensideJsonConfig $config The configuration in use. |
62
|
|
|
* |
63
|
|
|
* @param string $homePath The directory to use as home directory. |
64
|
|
|
* |
65
|
|
|
* @return PhpProcessSpawner |
66
|
|
|
*/ |
67
|
|
|
public static function create(TensideJsonConfig $config, $homePath) |
68
|
|
|
{ |
69
|
|
|
return new static($config, $homePath); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Run the process. |
74
|
|
|
* |
75
|
|
|
* @param array $arguments The additional arguments to add to the call. |
76
|
|
|
* |
77
|
|
|
* @return Process |
78
|
|
|
*/ |
79
|
|
|
public function spawn($arguments) |
80
|
|
|
{ |
81
|
|
|
$cmd = sprintf( |
82
|
|
|
'%s %s', |
83
|
|
|
escapeshellcmd($this->config->getPhpCliBinary()), |
84
|
|
|
$this->getArguments($arguments) |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
return new Process($cmd, $this->homePath, $this->getEnvironment(), null, null); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Retrieve the command line arguments to use. |
92
|
|
|
* |
93
|
|
|
* @param array $additionalArguments The additional arguments to add to the call. |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
private function getArguments($additionalArguments) |
98
|
|
|
{ |
99
|
|
|
$arguments = []; |
100
|
|
|
if (null !== ($cliArguments = $this->config->getPhpCliArguments())) { |
101
|
|
|
foreach ($cliArguments as $argument) { |
102
|
|
|
$arguments[] = $argument; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
$arguments = array_map('escapeshellarg', array_merge($arguments, $additionalArguments)); |
106
|
|
|
|
107
|
|
|
return implode(' ', $arguments); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Retrieve the command line environment variables to use. |
112
|
|
|
* |
113
|
|
|
* @return array |
114
|
|
|
*/ |
115
|
|
|
private function getEnvironment() |
116
|
|
|
{ |
117
|
|
|
$variables = $this->getDefinedEnvironmentVariables(); |
118
|
|
|
|
119
|
|
|
if (null === ($environment = $this->config->getPhpCliEnvironment())) { |
120
|
|
|
return $variables; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$variables = array_merge($variables, $environment); |
124
|
|
|
|
125
|
|
|
return $variables; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Retrieve the passed environment variables from the current session and return them. |
130
|
|
|
* |
131
|
|
|
* @return array |
132
|
|
|
* |
133
|
|
|
* @SuppressWarnings(PHPMD.Superglobals) |
134
|
|
|
* @SuppressWarnings(PHPMD.CamelCaseVariableName) |
135
|
|
|
*/ |
136
|
|
|
private function getDefinedEnvironmentVariables() |
137
|
|
|
{ |
138
|
|
|
$names = array_merge( |
139
|
|
|
['SYMFONY_ENV', 'SYMFONY_DEBUG', 'COMPOSER', 'HOME', 'USER'], |
140
|
|
|
array_keys($_ENV) |
141
|
|
|
); |
142
|
|
|
|
143
|
|
|
$variables = []; |
144
|
|
|
foreach ($names as $name) { |
145
|
|
|
if (false !== ($composerEnv = getenv($name))) { |
146
|
|
|
$variables[$name] = $composerEnv; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $variables; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|