Passed
Branch develop (0af400)
by Nuno
02:22
created

Kernel::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 3
cts 4
cp 0.75
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2.0625
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 function define;
17
use ReflectionClass;
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 29
    public function __construct(
62
        \Illuminate\Contracts\Foundation\Application $app,
63
        \Illuminate\Contracts\Events\Dispatcher $events
64
    ) {
65 29
        if (! defined('ARTISAN_BINARY')) {
66
            define('ARTISAN_BINARY', basename($_SERVER['SCRIPT_FILENAME']));
67
        }
68
69 29
        parent::__construct($app, $events);
70 29
    }
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 29
    public function bootstrap(): void
86
    {
87 29
        parent::bootstrap();
88
89 29
        if ($commandClass = $this->app['config']->get('commands.default')) {
90 29
            $this->getArtisan()
91 29
                ->setDefaultCommand(
92 29
                    $this->app[$commandClass]->getName()
93
                );
94
        }
95 29
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 29
    protected function commands(): void
101
    {
102 29
        $config = $this->app['config'];
103
104
        /*
105
         * Loads commands paths.
106
         */
107 29
        $this->load($config->get('commands.paths', $this->app->path('Commands')));
108
109
        /**
110
         * Loads configurated commands.
111
         */
112 29
        $commands = collect($config->get('commands.add', []))->merge($config->get('commands.hidden', []));
113
114 29
        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 29
            $commands->push($config->get('commands.default'));
116
        }
117
118
        /*
119
         * Loads development commands.
120
         */
121 29
        if ($this->app->environment() !== 'production') {
122 28
            $commands = $commands->merge($this->developmentCommands);
123
        }
124
125 29
        $commands = $commands->diff($toRemoveCommands = $config->get('commands.remove', []));
126
127 29
        Artisan::starting(
128
            function ($artisan) use ($toRemoveCommands) {
129 29
                $reflectionClass = new ReflectionClass(Artisan::class);
130 29
                $commands = collect($artisan->all())
131 29
                    ->filter(
132
                        function ($command) use ($toRemoveCommands) {
133 29
                            return ! in_array(get_class($command), $toRemoveCommands, true);
134 29
                        }
135
                    )
136 29
                    ->toArray();
137
138 29
                $property = $reflectionClass->getParentClass()
139 29
                    ->getProperty('commands');
140
141 29
                $property->setAccessible(true);
142 29
                $property->setValue($artisan, $commands);
143 29
                $property->setAccessible(false);
144 29
            }
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 29
        Artisan::starting(
153
            function ($artisan) use ($commands) {
154 29
                $artisan->resolveCommands($commands->toArray());
155 29
            }
156
        );
157
158 29
        Artisan::starting(
159
            function ($artisan) use ($config) {
160 29
                collect($artisan->all())->each(
161
                    function ($command) use ($config, $artisan) {
162 29
                        if (in_array(get_class($command), $config->get('commands.hidden', []), true)) {
163 29
                            $command->setHidden(true);
164
                        }
165
                        
166 29
                        $command->setApplication($artisan);
167
168 29
                        if ($command instanceof Commands\Command) {
169 29
                            $this->app->call([$command, 'schedule']);
170
                        }
171 29
                    }
172
                );
173 29
            }
174
        );
175 29
    }
176
177
    /**
178
     * Gets the application name.
179
     *
180
     * @return string
181
     */
182 1
    public function getName(): string
183
    {
184 1
        return $this->getArtisan()
185 1
            ->getName();
186
    }
187
}
188