1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright 2018 Aleksander Stelmaczonek <[email protected]> |
4
|
|
|
* @license MIT License, see license file distributed with this source code |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Koriit\PHPDeps; |
8
|
|
|
|
9
|
|
|
use Exception; |
10
|
|
|
use Koriit\PHPDeps\Application\ApplicationInterface; |
11
|
|
|
use Koriit\PHPDeps\Application\Exceptions\ApplicationAlreadyRunning; |
12
|
|
|
use Koriit\PHPDeps\Commands\CheckCommand; |
13
|
|
|
use Koriit\PHPDeps\Commands\DependCommand; |
14
|
|
|
use Koriit\PHPDeps\Commands\DependenciesCommand; |
15
|
|
|
use Koriit\PHPDeps\Commands\ModulesCommand; |
16
|
|
|
use Psr\Container\ContainerExceptionInterface; |
17
|
|
|
use Psr\Container\ContainerInterface; |
18
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
19
|
|
|
use Symfony\Component\Console\Application as ConsoleKernel; |
20
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
21
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
22
|
|
|
|
23
|
|
|
class PHPDepsApplication implements ApplicationInterface |
24
|
|
|
{ |
25
|
|
|
const VERSION = 'v0.1.0'; |
26
|
|
|
|
27
|
|
|
/** @var bool */ |
28
|
|
|
private $running; |
29
|
|
|
|
30
|
|
|
/** @var ContainerInterface */ |
31
|
|
|
private $container; |
32
|
|
|
|
33
|
|
|
/** @var ConsoleKernel */ |
34
|
|
|
private $consoleKernel; |
35
|
|
|
|
36
|
|
|
public function __construct(ContainerInterface $container, ConsoleKernel $consoleKernel) |
37
|
|
|
{ |
38
|
|
|
$this->running = false; |
39
|
|
|
$this->container = $container; |
40
|
|
|
$this->consoleKernel = $consoleKernel; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @throws ContainerExceptionInterface |
45
|
|
|
* @throws Exception |
46
|
|
|
* @throws NotFoundExceptionInterface |
47
|
|
|
*/ |
48
|
|
|
public function run() |
49
|
|
|
{ |
50
|
|
|
if ($this->running) { |
51
|
|
|
throw new ApplicationAlreadyRunning(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$this->running = true; |
55
|
|
|
|
56
|
|
|
$this->initialize(); |
57
|
|
|
$this->loadCommands(); |
58
|
|
|
$exitCode = $this->executeCommand(); |
59
|
|
|
|
60
|
|
|
$this->running = false; |
61
|
|
|
|
62
|
|
|
exit($exitCode > ExitCodes::STATUS_OUT_OF_RANGE ? ExitCodes::STATUS_OUT_OF_RANGE : $exitCode); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getName() |
66
|
|
|
{ |
67
|
|
|
return 'PHPDeps'; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function isRunning() |
71
|
|
|
{ |
72
|
|
|
return $this->running; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return string[] |
77
|
|
|
*/ |
78
|
|
|
public function getCommandsList() |
79
|
|
|
{ |
80
|
|
|
return [ |
81
|
|
|
CheckCommand::class, |
82
|
|
|
DependCommand::class, |
83
|
|
|
DependenciesCommand::class, |
84
|
|
|
ModulesCommand::class, |
85
|
|
|
]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
public function getDefaultCommand() |
92
|
|
|
{ |
93
|
|
|
return CheckCommand::class; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
protected function initialize() |
97
|
|
|
{ |
98
|
|
|
$this->consoleKernel->setName($this->getName()); |
99
|
|
|
$this->consoleKernel->setVersion(self::VERSION); |
100
|
|
|
$this->consoleKernel->setAutoExit(false); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @throws ContainerExceptionInterface |
105
|
|
|
* @throws NotFoundExceptionInterface |
106
|
|
|
*/ |
107
|
|
|
protected function loadCommands() |
108
|
|
|
{ |
109
|
|
|
foreach ($this->getCommandsList() as $command) { |
110
|
|
|
$this->consoleKernel->add($this->container->get($command)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$this->consoleKernel->setDefaultCommand($this->container->get($this->getDefaultCommand())->getName()); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @throws ContainerExceptionInterface |
118
|
|
|
* @throws NotFoundExceptionInterface |
119
|
|
|
* @throws Exception |
120
|
|
|
* |
121
|
|
|
* @return int Exit code |
122
|
|
|
*/ |
123
|
|
|
protected function executeCommand() |
124
|
|
|
{ |
125
|
|
|
return $this->consoleKernel->run( |
126
|
|
|
$this->container->get(InputInterface::class), |
127
|
|
|
$this->container->get(OutputInterface::class) |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exit
expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.