Test Failed
Push — stable ( 4f7ebd...0c390a )
by Nuno
01:59
created

LoadCommands::getDetectedCommands()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 21
nc 2
nop 1
dl 0
loc 33
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace LaravelZero\Framework\Bootstrappers;
4
5
use ReflectionClass;
6
use Illuminate\Support\Str;
7
use LaravelZero\Framework\Commands;
8
use Symfony\Component\Finder\Finder;
9
use Illuminate\Contracts\Config\Repository;
10
use LaravelZero\Framework\Commands\Command;
11
12
/**
13
 * This is the Laravel Zero Framework Bootstrapper Configuration class.
14
 *
15
 * Configures the console application.
16
 *
17
 * Takes in consideration the app name and the app version. Also
18
 * adds all the application commands.
19
 *
20
 * @author Nuno Maduro <[email protected]>
21
 */
22
class LoadCommands extends Bootstrapper
23
{
24
    /**
25
     * The application's development commands.
26
     *
27
     * @var string[]
28
     */
29
    protected $developmentCommands = [
30
        Commands\App\Builder::class,
31
        Commands\App\Renamer::class,
32
        Commands\App\CommandMaker::class,
33
        Commands\Component\Illuminate\Database\Installer::class,
34
    ];
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function bootstrap(): void
40
    {
41
        $config = $this->container->make('config');
42
43
        $commands = collect(
44
            array_merge(
45
                $config->get('app.commands') ?: [],
46
                $this->getDetectedCommands($config)
47
            )
48
        );
49
50
        if (! $config->get('app.production')) {
51
            $commands = $commands->merge($this->developmentCommands);
52
        }
53
54
        $commands->push($config->get('app.default-command'))
55
            ->unique()
56
            ->each(
57
                function ($command) {
58
                    if ($command) {
59
                        $commandInstance = $this->container->make($command);
60
61
                        if ($commandInstance instanceof Commands\Command) {
62
                            $this->container->call([$commandInstance, 'schedule']);
63
                        }
64
65
                        $this->application->add($commandInstance);
66
                    }
67
                }
68
            );
69
    }
70
71
    /**
72
     * Returns the detected commands.
73
     *
74
     * @param \Illuminate\Contracts\Config\Repository $config
75
     *
76
     * @return array
77
     */
78
    protected function getDetectedCommands(Repository $config): array
79
    {
80
        $commands = [];
81
82
        $paths = collect($config->get('app.commands-paths', ["Commands"]))
83
            ->filter(
84
                function ($path) {
85
                    return file_exists(base_path('app'.DIRECTORY_SEPARATOR.$path));
86
                }
87
            )
88
            ->map(
89
                function ($path) {
90
                    return base_path('app'.DIRECTORY_SEPARATOR.$path);
91
                }
92
            )->toArray();
93
94
        $namespace = app()->getNamespace().'\\';
95
        if (! empty($paths)) {
96
            foreach ((new Finder)->in($paths)
97
                         ->files() as $command) {
98
                $command = $namespace.str_replace(
99
                        ['/', '.php'],
100
                        ['\\', ''],
101
                        Str::after($command->getPathname(), base_path('app').DIRECTORY_SEPARATOR)
102
                    );
103
                if (is_subclass_of($command, Command::class) && ! (new ReflectionClass($command))->isAbstract()) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \LaravelZero\Framework\Commands\Command::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
104
                    $commands[] = $command;
105
                }
106
            }
107
        }
108
109
        return $commands;
110
    }
111
}
112