1 | <?php |
||
2 | |||
3 | /** |
||
4 | * Platine Framework |
||
5 | * |
||
6 | * Platine Framework is a lightweight, high-performance, simple and elegant |
||
7 | * PHP Web framework |
||
8 | * |
||
9 | * This content is released under the MIT License (MIT) |
||
10 | * |
||
11 | * Copyright (c) 2020 Platine Framework |
||
12 | * |
||
13 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
||
14 | * of this software and associated documentation files (the "Software"), to deal |
||
15 | * in the Software without restriction, including without limitation the rights |
||
16 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||
17 | * copies of the Software, and to permit persons to whom the Software is |
||
18 | * furnished to do so, subject to the following conditions: |
||
19 | * |
||
20 | * The above copyright notice and this permission notice shall be included in all |
||
21 | * copies or substantial portions of the Software. |
||
22 | * |
||
23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||
24 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||
25 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||
26 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||
27 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||
28 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
||
29 | * SOFTWARE. |
||
30 | */ |
||
31 | |||
32 | /** |
||
33 | * @file ConsoleKernel.php |
||
34 | * |
||
35 | * The Console Kernel class |
||
36 | * |
||
37 | * @package Platine\Framework\Kernel |
||
38 | * @author Platine Developers team |
||
39 | * @copyright Copyright (c) 2020 |
||
40 | * @license http://opensource.org/licenses/MIT MIT License |
||
41 | * @link https://www.platine-php.com |
||
42 | * @version 1.0.0 |
||
43 | * @filesource |
||
44 | */ |
||
45 | |||
46 | declare(strict_types=1); |
||
47 | |||
48 | namespace Platine\Framework\Kernel; |
||
49 | |||
50 | use InvalidArgumentException; |
||
51 | use Platine\Config\Config; |
||
52 | use Platine\Console\Application as ConsoleApp; |
||
53 | use Platine\Console\Command\Command; |
||
54 | use Platine\Framework\App\Application; |
||
55 | |||
56 | /** |
||
57 | * @class ConsoleKernel |
||
58 | * @package Platine\Framework\Kernel |
||
59 | * @template T |
||
60 | */ |
||
61 | class ConsoleKernel extends BaseKernel |
||
62 | { |
||
63 | /** |
||
64 | * Console application instance |
||
65 | * @var ConsoleApp |
||
66 | */ |
||
67 | protected ConsoleApp $console; |
||
68 | |||
69 | /** |
||
70 | * The list of command |
||
71 | * @var Command[] |
||
72 | */ |
||
73 | protected array $commands = []; |
||
74 | |||
75 | /** |
||
76 | * Whether the commands already loaded |
||
77 | * @var bool |
||
78 | */ |
||
79 | protected bool $commandsLoaded = false; |
||
80 | |||
81 | /** |
||
82 | * Create new instance |
||
83 | * @param Application $app |
||
84 | * @param ConsoleApp $console |
||
85 | */ |
||
86 | public function __construct(Application $app, ConsoleApp $console) |
||
87 | { |
||
88 | parent::__construct($app); |
||
89 | $this->console = $console; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Run the application |
||
94 | * @param string[] $argv |
||
95 | * @return void |
||
96 | */ |
||
97 | public function run(array $argv): void |
||
98 | { |
||
99 | $this->bootstrap(); |
||
100 | |||
101 | $this->console->handle($argv); |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Bootstrap the application |
||
106 | * @return void |
||
107 | */ |
||
108 | public function bootstrap(): void |
||
109 | { |
||
110 | parent::bootstrap(); |
||
111 | |||
112 | if ($this->commandsLoaded === false) { |
||
113 | $this->registerConfiguredCommands(); |
||
114 | |||
115 | //Load providers commands |
||
116 | /** @var class-string[] $commands */ |
||
117 | $commands = $this->app->getProvidersCommands(); |
||
118 | foreach ($commands as $command) { |
||
119 | $this->addCommand($command); |
||
120 | } |
||
121 | |||
122 | $this->commandsLoaded = true; |
||
123 | } |
||
124 | |||
125 | foreach ($this->commands as $command) { |
||
126 | $this->console->addCommand($command); |
||
127 | } |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Add new command |
||
132 | * @param string|Command $command |
||
133 | * @return void |
||
134 | */ |
||
135 | public function addCommand(string|Command $command): void |
||
136 | { |
||
137 | if (is_string($command)) { |
||
138 | $command = $this->createCommand($command); |
||
139 | } |
||
140 | |||
141 | $this->commands[] = $command; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Return the console application |
||
146 | * @return ConsoleApp |
||
147 | */ |
||
148 | public function getConsoleApp(): ConsoleApp |
||
149 | { |
||
150 | return $this->console; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Load configured commands |
||
155 | * @return void |
||
156 | */ |
||
157 | protected function registerConfiguredCommands(): void |
||
158 | { |
||
159 | /** @var Config<T> $config */ |
||
160 | $config = $this->app->get(Config::class); |
||
161 | |||
162 | /** @var string[] $commands */ |
||
163 | $commands = $config->get('commands', []); |
||
164 | foreach ($commands as $command) { |
||
165 | $this->addCommand($command); |
||
166 | } |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Create new command |
||
171 | * @param string|class-string<Command> $command |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
172 | * @return Command |
||
173 | */ |
||
174 | protected function createCommand(string $command): Command |
||
175 | { |
||
176 | if ($this->app->has($command)) { |
||
177 | return $this->app->get($command); |
||
178 | } |
||
179 | |||
180 | if (class_exists($command)) { |
||
181 | /** @var Command $o */ |
||
182 | $o = new $command(); |
||
183 | return $o; |
||
184 | } |
||
185 | |||
186 | throw new InvalidArgumentException( |
||
187 | sprintf('The command [%s] must be an identifier of container or class', $command) |
||
188 | ); |
||
189 | } |
||
190 | } |
||
191 |