Test Failed
Pull Request — stable (#303)
by Sander
07:26
created

Kernel::call()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 2
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 LaravelZero\Framework\Providers\CommandRecorder\CommandRecorder;
18
use ReflectionClass;
19
use function collect;
20
use function defined;
21
use function in_array;
22
use function get_class;
23
use Illuminate\Console\Application as Artisan;
24
use Symfony\Component\Console\Output\OutputInterface;
25
use Illuminate\Foundation\Console\Kernel as BaseKernel;
26
27
class Kernel extends BaseKernel
28
{
29
    /**
30
     * The application's development commands.
31
     *
32
     * @var string[]
33
     */
34
    protected $developmentCommands = [
35
        Commands\BuildCommand::class,
36
        Commands\RenameCommand::class,
37
        Commands\MakeCommand::class,
38
        Commands\InstallCommand::class,
39
    ];
40
41
    /**
42
     * The application's bootstrap classes.
43
     *
44
     * @var string[]
45
     */
46
    protected $bootstrappers = [
47
        \LaravelZero\Framework\Bootstrap\CoreBindings::class,
48
        \LaravelZero\Framework\Bootstrap\LoadEnvironmentVariables::class,
49
        \LaravelZero\Framework\Bootstrap\LoadConfiguration::class,
50
        \Illuminate\Foundation\Bootstrap\HandleExceptions::class,
51
        \LaravelZero\Framework\Bootstrap\RegisterFacades::class,
52
        \LaravelZero\Framework\Bootstrap\RegisterProviders::class,
53
        \Illuminate\Foundation\Bootstrap\BootProviders::class,
54
    ];
55
56
    /**
57
     * Kernel constructor.
58 32
     */
59
    public function __construct(
60
        \Illuminate\Contracts\Foundation\Application $app,
61
        \Illuminate\Contracts\Events\Dispatcher $events
62 32
    ) {
63
        if (! defined('ARTISAN_BINARY')) {
64
            define('ARTISAN_BINARY', basename($_SERVER['SCRIPT_FILENAME']));
65
        }
66 32
67 32
        parent::__construct($app, $events);
68
    }
69
70
    /**
71
     * {@inheritdoc}
72 1
     */
73
    public function handle($input, $output = null)
74 1
    {
75
        $this->app->instance(OutputInterface::class, $output);
76 1
77
        return parent::handle($input, $output);
78
    }
79
80
    /**
81
     * {@inheritdoc}
82 32
     */
83
    public function bootstrap(): void
84 32
    {
85
        parent::bootstrap();
86 32
87 32
        if ($commandClass = $this->app['config']->get('commands.default')) {
88 32
            $this->getArtisan()
89 32
                ->setDefaultCommand(
90
                    $this->app[$commandClass]->getName()
91
                );
92 32
        }
93
    }
94
95
    /**
96
     * {@inheritdoc}
97 32
     */
98
    protected function commands(): void
99 32
    {
100
        $config = $this->app['config'];
101
102
        /*
103
         * Loads commands paths.
104 32
         */
105
        $this->load($config->get('commands.paths', $this->app->path('Commands')));
106
107
        /**
108
         * Loads configurated commands.
109 32
         */
110
        $commands = collect($config->get('commands.add', []))->merge($config->get('commands.hidden', []));
111 32
112 32
        if ($command = $config->get('commands.default')) {
113
            $commands->push($command);
114
        }
115
116
        /*
117
         * Loads development commands.
118 32
         */
119 31
        if ($this->app->environment() !== 'production') {
120
            $commands = $commands->merge($this->developmentCommands);
121
        }
122 32
123
        $commands = $commands->diff($toRemoveCommands = $config->get('commands.remove', []));
124 32
125
        Artisan::starting(
126 32
            function ($artisan) use ($toRemoveCommands) {
127 32
                $reflectionClass = new ReflectionClass(Artisan::class);
128 32
                $commands = collect($artisan->all())
129
                    ->filter(
130 32
                        function ($command) use ($toRemoveCommands) {
131 32
                            return ! in_array(get_class($command), $toRemoveCommands, true);
132
                        }
133 32
                    )
134
                    ->toArray();
135 32
136 32
                $property = $reflectionClass->getParentClass()
137
                    ->getProperty('commands');
138 32
139 32
                $property->setAccessible(true);
140 32
                $property->setValue($artisan, $commands);
141 32
                $property->setAccessible(false);
142
            }
143
        );
144
145
        /*
146
         * Registers a bootstrap callback on the artisan console application
147
         * in order to call the schedule method on each Laravel Zero
148
         * command class.
149 32
         */
150
        Artisan::starting(
151 32
            function ($artisan) use ($commands) {
152 32
                $artisan->resolveCommands($commands->toArray());
153
            }
154
        );
155 32
156
        Artisan::starting(
157 32
            function ($artisan) use ($config) {
158
                collect($artisan->all())->each(
159 32
                    function ($command) use ($config, $artisan) {
160 32
                        if (in_array(get_class($command), $config->get('commands.hidden', []), true)) {
161
                            $command->setHidden(true);
162
                        }
163 32
164
                        $command->setApplication($artisan);
165 32
166 32
                        if ($command instanceof Commands\Command) {
167
                            $this->app->call([$command, 'schedule']);
168 32
                        }
169
                    }
170 32
                );
171
            }
172 32
        );
173
    }
174
175
    /**
176
     * Gets the application name.
177 1
     */
178
    public function getName(): string
179 1
    {
180 1
        return $this->getArtisan()
181
            ->getName();
182
    }
183
184
    /**
185
     * @inheritdoc
186
     */
187
    public function call($command, array $parameters = [], $outputBuffer = null)
188
    {
189
        app(CommandRecorder::class)->record($command, $parameters);
190
191
        return parent::call($command, $parameters, $outputBuffer);
192
    }
193
}
194