|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of byrokrat\giroapp. |
|
4
|
|
|
* |
|
5
|
|
|
* byrokrat\giroapp is free software: you can redistribute it and/or |
|
6
|
|
|
* modify it under the terms of the GNU General Public License as published |
|
7
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
|
8
|
|
|
* (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* byrokrat\giroapp is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU General Public License |
|
16
|
|
|
* along with byrokrat\giroapp. If not, see <http://www.gnu.org/licenses/>. |
|
17
|
|
|
* |
|
18
|
|
|
* Copyright 2016-17 Hannes Forsgård |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
declare(strict_types = 1); |
|
22
|
|
|
|
|
23
|
|
|
namespace byrokrat\giroapp\Console; |
|
24
|
|
|
|
|
25
|
|
|
use byrokrat\giroapp\Events; |
|
26
|
|
|
use byrokrat\giroapp\Event\LogEvent; |
|
27
|
|
|
use byrokrat\giroapp\Listener\ExitStatusListener; |
|
28
|
|
|
use Symfony\Component\DependencyInjection\Container; |
|
29
|
|
|
use Symfony\Component\Console\Command\Command; |
|
30
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
31
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
32
|
|
|
use Symfony\Component\Console\Output\NullOutput; |
|
33
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Wrapper of giroapp console commands |
|
37
|
|
|
*/ |
|
38
|
|
|
class CommandWrapper extends Command |
|
39
|
|
|
{ |
|
40
|
|
|
/** |
|
41
|
|
|
* @var Container |
|
42
|
|
|
*/ |
|
43
|
|
|
private static $container; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var string Name of command class |
|
47
|
|
|
*/ |
|
48
|
|
|
private $commandClass; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var bool |
|
52
|
|
|
*/ |
|
53
|
|
|
private $discardOutputMessages = false; |
|
54
|
|
|
|
|
55
|
|
|
public function __construct(string $commandClass) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->commandClass = $commandClass; |
|
58
|
|
|
parent::__construct(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public static function setContainer(Container $container): void |
|
62
|
|
|
{ |
|
63
|
|
|
self::$container = $container; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Instruct wrapper to ignore messages written to standard out |
|
68
|
|
|
*/ |
|
69
|
|
|
public function discardOutputMessages(): void |
|
70
|
|
|
{ |
|
71
|
|
|
$this->discardOutputMessages = true; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
protected function configure(): void |
|
75
|
|
|
{ |
|
76
|
|
|
$this->commandClass::configure($this); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
|
80
|
|
|
{ |
|
81
|
|
|
if ($this->discardOutputMessages) { |
|
82
|
|
|
self::$container->set('std_out', new NullOutput); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** @var EventDispatcherInterface $dispatcher */ |
|
86
|
|
|
$dispatcher = self::$container->get(EventDispatcherInterface::CLASS); |
|
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
/** @var ExitStatusListener $exitStatusListener */ |
|
89
|
|
|
$exitStatusListener = self::$container->get(ExitStatusListener::CLASS); |
|
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
/** @var CommandInterface $command */ |
|
92
|
|
|
$command = self::$container->get($this->commandClass); |
|
93
|
|
|
|
|
94
|
|
|
try { |
|
95
|
|
|
$dispatcher->dispatch( |
|
96
|
|
|
Events::EXECUTION_STARTED, |
|
97
|
|
|
new LogEvent(sprintf( |
|
98
|
|
|
'Execution started using: <info>%s</info>', |
|
99
|
|
|
self::$container->getParameter('fs.user_dir') |
|
100
|
|
|
)) |
|
101
|
|
|
); |
|
102
|
|
|
|
|
103
|
|
|
$command->execute($input, $output); |
|
104
|
|
|
|
|
105
|
|
|
$dispatcher->dispatch( |
|
106
|
|
|
Events::EXECUTION_STOPED, |
|
107
|
|
|
new LogEvent('Execution successful') |
|
108
|
|
|
); |
|
109
|
|
|
} catch (\Exception $e) { |
|
110
|
|
|
$dispatcher->dispatch( |
|
111
|
|
|
Events::ERROR, |
|
112
|
|
|
new LogEvent( |
|
113
|
|
|
"[EXCEPTION] {$e->getMessage()}", |
|
114
|
|
|
[ |
|
115
|
|
|
'class' => get_class($e), |
|
116
|
|
|
'file' => $e->getFile(), |
|
117
|
|
|
'line' => $e->getLine() |
|
118
|
|
|
] |
|
119
|
|
|
) |
|
120
|
|
|
); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return $exitStatusListener->getExitStatus(); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|