CommandLoader   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 0
loc 65
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 2
C load() 0 32 8
A addCommandDir() 0 4 1
A addCommand() 0 4 1
1
<?php
2
3
namespace Magium\Cli;
4
5
use Magium\NotFoundException;
6
use Symfony\Component\Console\Command\Command;
7
8
class CommandLoader
9
{
10
    protected $application;
11
    protected $config;
12
13
    protected static $dirs = [];
14
    protected static $commands = [];
15
16
    public function __construct(
17
        \Symfony\Component\Console\Application $application,
18
        $configDir
19
    )
20
    {
21
        if (!is_dir($configDir)) {
22
            throw new NotFoundException('Configuration dir not found: ' . $configDir);
23
        }
24
        $this->config = $configDir;
25
26
        $this->application = $application;
27
    }
28
29
    public function load()
30
    {
31
        self::addCommandDir('Magium\Cli\Command', __DIR__ . '/Command');
32
33
        foreach (self::$dirs as $namespace => $dir) {
34
            $files = glob($dir . '/*.php');
35
36
            foreach ($files as $file) {
37
                $class = substr(basename($file), 0, -4);
38
                $className = $namespace . '\\' . $class;
39
                if (class_exists($className)) {
40
                    $reflection = new \ReflectionClass($className);
41
                    if (!$reflection->isInstantiable()) {
42
                        continue;
43
                    }
44
                    $object = new $className();
45
                    if ($object instanceof ConfigurationPathInterface) {
46
                        $object->setPath($this->config);
47
                    }
48
                    if ($object instanceof Command) {
49
                        self::addCommand($object);
50
                    }
51
                }
52
            }
53
54
        }
55
56
57
        foreach (self::$commands as $command) {
58
            $this->application->add($command);
59
        }
60
    }
61
62
    public static function addCommandDir($namespacePrefix, $dir)
63
    {
64
        self::$dirs[$namespacePrefix] = $dir;
65
    }
66
67
    public static function addCommand(Command $command)
68
    {
69
        self::$commands[] = $command;
70
    }
71
72
}