1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Joomla! Statistics Server |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (C) 2013 - 2017 Open Source Matters, Inc. All rights reserved. |
6
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License Version 2 or Later |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Joomla\StatsServer; |
10
|
|
|
|
11
|
|
|
use Joomla\DI\{ |
12
|
|
|
ContainerAwareInterface, ContainerAwareTrait |
13
|
|
|
}; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* CLI Console |
17
|
|
|
*/ |
18
|
|
|
class Console implements ContainerAwareInterface |
19
|
|
|
{ |
20
|
|
|
use ContainerAwareTrait; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Array of available command objects |
24
|
|
|
* |
25
|
|
|
* @var CommandInterface[] |
26
|
|
|
*/ |
27
|
|
|
private $commands = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Get the available commands. |
31
|
|
|
* |
32
|
|
|
* @return CommandInterface[] |
33
|
|
|
* |
34
|
|
|
* @throws \RuntimeException |
35
|
|
|
*/ |
36
|
|
|
public function getCommands() : array |
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
|
|
|
private function loadCommands() : array |
52
|
|
|
{ |
53
|
|
|
$commands = []; |
54
|
|
|
|
55
|
|
|
/** @var \DirectoryIterator $fileInfo */ |
56
|
|
|
foreach (new \DirectoryIterator(__DIR__ . '/Commands') as $fileInfo) |
57
|
|
|
{ |
58
|
|
|
if ($fileInfo->isDot()) |
59
|
|
|
{ |
60
|
|
|
continue; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if ($fileInfo->isDir()) |
64
|
|
|
{ |
65
|
|
|
$namespace = $fileInfo->getFilename(); |
66
|
|
|
|
67
|
|
|
/** @var \DirectoryIterator $subFileInfo */ |
68
|
|
|
foreach (new \DirectoryIterator($fileInfo->getPathname()) as $subFileInfo) |
69
|
|
|
{ |
70
|
|
|
if ($subFileInfo->isDot() || !$subFileInfo->isFile()) |
71
|
|
|
{ |
72
|
|
|
continue; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$command = $subFileInfo->getBasename('.php'); |
76
|
|
|
$className = __NAMESPACE__ . "\\Commands\\$namespace\\$command"; |
77
|
|
|
|
78
|
|
|
if (!class_exists($className)) |
79
|
|
|
{ |
80
|
|
|
throw new \RuntimeException(sprintf('Required class "%s" not found.', $className)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// If the class isn't instantiable, it isn't a valid command |
84
|
|
View Code Duplication |
if ((new \ReflectionClass($className))->isInstantiable()) |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
$commands[strtolower("$namespace:" . str_replace('Command', '', $command))] = $this->getContainer()->get($className); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
else |
91
|
|
|
{ |
92
|
|
|
$command = $fileInfo->getBasename('.php'); |
93
|
|
|
$className = __NAMESPACE__ . "\\Commands\\$command"; |
94
|
|
|
|
95
|
|
|
if (!class_exists($className)) |
96
|
|
|
{ |
97
|
|
|
throw new \RuntimeException(sprintf('Required class "%s" not found.', $className)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// If the class isn't instantiable, it isn't a valid command |
101
|
|
View Code Duplication |
if ((new \ReflectionClass($className))->isInstantiable()) |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
$commands[strtolower(str_replace('Command', '', $command))] = $this->getContainer()->get($className); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return $commands; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.