forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessHelper.php
More file actions
91 lines (76 loc) · 2.27 KB
/
ProcessHelper.php
File metadata and controls
91 lines (76 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php declare(strict_types = 1);
namespace PHPStan\Process;
use PHPStan\Command\AnalyseCommand;
use Symfony\Component\Console\Input\InputInterface;
use function array_merge;
use function escapeshellarg;
use function implode;
use function ini_get;
use function is_bool;
use function php_ini_loaded_file;
use function sprintf;
use function sys_get_temp_dir;
use const PHP_BINARY;
final class ProcessHelper
{
/**
* @param string[] $additionalItems
*/
public static function getWorkerCommand(
string $mainScript,
string $commandName,
?string $projectConfigFile,
array $additionalItems,
InputInterface $input,
): string
{
$phpIni = php_ini_loaded_file();
$phpCmd = $phpIni === false ? escapeshellarg(PHP_BINARY) : sprintf('%s -c %s', escapeshellarg(PHP_BINARY), escapeshellarg($phpIni));
$processCommandArray = [
$phpCmd,
'-d',
// quote value so PHP will parse it as a string when the path contains a bitwise operator like ~
'sys_temp_dir=' . escapeshellarg("'" . sys_get_temp_dir() . "'"),
];
if ($input->getOption('memory-limit') === null) {
$processCommandArray[] = '-d';
$processCommandArray[] = 'memory_limit=' . ini_get('memory_limit');
}
foreach ([$mainScript, $commandName] as $arg) {
$processCommandArray[] = escapeshellarg($arg);
}
if ($projectConfigFile !== null) {
$processCommandArray[] = '--configuration';
$processCommandArray[] = escapeshellarg($projectConfigFile);
}
$options = [
AnalyseCommand::OPTION_LEVEL,
'autoload-file',
'memory-limit',
'xdebug',
'verbose',
];
foreach ($options as $optionName) {
/** @var bool|string|null $optionValue */
$optionValue = $input->getOption($optionName);
if (is_bool($optionValue)) {
if ($optionValue === true) {
$processCommandArray[] = sprintf('--%s', $optionName);
}
continue;
}
if ($optionValue === null) {
continue;
}
$processCommandArray[] = sprintf('--%s=%s', $optionName, escapeshellarg($optionValue));
}
$processCommandArray = array_merge($processCommandArray, $additionalItems);
$processCommandArray[] = '--';
/** @var string[] $paths */
$paths = $input->getArgument('paths');
foreach ($paths as $path) {
$processCommandArray[] = escapeshellarg($path);
}
return implode(' ', $processCommandArray);
}
}