|
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
|
|
|
} |