Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <fabien@symfony.com>
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 Symfony\Component\Console\Helper;
13
 
14
use Symfony\Component\Console\Output\ConsoleOutputInterface;
15
use Symfony\Component\Console\Output\OutputInterface;
16
use Symfony\Component\Process\Exception\ProcessFailedException;
17
use Symfony\Component\Process\Process;
18
 
19
/**
20
 * The ProcessHelper class provides helpers to run external processes.
21
 *
22
 * @author Fabien Potencier <fabien@symfony.com>
23
 *
24
 * @final
25
 */
26
class ProcessHelper extends Helper
27
{
28
    /**
29
     * Runs an external process.
30
     *
31
     * @param array|Process $cmd      An instance of Process or an array of the command and arguments
32
     * @param callable|null $callback A PHP callback to run whenever there is some
33
     *                                output available on STDOUT or STDERR
34
     */
35
    public function run(OutputInterface $output, array|Process $cmd, string $error = null, callable $callback = null, int $verbosity = OutputInterface::VERBOSITY_VERY_VERBOSE): Process
36
    {
37
        if (!class_exists(Process::class)) {
38
            throw new \LogicException('The ProcessHelper cannot be run as the Process component is not installed. Try running "compose require symfony/process".');
39
        }
40
 
41
        if ($output instanceof ConsoleOutputInterface) {
42
            $output = $output->getErrorOutput();
43
        }
44
 
45
        $formatter = $this->getHelperSet()->get('debug_formatter');
46
 
47
        if ($cmd instanceof Process) {
48
            $cmd = [$cmd];
49
        }
50
 
51
        if (\is_string($cmd[0] ?? null)) {
52
            $process = new Process($cmd);
53
            $cmd = [];
54
        } elseif (($cmd[0] ?? null) instanceof Process) {
55
            $process = $cmd[0];
56
            unset($cmd[0]);
57
        } else {
58
            throw new \InvalidArgumentException(sprintf('Invalid command provided to "%s()": the command should be an array whose first element is either the path to the binary to run or a "Process" object.', __METHOD__));
59
        }
60
 
61
        if ($verbosity <= $output->getVerbosity()) {
62
            $output->write($formatter->start(spl_object_hash($process), $this->escapeString($process->getCommandLine())));
63
        }
64
 
65
        if ($output->isDebug()) {
66
            $callback = $this->wrapCallback($output, $process, $callback);
67
        }
68
 
69
        $process->run($callback, $cmd);
70
 
71
        if ($verbosity <= $output->getVerbosity()) {
72
            $message = $process->isSuccessful() ? 'Command ran successfully' : sprintf('%s Command did not run successfully', $process->getExitCode());
73
            $output->write($formatter->stop(spl_object_hash($process), $message, $process->isSuccessful()));
74
        }
75
 
76
        if (!$process->isSuccessful() && null !== $error) {
77
            $output->writeln(sprintf('<error>%s</error>', $this->escapeString($error)));
78
        }
79
 
80
        return $process;
81
    }
82
 
83
    /**
84
     * Runs the process.
85
     *
86
     * This is identical to run() except that an exception is thrown if the process
87
     * exits with a non-zero exit code.
88
     *
89
     * @param array|Process $cmd      An instance of Process or a command to run
90
     * @param callable|null $callback A PHP callback to run whenever there is some
91
     *                                output available on STDOUT or STDERR
92
     *
93
     * @throws ProcessFailedException
94
     *
95
     * @see run()
96
     */
97
    public function mustRun(OutputInterface $output, array|Process $cmd, string $error = null, callable $callback = null): Process
98
    {
99
        $process = $this->run($output, $cmd, $error, $callback);
100
 
101
        if (!$process->isSuccessful()) {
102
            throw new ProcessFailedException($process);
103
        }
104
 
105
        return $process;
106
    }
107
 
108
    /**
109
     * Wraps a Process callback to add debugging output.
110
     */
111
    public function wrapCallback(OutputInterface $output, Process $process, callable $callback = null): callable
112
    {
113
        if ($output instanceof ConsoleOutputInterface) {
114
            $output = $output->getErrorOutput();
115
        }
116
 
117
        $formatter = $this->getHelperSet()->get('debug_formatter');
118
 
119
        return function ($type, $buffer) use ($output, $process, $callback, $formatter) {
120
            $output->write($formatter->progress(spl_object_hash($process), $this->escapeString($buffer), Process::ERR === $type));
121
 
122
            if (null !== $callback) {
123
                $callback($type, $buffer);
124
            }
125
        };
126
    }
127
 
128
    private function escapeString(string $str): string
129
    {
130
        return str_replace('<', '\\<', $str);
131
    }
132
 
133
    public function getName(): string
134
    {
135
        return 'process';
136
    }
137
}