Passed
Branch develop (893064)
by Nuno
02:09
created

Kernel   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Test Coverage

Coverage 98.08%

Importance

Changes 0
Metric Value
wmc 11
dl 0
loc 158
ccs 51
cts 52
cp 0.9808
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B commands() 0 68 5
A bootstrap() 0 8 2
A getName() 0 4 1
A __construct() 0 9 2
A handle() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Laravel Zero.
7
 *
8
 * (c) Nuno Maduro <[email protected]>
9
 *
10
 *  For the full copyright and license information, please view the LICENSE
11
 *  file that was distributed with this source code.
12
 */
13
14
namespace LaravelZero\Framework;
15
16
use ReflectionClass;
17
use function define;
18
use function collect;
19
use function defined;
20
use function in_array;
21
use function get_class;
22
use Illuminate\Console\Application as Artisan;
23
use Symfony\Component\Console\Output\OutputInterface;
24
use Illuminate\Foundation\Console\Kernel as BaseKernel;
25
26
class Kernel extends BaseKernel
27
{
28
    /**
29
     * The application's development commands.
30
     *
31
     * @var string[]
32
     */
33
    protected $developmentCommands = [
34
        Commands\BuildCommand::class,
35
        Commands\RenameCommand::class,
36
        Commands\MakeCommand::class,
37
        Commands\InstallCommand::class,
38
    ];
39
40
    /**
41
     * The application's bootstrap classes.
42
     *
43
     * @var string[]
44
     */
45
    protected $bootstrappers = [
46
        \LaravelZero\Framework\Bootstrap\CoreBindings::class,
47
        \LaravelZero\Framework\Bootstrap\LoadEnvironmentVariables::class,
48
        \LaravelZero\Framework\Bootstrap\LoadConfiguration::class,
49
        \Illuminate\Foundation\Bootstrap\HandleExceptions::class,
50
        \Illuminate\Foundation\Bootstrap\RegisterFacades::class,
51
        \LaravelZero\Framework\Bootstrap\RegisterProviders::class,
52
        \Illuminate\Foundation\Bootstrap\BootProviders::class,
53
    ];
54
55
    /**
56
     * Kernel constructor.
57
     *
58
     * @param \Illuminate\Contracts\Foundation\Application $app
59
     * @param \Illuminate\Contracts\Events\Dispatcher $events
60
     */
61 25
    public function __construct(
62
        \Illuminate\Contracts\Foundation\Application $app,
63
        \Illuminate\Contracts\Events\Dispatcher $events
64
    ) {
65 25
        if (! defined('ARTISAN_BINARY')) {
66
            define('ARTISAN_BINARY', basename($_SERVER['SCRIPT_FILENAME']));
67
        }
68
69 25
        parent::__construct($app, $events);
70 25
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75 1
    public function handle($input, $output = null): int
76
    {
77 1
        $this->app->instance(OutputInterface::class, $output);
78
79 1
        return parent::handle($input, $output);
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 25
    public function bootstrap(): void
86
    {
87 25
        parent::bootstrap();
88
89 25
        if ($commandClass = $this->app['config']->get('commands.default')) {
90 25
            $this->getArtisan()
91 25
                ->setDefaultCommand(
92 25
                    $this->app[$commandClass]->getName()
93
                );
94
        }
95 25
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 25
    protected function commands(): void
101
    {
102 25
        $config = $this->app['config'];
103
104
        /*
105
         * Loads commands paths.
106
         */
107 25
        $this->load($config->get('commands.paths', $this->app->path('Commands')));
108
109
        /**
110
         * Loads configurated commands.
111
         */
112 25
        $commands = collect($config->get('commands.add', []))->merge($config->get('commands.hidden', []));
113
114 25
        if ($command = $config->get('commands.default')) {
0 ignored issues
show
Unused Code introduced by
The assignment to $command is dead and can be removed.
Loading history...
115 25
            $commands->push($config->get('commands.default'));
116
        }
117
118
        /*
119
         * Loads development commands.
120
         */
121 25
        if ($this->app->environment() !== 'production') {
122 24
            $commands = $commands->merge($this->developmentCommands);
123
        }
124
125 25
        $commands = $commands->diff($toRemoveCommands = $config->get('commands.remove', []));
126
127 25
        Artisan::starting(
128
            function ($artisan) use ($toRemoveCommands) {
129 25
                $reflectionClass = new ReflectionClass(Artisan::class);
130 25
                $commands = collect($artisan->all())
131 25
                    ->filter(
132
                        function ($command) use ($toRemoveCommands) {
133 25
                            return ! in_array(get_class($command), $toRemoveCommands, true);
134 25
                        }
135
                    )
136 25
                    ->toArray();
137
138 25
                $property = $reflectionClass->getParentClass()
139 25
                    ->getProperty('commands');
140
141 25
                $property->setAccessible(true);
142 25
                $property->setValue($artisan, $commands);
143 25
                $property->setAccessible(false);
144 25
            }
145
        );
146
147
        /*
148
         * Registers a bootstrap callback on the artisan console application
149
         * in order to call the schedule method on each Laravel Zero
150
         * command class.
151
         */
152 25
        Artisan::starting(
153
            function ($artisan) use ($commands) {
154 25
                $artisan->resolveCommands($commands->toArray());
155 25
            }
156
        );
157
158 25
        Artisan::starting(
159
            function ($artisan) use ($config) {
160 25
                collect($artisan->all())->each(
161
                    function ($command) use ($config) {
162 25
                        if (in_array(get_class($command), $config->get('commands.hidden', []), true)) {
163 25
                            $command->setHidden(true);
164
                        }
165
166 25
                        if ($command instanceof Commands\Command) {
167 25
                            $this->app->call([$command, 'schedule']);
168
                        }
169 25
                    }
170
                );
171 25
            }
172
        );
173 25
    }
174
175
    /**
176
     * Gets the application name.
177
     *
178
     * @return string
179
     */
180 1
    public function getName(): string
181
    {
182 1
        return $this->getArtisan()
183 1
            ->getName();
184
    }
185
}
186