1 | <?php |
||||||
2 | |||||||
3 | namespace Elgg; |
||||||
4 | |||||||
5 | use Elgg\Cli\Application; |
||||||
0 ignored issues
–
show
|
|||||||
6 | use Elgg\Cli\BaseCommand; |
||||||
7 | use Exception; |
||||||
8 | use Symfony\Component\Console\Input\InputInterface; |
||||||
9 | use Symfony\Component\Console\Input\InputOption; |
||||||
10 | use Symfony\Component\Console\Output\OutputInterface; |
||||||
11 | |||||||
12 | /** |
||||||
13 | * CLI bootstrap |
||||||
14 | */ |
||||||
15 | class Cli { |
||||||
16 | |||||||
17 | use Loggable; |
||||||
18 | |||||||
19 | /** |
||||||
20 | * @var Application |
||||||
21 | */ |
||||||
22 | protected $console; |
||||||
23 | |||||||
24 | /** |
||||||
25 | * @var HooksRegistrationService |
||||||
26 | */ |
||||||
27 | protected $hooks; |
||||||
28 | |||||||
29 | /** |
||||||
30 | * @var InputInterface |
||||||
31 | */ |
||||||
32 | protected $input; |
||||||
33 | |||||||
34 | /** |
||||||
35 | * @var OutputInterface |
||||||
36 | */ |
||||||
37 | protected $output; |
||||||
38 | |||||||
39 | /** |
||||||
40 | * Constructor |
||||||
41 | * |
||||||
42 | * @param Application $console Console application instance |
||||||
43 | * @param PluginHooksService $hooks Hooks registration service |
||||||
44 | * @param InputInterface $input Console input |
||||||
45 | * @param OutputInterface $output Console output |
||||||
46 | */ |
||||||
47 | 1 | public function __construct( |
|||||
48 | Application $console, |
||||||
49 | PluginHooksService $hooks, |
||||||
50 | InputInterface $input, |
||||||
51 | OutputInterface $output |
||||||
52 | ) { |
||||||
53 | 1 | $this->console = $console; |
|||||
54 | 1 | $this->hooks = $hooks; |
|||||
55 | 1 | $this->input = $input; |
|||||
56 | 1 | $this->output = $output; |
|||||
57 | 1 | } |
|||||
58 | |||||||
59 | /** |
||||||
60 | * Add CLI tools to the console application |
||||||
61 | * @return void |
||||||
62 | */ |
||||||
63 | protected function bootstrap() { |
||||||
64 | $commands = $this->hooks->trigger('commands', 'cli', null, []); |
||||||
0 ignored issues
–
show
The method
trigger() does not exist on Elgg\HooksRegistrationService . Since it exists in all sub-types, consider adding an abstract or default implementation to Elgg\HooksRegistrationService .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
65 | |||||||
66 | foreach ($commands as $command) { |
||||||
67 | $this->add($command); |
||||||
68 | } |
||||||
69 | } |
||||||
70 | |||||||
71 | /** |
||||||
72 | * Add a new CLI command |
||||||
73 | * |
||||||
74 | * @param string $command Command class |
||||||
75 | * Must extend \Elgg\Cli\BaseCommand |
||||||
76 | * |
||||||
77 | * @return void |
||||||
78 | */ |
||||||
79 | public function add($command) { |
||||||
80 | if (!class_exists($command)) { |
||||||
81 | return; |
||||||
82 | } |
||||||
83 | |||||||
84 | if (!is_subclass_of($command, BaseCommand::class)) { |
||||||
85 | return; |
||||||
86 | } |
||||||
87 | |||||||
88 | $command = new $command(); |
||||||
89 | /* @var $command BaseCommand */ |
||||||
90 | |||||||
91 | if ($this->logger) { |
||||||
92 | $command->setLogger($this->logger); |
||||||
93 | } |
||||||
94 | |||||||
95 | $command->addOption('as', 'u', InputOption::VALUE_OPTIONAL, |
||||||
96 | 'Execute the command on behalf of a user with the given username' |
||||||
97 | ); |
||||||
98 | |||||||
99 | $this->console->add($command); |
||||||
100 | } |
||||||
101 | |||||||
102 | /** |
||||||
103 | * Bootstrap and run console application |
||||||
104 | * |
||||||
105 | * @return void |
||||||
106 | * @throws Exception |
||||||
107 | */ |
||||||
108 | public function run() { |
||||||
109 | $this->bootstrap(); |
||||||
110 | $this->console->run($this->input, $this->output); |
||||||
111 | } |
||||||
112 | |||||||
113 | /** |
||||||
114 | * Returns console input |
||||||
115 | * @return InputInterface |
||||||
116 | */ |
||||||
117 | public function getInput() { |
||||||
118 | return $this->input; |
||||||
119 | } |
||||||
120 | |||||||
121 | /** |
||||||
122 | * Returns console output |
||||||
123 | * @return OutputInterface |
||||||
124 | */ |
||||||
125 | public function getOutput() { |
||||||
126 | return $this->output; |
||||||
127 | } |
||||||
128 | } |
||||||
129 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: