Newer
Older
Klaranouba7
a validé
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Flex;
use Composer\Composer;
use Composer\EventDispatcher\ScriptExecutionException;
use Composer\IO\IOInterface;
use Composer\Semver\Constraint\MatchAllConstraint;
use Composer\Util\ProcessExecutor;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\StreamOutput;
use Symfony\Component\Process\PhpExecutableFinder;
/**
* @author Fabien Potencier <fabien@symfony.com>
*/
class ScriptExecutor
{
private $composer;
private $io;
private $options;
private $executor;
public function __construct(Composer $composer, IOInterface $io, Options $options, ProcessExecutor $executor = null)
{
$this->composer = $composer;
$this->io = $io;
$this->options = $options;
$this->executor = $executor ?: new ProcessExecutor();
}
/**
* @throws ScriptExecutionException if the executed command returns a non-0 exit code
*/
public function execute(string $type, string $cmd)
{
$parsedCmd = $this->options->expandTargetDir($cmd);
if (null === $expandedCmd = $this->expandCmd($type, $parsedCmd)) {
return;
}
$cmdOutput = new StreamOutput(fopen('php://temp', 'rw'), OutputInterface::VERBOSITY_VERBOSE, $this->io->isDecorated());
$outputHandler = function ($type, $buffer) use ($cmdOutput) {
$cmdOutput->write($buffer, false, OutputInterface::OUTPUT_RAW);
};
$this->io->writeError(sprintf('Executing script %s', $parsedCmd), $this->io->isVerbose());
$exitCode = $this->executor->execute($expandedCmd, $outputHandler);
$code = 0 === $exitCode ? ' <info>[OK]</>' : ' <error>[KO]</>';
if ($this->io->isVerbose()) {
$this->io->writeError(sprintf('Executed script %s %s', $cmd, $code));
} else {
$this->io->writeError($code);
}
if (0 !== $exitCode) {
$this->io->writeError(' <error>[KO]</>');
$this->io->writeError(sprintf('<error>Script %s returned with error code %s</>', $cmd, $exitCode));
fseek($cmdOutput->getStream(), 0);
foreach (explode("\n", stream_get_contents($cmdOutput->getStream())) as $line) {
$this->io->writeError('!! '.$line);
}
throw new ScriptExecutionException($cmd, $exitCode);
}
}
private function expandCmd(string $type, string $cmd)
{
switch ($type) {
case 'symfony-cmd':
return $this->expandSymfonyCmd($cmd);
case 'php-script':
return $this->expandPhpScript($cmd);
case 'script':
return $cmd;
default:
throw new \InvalidArgumentException(sprintf('Invalid symfony/flex auto-script in composer.json: "%s" is not a valid type of command.', $type));
}
}
private function expandSymfonyCmd(string $cmd)
{
$repo = $this->composer->getRepositoryManager()->getLocalRepository();
if (!$repo->findPackage('symfony/console', new MatchAllConstraint())) {
$this->io->writeError(sprintf('<warning>Skipping "%s" (needs symfony/console to run).</>', $cmd));
return null;
}
$console = ProcessExecutor::escape($this->options->get('root-dir').'/'.$this->options->get('bin-dir').'/console');
if ($this->io->isDecorated()) {
$console .= ' --ansi';
}
return $this->expandPhpScript($console.' '.$cmd);
}
private function expandPhpScript(string $cmd): string
{
$phpFinder = new PhpExecutableFinder();
if (!$php = $phpFinder->find(false)) {
throw new \RuntimeException('The PHP executable could not be found, add it to your PATH and try again.');
}
$arguments = $phpFinder->findArguments();
if ($env = (string) (getenv('COMPOSER_ORIGINAL_INIS'))) {
$paths = explode(\PATH_SEPARATOR, $env);
$ini = array_shift($paths);
} else {
$ini = php_ini_loaded_file();
}
if ($ini) {
$arguments[] = '--php-ini='.$ini;
}
$phpArgs = implode(' ', array_map([ProcessExecutor::class, 'escape'], $arguments));
return ProcessExecutor::escape($php).($phpArgs ? ' '.$phpArgs : '').' '.$cmd;
}
}