scratch – Blame information for rev 122
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
120 | office | 1 | <?php |
2 | |||
3 | /* |
||
4 | * This file is part of Alchemy\BinaryDriver. |
||
5 | * |
||
6 | * (c) Alchemy <info@alchemy.fr> |
||
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 Alchemy\BinaryDriver; |
||
13 | |||
14 | use Alchemy\BinaryDriver\Exception\ExecutionFailureException; |
||
15 | use Psr\Log\LoggerInterface; |
||
16 | use SplObjectStorage; |
||
17 | use Symfony\Component\Process\Exception\RuntimeException; |
||
18 | use Symfony\Component\Process\Process; |
||
19 | |||
20 | class ProcessRunner implements ProcessRunnerInterface |
||
21 | { |
||
22 | /** @var LoggerInterface */ |
||
23 | private $logger; |
||
24 | |||
25 | /** @var string */ |
||
26 | private $name; |
||
27 | |||
28 | public function __construct(LoggerInterface $logger, $name) |
||
29 | { |
||
30 | $this->logger = $logger; |
||
31 | $this->name = $name; |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * {@inheritdoc} |
||
36 | * |
||
37 | * @return ProcessRunner |
||
38 | */ |
||
39 | public function setLogger(LoggerInterface $logger) |
||
40 | { |
||
41 | $this->logger = $logger; |
||
42 | |||
43 | return $this; |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * @return LoggerInterface |
||
48 | */ |
||
49 | public function getLogger() |
||
50 | { |
||
51 | return $this->logger; |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * {@inheritdoc} |
||
56 | */ |
||
57 | public function run(Process $process, SplObjectStorage $listeners, $bypassErrors) |
||
58 | { |
||
59 | $this->logger->info(sprintf( |
||
60 | '%s running command %s', $this->name, $process->getCommandLine() |
||
61 | )); |
||
62 | |||
63 | try { |
||
64 | $process->run($this->buildCallback($listeners)); |
||
65 | } catch (RuntimeException $e) { |
||
66 | if (!$bypassErrors) { |
||
67 | $this->doExecutionFailure($process->getCommandLine(), $e); |
||
68 | } |
||
69 | } |
||
70 | |||
71 | if (!$bypassErrors && !$process->isSuccessful()) { |
||
72 | $this->doExecutionFailure($process->getCommandLine()); |
||
73 | } elseif (!$process->isSuccessful()) { |
||
74 | $this->logger->error(sprintf( |
||
75 | '%s failed to execute command %s', $this->name, $process->getCommandLine() |
||
76 | )); |
||
77 | |||
78 | return; |
||
79 | } else { |
||
80 | $this->logger->info(sprintf('%s executed command successfully', $this->name)); |
||
81 | |||
82 | return $process->getOutput(); |
||
83 | } |
||
84 | } |
||
85 | |||
86 | private function buildCallback(SplObjectStorage $listeners) |
||
87 | { |
||
88 | return function ($type, $data) use ($listeners) { |
||
89 | foreach ($listeners as $listener) { |
||
90 | $listener->handle($type, $data); |
||
91 | } |
||
92 | }; |
||
93 | } |
||
94 | |||
95 | private function doExecutionFailure($command, \Exception $e = null) |
||
96 | { |
||
97 | $this->logger->error(sprintf( |
||
98 | '%s failed to execute command %s', $this->name, $command |
||
99 | )); |
||
100 | throw new ExecutionFailureException(sprintf( |
||
101 | '%s failed to execute command %s', $this->name, $command |
||
102 | ), $e ? $e->getCode() : null, $e ?: null); |
||
103 | } |
||
104 | } |