Test Failed
Push — stable ( 03ee42...7388a2 )
by Nuno
01:54
created

LoadCommands::bootstrap()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 18
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 32
rs 8.439
1
<?php
2
3
namespace LaravelZero\Framework\Bootstrappers;
4
5
use Illuminate\Console\Scheduling;
6
use Illuminate\Contracts\Config\Repository;
7
use LaravelZero\Framework\Commands;
8
9
/**
10
 * This is the Laravel Zero Framework Bootstrapper Configuration class.
11
 *
12
 * Configures the console application.
13
 *
14
 * Takes in consideration the app name and the app version. Also
15
 * adds all the application commands.
16
 *
17
 * @author Nuno Maduro <[email protected]>
18
 */
19
class LoadCommands extends Bootstrapper
20
{
21
    /**
22
     * The application's core commands.
23
     *
24
     * @var string[]
25
     */
26
    protected $commands = [
27
        Scheduling\ScheduleRunCommand::class,
28
        Scheduling\ScheduleFinishCommand::class,
29
    ];
30
31
    /**
32
     * The application's development commands.
33
     *
34
     * @var string[]
35
     */
36
    protected $developmentCommands = [
37
        Commands\App\Builder::class,
38
        Commands\App\Renamer::class,
39
        Commands\Component\Installer::class,
40
    ];
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function bootstrap(): void
46
    {
47
        $config = $this->container->make('config');
48
49
        $commands = collect(
50
            array_merge(
51
                $config->get('app.commands') ?: [],
52
                $this->commands,
53
                $this->getDetectedCommands($config)
54
            )
55
        );
56
57
        if (! $config->get('app.production')) {
58
            $commands = $commands->merge($this->developmentCommands);
59
        }
60
61
        $commands->push($config->get('app.default-command'))
62
            ->unique()
63
            ->each(
64
                function ($command) {
65
                    if ($command) {
66
                        $commandInstance = $this->container->make($command);
67
68
                        if ($commandInstance instanceof Commands\Command) {
69
                            $this->container->call([$commandInstance, 'schedule']);
70
                        }
71
72
                        $this->application->add($commandInstance);
73
                    }
74
                }
75
            );
76
    }
77
78
    /**
79
     * Returns the detected commands.
80
     *
81
     * @param \Illuminate\Contracts\Config\Repository $config
82
     *
83
     * @return array
84
     */
85
    protected function getDetectedCommands(Repository $config): array
86
    {
87
        $commands = [];
88
89
        $namespaces = $config->get('commands-namespaces') ?: ["App\Commands"];
90
91
        foreach ($namespaces as $namespace) {
92
            $parts = explode("\\", $namespace);
93
            $path = base_path(implode(DIRECTORY_SEPARATOR, $parts));
94
95
            foreach (glob("$path/*.php") as $commandFile) {
96
                $commandClass = pathinfo($commandFile)['filename'];
97
                $commands[] = $namespace."\\".$commandClass;
98
            }
99
        }
100
101
        return $commands;
102
    }
103
}
104