-
-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathKernel.php
277 lines (234 loc) · 7.85 KB
/
Kernel.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
declare(strict_types=1);
/**
* This file is part of Laravel Zero.
*
* (c) Nuno Maduro <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace LaravelZero\Framework;
use Illuminate\Console\Application as Artisan;
use Illuminate\Foundation\Console\ConfigPublishCommand;
use Illuminate\Foundation\Console\Kernel as BaseKernel;
use LaravelZero\Framework\Providers\CommandRecorder\CommandRecorderRepository;
use NunoMaduro\Collision\Adapters\Laravel\Commands\TestCommand;
use ReflectionClass;
use Symfony\Component\Console\Exception\CommandNotFoundException;
use Symfony\Component\Console\Output\OutputInterface;
use function collect;
use function define;
use function defined;
use function get_class;
use function in_array;
class Kernel extends BaseKernel
{
/**
* The application's development commands.
*
* @var string[]
*/
protected $developmentCommands = [
Commands\BuildCommand::class,
Commands\RenameCommand::class,
Commands\MakeCommand::class,
Commands\InstallCommand::class,
Commands\StubPublishCommand::class,
Commands\TestMakeCommand::class,
// ...
ConfigPublishCommand::class,
];
/**
* Commands that should be removed in production.
*
* @var string[]
*/
protected $developmentOnlyCommands = [
TestCommand::class,
];
/**
* The application's default hidden commands.
*
* @var string[]
*/
protected $hiddenCommands = [
ConfigPublishCommand::class,
];
/**
* The application's bootstrap classes.
*
* @var string[]
*/
protected $bootstrappers = [
\LaravelZero\Framework\Bootstrap\CoreBindings::class,
\LaravelZero\Framework\Bootstrap\LoadEnvironmentVariables::class,
\LaravelZero\Framework\Bootstrap\LoadConfiguration::class,
\Illuminate\Foundation\Bootstrap\HandleExceptions::class,
\LaravelZero\Framework\Bootstrap\RegisterFacades::class,
\LaravelZero\Framework\Bootstrap\RegisterProviders::class,
\Illuminate\Foundation\Bootstrap\BootProviders::class,
];
/**
* Kernel constructor.
*/
public function __construct(
\Illuminate\Contracts\Foundation\Application $app,
\Illuminate\Contracts\Events\Dispatcher $events
) {
if (! defined('ARTISAN_BINARY')) {
define('ARTISAN_BINARY', basename($_SERVER['SCRIPT_FILENAME']));
}
parent::__construct($app, $events);
}
/**
* {@inheritdoc}
*/
public function handle($input, $output = null)
{
$this->app->instance(OutputInterface::class, $output);
if (function_exists('Termwind\renderUsing') && $output) {
\Termwind\renderUsing($output);
}
$this->ensureDefaultCommand($input);
return parent::handle($input, $output);
}
/**
* With Symfony's default commands, you cannot pass any argument
* or option because they are ignored. When the first argument
* does not match any commands, this method ensures given
* arguments/options are proxied to the default command.
*/
protected function ensureDefaultCommand($input): void
{
$this->bootstrap();
$application = $this->getArtisan();
try {
/**
* First, we are going to see if the first argument
* matches any command on our console application.
*/
if ($commandName = $input->getFirstArgument()) {
$application->find($commandName);
}
} catch (CommandNotFoundException $e) {
/**
* If none, the given arguments and options should
* be proxied to the application default command.
*/
$application->setDefaultCommand(
resolve(config('commands.default'))->getName(), true
);
}
}
/**
* {@inheritdoc}
*/
public function bootstrap(): void
{
parent::bootstrap();
if ($commandClass = $this->app['config']->get('commands.default')) {
$this->getArtisan()
->setDefaultCommand(
$this->app[$commandClass]->getName()
);
}
}
/**
* {@inheritdoc}
*/
protected function commands(): void
{
$config = $this->app['config'];
/*
* Loads commands paths.
*
* @phpstan-ignore-next-line
*/
$this->load($config->get('commands.paths', $this->app->path('Commands')));
/**
* Loads configured commands.
*/
$commands = collect($config->get('commands.add', []))->merge(
$config->get('commands.hidden', $this->hiddenCommands),
);
if ($command = $config->get('commands.default')) {
$commands->push($command);
}
/*
* Loads development commands.
*/
if ($this->app->environment() !== 'production') {
$commands = $commands->merge($this->developmentCommands);
}
$toRemoveCommands = $config->get('commands.remove', []);
if ($this->app->environment('production')) {
$toRemoveCommands = array_merge($toRemoveCommands, $this->developmentOnlyCommands);
}
$commands = $commands->diff($toRemoveCommands);
Artisan::starting(
function ($artisan) use ($toRemoveCommands) {
$reflectionClass = new ReflectionClass(Artisan::class);
$commands = collect($artisan->all())
->filter(
fn ($command) => ! in_array(get_class($command), $toRemoveCommands, true)
)
->toArray();
$property = $reflectionClass->getParentClass()
->getProperty('commands');
$property->setAccessible(true);
$property->setValue($artisan, $commands);
$property->setAccessible(false);
}
);
/*
* Registers a bootstrap callback on the artisan console application
* in order to call the schedule method on each Laravel Zero
* command class.
*/
Artisan::starting(
function ($artisan) use ($commands) {
$artisan->resolveCommands($commands->toArray());
$artisan->setContainerCommandLoader();
}
);
Artisan::starting(
function ($artisan) use ($config) {
$commands = array_merge(
$config->get('commands.hidden'),
$this->hiddenCommands,
);
collect($artisan->all())->each(
function ($command) use ($artisan, $commands) {
if (in_array(get_class($command), $commands, true)) {
$command->setHidden(true);
}
$command->setApplication($artisan);
if ($command instanceof Commands\Command) {
$this->app->call([$command, 'schedule']);
}
}
);
}
);
}
/**
* Gets the application name.
*/
public function getName(): string
{
return $this->getArtisan()
->getName();
}
/**
* {@inheritdoc}
*/
public function call($command, array $parameters = [], $outputBuffer = null)
{
if (function_exists('Termwind\renderUsing') && $outputBuffer) {
\Termwind\renderUsing($outputBuffer);
}
resolve(CommandRecorderRepository::class)->create($command, $parameters);
return parent::call($command, $parameters, $outputBuffer);
}
}