Completed
Push — master ( 499323...47affb )
by Arman
14s queued 10s
created

CommandDiscovery::discover()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 15
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 28
rs 9.4555
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.9.9
13
 */
14
15
namespace Quantum\Console;
16
17
use ReflectionException;
18
use ReflectionClass;
19
20
/**
21
 * Class CommandDiscovery
22
 * @package Quantum\Console
23
 */
24
class CommandDiscovery
25
{
26
27
    /**
28
     * @param string $directory
29
     * @param string $namespace
30
     * @return array
31
     * @throws ReflectionException
32
     */
33
    public static function discover(string $directory, string $namespace): array
34
    {
35
        $commands = [];
36
37
        foreach (get_directory_classes($directory) as $className) {
38
            $commandClass = $namespace . $className;
39
40
            if (!class_exists($commandClass)) {
41
                continue;
42
            }
43
44
            $commandReflection = new ReflectionClass($commandClass);
45
46
            if (!$commandReflection->isInstantiable() || !$commandReflection->isSubclassOf(QtCommand::class)) {
47
                continue;
48
            }
49
50
            $instance = $commandReflection->newInstance();
51
52
            $commands[] = [
53
                'class' => $commandClass,
54
                'name' => $instance->getName(),
55
                'description' => $instance->getDescription(),
56
                'help' => $instance->getHelp(),
57
            ];
58
        }
59
60
        return $commands;
61
    }
62
}