-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcess.php
More file actions
98 lines (85 loc) · 3.16 KB
/
Copy pathProcess.php
File metadata and controls
98 lines (85 loc) · 3.16 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
92
93
94
95
96
97
98
<?php
declare(strict_types=1);
namespace Php\Pie\Util;
use Composer\IO\IOInterface;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process as SymfonyProcess;
use function sprintf;
use function str_contains;
use function strtolower;
use function trim;
/** @internal This is not public API for PIE, so should not be depended upon unless you accept the risk of BC breaks */
final class Process
{
public const NO_TIMEOUT = null;
public const SHORT_TIMEOUT = 10;
private function __construct()
{
}
/**
* Just a helper to invoke a Symfony Process command with a simplified API
* for the common invocations we have in PIE.
*
* Things to note:
* - uses mustRun (i.e. throws exception if command execution fails)
* - very short timeout by default (5 seconds)
* - output is trimmed
*
* @param list<string> $command
* @param callable(SymfonyProcess::ERR|SymfonyProcess::OUT, string): void|null $outputCallback
* @param array<string, scalar>|null $env
*
* @throws ProcessFailedException
*/
public static function run(
array $command,
string|null $workingDirectory = null,
int|null $timeout = self::NO_TIMEOUT,
callable|null $outputCallback = null,
array|null $env = null,
): string {
return trim((new SymfonyProcess($command, $workingDirectory, $env, timeout: $timeout))
->mustRun($outputCallback)
->getOutput());
}
/**
* @param IOInterface::* $minVerbosity
*
* @return callable(SymfonyProcess::ERR|SymfonyProcess::OUT, string): void|null
*/
public static function outputCallbackForVerbosity(IOInterface $io, int $minVerbosity): callable|null
{
if (
($minVerbosity === IOInterface::VERBOSE && ! $io->isVerbose() && ! $io->isVeryVerbose() && ! $io->isDebug())
|| ($minVerbosity === IOInterface::VERY_VERBOSE && ! $io->isVeryVerbose() && ! $io->isDebug())
|| ($minVerbosity === IOInterface::DEBUG && ! $io->isDebug())
) {
return null;
}
return static function (string $type, string $outputMessage) use ($io): void {
$io->write(sprintf(
'%s%s%s',
$type === SymfonyProcess::ERR ? '<comment>' : '',
$outputMessage,
$type === SymfonyProcess::ERR ? '</comment>' : '',
), false);
};
}
public static function processProbablyPermissionDenied(ProcessFailedException $e): bool
{
$mergedProcessOutput = strtolower($e->getProcess()->getErrorOutput() . $e->getProcess()->getOutput());
$needles = [
'permission denied',
'you must be root',
'operation not permitted',
'are you root',
'has to be run with superuser privileges',
];
foreach ($needles as $needle) {
if (str_contains($mergedProcessOutput, $needle)) {
return true;
}
}
return false;
}
}