PluginProbe
Depicter — Popup & Slider Builder / 1.3.2
Depicter — Popup & Slider Builder v1.3.2
4.8.1 trunk 1.0.0 1.1.0 1.1.2 1.1.4 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.5 1.3.8 1.5.0 1.5.1 1.5.2 1.5.5 1.6.0 1.6.1 1.6.2 1.7.0 All 76 releases
depicter / vendor / symfony / console / Helper / ProcessHelper.php

ProcessHelper.php in Depicter — Popup & Slider Builder 1.3.2, at vendor/symfony/console/Helper/ProcessHelper.php

149 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * @return Process The process that ran
36 */
37 public function run(OutputInterface $output, $cmd, string $error = null, callable $callback = null, int $verbosity = OutputInterface::VERBOSITY_VERY_VERBOSE): Process
38 {
39 if (!class_exists(Process::class)) {
40 throw new \LogicException('The ProcessHelper cannot be run as the Process component is not installed. Try running "compose require symfony/process".');
41 }
42
43 if ($output instanceof ConsoleOutputInterface) {
44 $output = $output->getErrorOutput();
45 }
46
47 $formatter = $this->getHelperSet()->get('debug_formatter');
48
49 if ($cmd instanceof Process) {
50 $cmd = [$cmd];
51 }
52
53 if (!\is_array($cmd)) {
54 throw new \TypeError(sprintf('The "command" argument of "%s()" must be an array or a "%s" instance, "%s" given.', __METHOD__, Process::class, get_debug_type($cmd)));
55 }
56
57 if (\is_string($cmd[0] ?? null)) {
58 $process = new Process($cmd);
59 $cmd = [];
60 } elseif (($cmd[0] ?? null) instanceof Process) {
61 $process = $cmd[0];
62 unset($cmd[0]);
63 } else {
64 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__));
65 }
66
67 if ($verbosity <= $output->getVerbosity()) {
68 $output->write($formatter->start(spl_object_hash($process), $this->escapeString($process->getCommandLine())));
69 }
70
71 if ($output->isDebug()) {
72 $callback = $this->wrapCallback($output, $process, $callback);
73 }
74
75 $process->run($callback, $cmd);
76
77 if ($verbosity <= $output->getVerbosity()) {
78 $message = $process->isSuccessful() ? 'Command ran successfully' : sprintf('%s Command did not run successfully', $process->getExitCode());
79 $output->write($formatter->stop(spl_object_hash($process), $message, $process->isSuccessful()));
80 }
81
82 if (!$process->isSuccessful() && null !== $error) {
83 $output->writeln(sprintf('<error>%s</error>', $this->escapeString($error)));
84 }
85
86 return $process;
87 }
88
89 /**
90 * Runs the process.
91 *
92 * This is identical to run() except that an exception is thrown if the process
93 * exits with a non-zero exit code.
94 *
95 * @param string|Process $cmd An instance of Process or a command to run
96 * @param callable|null $callback A PHP callback to run whenever there is some
97 * output available on STDOUT or STDERR
98 *
99 * @return Process The process that ran
100 *
101 * @throws ProcessFailedException
102 *
103 * @see run()
104 */
105 public function mustRun(OutputInterface $output, $cmd, string $error = null, callable $callback = null): Process
106 {
107 $process = $this->run($output, $cmd, $error, $callback);
108
109 if (!$process->isSuccessful()) {
110 throw new ProcessFailedException($process);
111 }
112
113 return $process;
114 }
115
116 /**
117 * Wraps a Process callback to add debugging output.
118 */
119 public function wrapCallback(OutputInterface $output, Process $process, callable $callback = null): callable
120 {
121 if ($output instanceof ConsoleOutputInterface) {
122 $output = $output->getErrorOutput();
123 }
124
125 $formatter = $this->getHelperSet()->get('debug_formatter');
126
127 return function ($type, $buffer) use ($output, $process, $callback, $formatter) {
128 $output->write($formatter->progress(spl_object_hash($process), $this->escapeString($buffer), Process::ERR === $type));
129
130 if (null !== $callback) {
131 $callback($type, $buffer);
132 }
133 };
134 }
135
136 private function escapeString(string $str): string
137 {
138 return str_replace('<', '\\<', $str);
139 }
140
141 /**
142 * {@inheritdoc}
143 */
144 public function getName(): string
145 {
146 return 'process';
147 }
148 }
149