@@ -50,21 +50,19 @@ class CommandRunner extends Controller
5050{
5151
5252 /**
53- * Stores the info about found Commands.
53+ * The Command Manager
5454 *
55- * @var array
55+ * @var Commands
5656 */
57- protected $ commands = [];
58-
59- /**
60- * Message logger.
61- *
62- * @var \CodeIgniter\Log\Logger
63- */
64- protected $ logger ;
57+ protected $ commands ;
6558
6659 //--------------------------------------------------------------------
6760
61+ public function __construct ()
62+ {
63+ $ this ->commands = service ('commands ' );
64+ }
65+
6866 /**
6967 * We map all un-routed CLI methods through this function
7068 * so we have the chance to look for a Command first.
@@ -100,113 +98,21 @@ public function index(array $params)
10098 {
10199 $ command = array_shift ($ params );
102100
103- $ this ->createCommandList ();
104-
105101 if (is_null ($ command ))
106102 {
107103 $ command = 'list ' ;
108104 }
109105
110- return $ this ->runCommand ($ command , $ params );
111- }
112-
113- //--------------------------------------------------------------------
114-
115- /**
116- * Actually runs the command.
117- *
118- * @param string $command
119- * @param array $params
120- *
121- * @return mixed
122- */
123- protected function runCommand (string $ command , array $ params )
124- {
125- if (! isset ($ this ->commands [$ command ]))
126- {
127- CLI ::error (lang ('CLI.commandNotFound ' , [$ command ]));
128- CLI ::newLine ();
129- return ;
130- }
131-
132- // The file would have already been loaded during the
133- // createCommandList function...
134- $ className = $ this ->commands [$ command ]['class ' ];
135- $ class = new $ className ($ this ->logger , $ this );
136-
137- return $ class ->run ($ params );
106+ return service ('commands ' )->run ($ command , $ params );
138107 }
139108
140- //--------------------------------------------------------------------
141-
142- /**
143- * Scans all Commands directories and prepares a list
144- * of each command with it's group and file.
145- *
146- * @throws \ReflectionException
147- */
148- protected function createCommandList ()
149- {
150- $ files = Services::locator ()->listFiles ('Commands/ ' );
151-
152- // If no matching command files were found, bail
153- if (empty ($ files ))
154- {
155- // This should never happen in unit testing.
156- // if it does, we have far bigger problems!
157- // @codeCoverageIgnoreStart
158- return ;
159- // @codeCoverageIgnoreEnd
160- }
161-
162- // Loop over each file checking to see if a command with that
163- // alias exists in the class. If so, return it. Otherwise, try the next.
164- foreach ($ files as $ file )
165- {
166- $ className = Services::locator ()->findQualifiedNameFromPath ($ file );
167- if (empty ($ className ) || ! class_exists ($ className ))
168- {
169- continue ;
170- }
171-
172- $ class = new \ReflectionClass ($ className );
173-
174- if (! $ class ->isInstantiable () || ! $ class ->isSubclassOf (BaseCommand::class))
175- {
176- continue ;
177- }
178-
179- $ class = new $ className ($ this ->logger , $ this );
180-
181- // Store it!
182- if ($ class ->group !== null )
183- {
184- $ this ->commands [$ class ->name ] = [
185- 'class ' => $ className ,
186- 'file ' => $ file ,
187- 'group ' => $ class ->group ,
188- 'description ' => $ class ->description ,
189- ];
190- }
191-
192- $ class = null ;
193- unset($ class );
194- }
195-
196- asort ($ this ->commands );
197- }
198-
199- //--------------------------------------------------------------------
200-
201109 /**
202110 * Allows access to the current commands that have been found.
203111 *
204112 * @return array
205113 */
206114 public function getCommands (): array
207115 {
208- return $ this ->commands ;
116+ return $ this ->commands -> getCommands () ;
209117 }
210-
211- //--------------------------------------------------------------------
212118}
0 commit comments