1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Stats; |
4
|
|
|
|
5
|
|
|
use Joomla\Application\Cli\ColorStyle; |
6
|
|
|
use Joomla\Application\Cli\Output\Processor\ColorProcessor; |
7
|
|
|
use Joomla\Controller\AbstractController; |
8
|
|
|
use Joomla\DI\ContainerAwareInterface; |
9
|
|
|
use Joomla\DI\ContainerAwareTrait; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* CLI Console |
13
|
|
|
* |
14
|
|
|
* @since 1.0 |
15
|
|
|
*/ |
16
|
|
|
class Console implements ContainerAwareInterface |
17
|
|
|
{ |
18
|
|
|
use ContainerAwareTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Array of available command objects |
22
|
|
|
* |
23
|
|
|
* @var CommandInterface[] |
24
|
|
|
* @since 1.0 |
25
|
|
|
*/ |
26
|
|
|
private $commands = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Get the available commands. |
30
|
|
|
* |
31
|
|
|
* @return CommandInterface[] |
32
|
|
|
* |
33
|
|
|
* @since 1.0 |
34
|
|
|
* @throws \RuntimeException |
35
|
|
|
*/ |
36
|
|
|
public function getCommands() |
37
|
|
|
{ |
38
|
|
|
if (empty($this->commands)) |
39
|
|
|
{ |
40
|
|
|
$this->commands = $this->loadCommands(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $this->commands; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Load the application's commands |
48
|
|
|
* |
49
|
|
|
* @return CommandInterface[] |
50
|
|
|
* |
51
|
|
|
* @since 1.0 |
52
|
|
|
*/ |
53
|
|
|
private function loadCommands() |
54
|
|
|
{ |
55
|
|
|
$commands = []; |
56
|
|
|
|
57
|
|
|
/** @var \DirectoryIterator $fileInfo */ |
58
|
|
|
foreach (new \DirectoryIterator(__DIR__ . '/Commands') as $fileInfo) |
59
|
|
|
{ |
60
|
|
|
if ($fileInfo->isDot()) |
61
|
|
|
{ |
62
|
|
|
continue; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ($fileInfo->isDir()) |
66
|
|
|
{ |
67
|
|
|
$namespace = $fileInfo->getFilename(); |
68
|
|
|
|
69
|
|
|
/** @var \DirectoryIterator $subFileInfo */ |
70
|
|
|
foreach (new \DirectoryIterator($fileInfo->getPathname()) as $subFileInfo) |
71
|
|
|
{ |
72
|
|
|
if ($subFileInfo->isDot() || !$subFileInfo->isFile()) |
73
|
|
|
{ |
74
|
|
|
continue; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$command = $subFileInfo->getBasename('.php'); |
78
|
|
|
$className = __NAMESPACE__ . "\\Commands\\$namespace\\$command"; |
79
|
|
|
|
80
|
|
|
if (!class_exists($className)) |
81
|
|
|
{ |
82
|
|
|
throw new \RuntimeException(sprintf('Required class "%s" not found.', $className)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$commands[strtolower("$namespace:" . str_replace('Command', '', $command))] = $this->getContainer()->get($className); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
else |
89
|
|
|
{ |
90
|
|
|
$command = $fileInfo->getBasename('.php'); |
91
|
|
|
$className = __NAMESPACE__ . "\\Commands\\$command"; |
92
|
|
|
|
93
|
|
|
if (!class_exists($className)) |
94
|
|
|
{ |
95
|
|
|
throw new \RuntimeException(sprintf('Required class "%s" not found.', $className)); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$commands[strtolower(str_replace('Command', '', $command))] = $this->getContainer()->get($className); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $commands; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|