Skip to content

Commit de9f474

Browse files
authored
Merge pull request #4683 from paulbalandan/command-runner
Optimize CommandRunner and Commands
2 parents e3b5232 + 8f71558 commit de9f474

File tree

4 files changed

+66
-109
lines changed

4 files changed

+66
-109
lines changed

system/CLI/BaseCommand.php

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,16 @@
1616
use Throwable;
1717

1818
/**
19-
* Class BaseCommand
19+
* BaseCommand is the base class used in creating CLI commands.
20+
*
21+
* @property string $group
22+
* @property string $name
23+
* @property string $usage
24+
* @property string $description
25+
* @property array $options
26+
* @property array $arguments
27+
* @property LoggerInterface $logger
28+
* @property Commands $commands
2029
*/
2130
abstract class BaseCommand
2231
{
@@ -78,7 +87,6 @@ abstract class BaseCommand
7887
*/
7988
protected $commands;
8089

81-
//--------------------------------------------------------------------
8290
/**
8391
* BaseCommand constructor.
8492
*
@@ -91,8 +99,6 @@ public function __construct(LoggerInterface $logger, Commands $commands)
9199
$this->commands = $commands;
92100
}
93101

94-
//--------------------------------------------------------------------
95-
96102
/**
97103
* Actually execute a command.
98104
* This has to be over-ridden in any concrete implementation.
@@ -101,8 +107,6 @@ public function __construct(LoggerInterface $logger, Commands $commands)
101107
*/
102108
abstract public function run(array $params);
103109

104-
//--------------------------------------------------------------------
105-
106110
/**
107111
* Can be used by a command to run other commands.
108112
*
@@ -117,8 +121,6 @@ protected function call(string $command, array $params = [])
117121
return $this->commands->run($command, $params);
118122
}
119123

120-
//--------------------------------------------------------------------
121-
122124
/**
123125
* A simple method to display an error with line/file, in child commands.
124126
*
@@ -132,8 +134,6 @@ protected function showError(Throwable $e)
132134
require APPPATH . 'Views/errors/cli/error_exception.php';
133135
}
134136

135-
//--------------------------------------------------------------------
136-
137137
/**
138138
* Show Help includes (Usage, Arguments, Description, Options).
139139
*/
@@ -189,8 +189,6 @@ public function showHelp()
189189
}
190190
}
191191

192-
//--------------------------------------------------------------------
193-
194192
/**
195193
* Pads our string out so that all titles are the same length to nicely line up descriptions.
196194
*
@@ -208,8 +206,6 @@ public function setPad(string $item, int $max, int $extra = 2, int $indent = 0):
208206
return str_pad(str_repeat(' ', $indent) . $item, $max);
209207
}
210208

211-
//--------------------------------------------------------------------
212-
213209
/**
214210
* Get pad for $key => $value array output
215211
*
@@ -225,15 +221,15 @@ public function setPad(string $item, int $max, int $extra = 2, int $indent = 0):
225221
public function getPad(array $array, int $pad): int
226222
{
227223
$max = 0;
224+
228225
foreach (array_keys($array) as $key)
229226
{
230227
$max = max($max, strlen($key));
231228
}
229+
232230
return $max + $pad;
233231
}
234232

235-
//--------------------------------------------------------------------
236-
237233
/**
238234
* Makes it simple to access our protected properties.
239235
*
@@ -251,8 +247,6 @@ public function __get(string $key)
251247
return null;
252248
}
253249

254-
//--------------------------------------------------------------------
255-
256250
/**
257251
* Makes it simple to check our protected properties.
258252
*

system/CLI/CommandRunner.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,12 @@
2121
class CommandRunner extends Controller
2222
{
2323
/**
24-
* The Command Manager
24+
* Instance of class managing the collection of commands
2525
*
2626
* @var Commands
2727
*/
2828
protected $commands;
2929

30-
//--------------------------------------------------------------------
31-
3230
/**
3331
* Constructor
3432
*/
@@ -58,8 +56,6 @@ public function _remap($method, ...$params)
5856
return $this->index($params);
5957
}
6058

61-
//--------------------------------------------------------------------
62-
6359
/**
6460
* Default command.
6561
*
@@ -70,14 +66,9 @@ public function _remap($method, ...$params)
7066
*/
7167
public function index(array $params)
7268
{
73-
$command = array_shift($params);
74-
75-
if (is_null($command))
76-
{
77-
$command = 'list';
78-
}
69+
$command = array_shift($params) ?? 'list';
7970

80-
return service('commands')->run($command, $params);
71+
return $this->commands->run($command, $params);
8172
}
8273

8374
/**

system/CLI/Commands.php

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,12 @@
1111

1212
namespace CodeIgniter\CLI;
1313

14+
use CodeIgniter\Autoloader\FileLocator;
1415
use CodeIgniter\Log\Logger;
15-
use Config\Services;
1616
use ReflectionClass;
1717
use ReflectionException;
1818

1919
/**
20-
* Class Commands
21-
*
2220
* Core functionality for running, listing, etc commands.
2321
*/
2422
class Commands
@@ -44,7 +42,8 @@ class Commands
4442
*/
4543
public function __construct($logger = null)
4644
{
47-
$this->logger = $logger ?? Services::logger();
45+
$this->logger = $logger ?? service('logger');
46+
$this->discoverCommands();
4847
}
4948

5049
/**
@@ -55,8 +54,6 @@ public function __construct($logger = null)
5554
*/
5655
public function run(string $command, array $params)
5756
{
58-
$this->discoverCommands();
59-
6057
if (! $this->verifyCommand($command, $this->commands))
6158
{
6259
return;
@@ -77,39 +74,39 @@ public function run(string $command, array $params)
7774
*/
7875
public function getCommands()
7976
{
80-
$this->discoverCommands();
81-
8277
return $this->commands;
8378
}
8479

8580
/**
8681
* Discovers all commands in the framework and within user code,
8782
* and collects instances of them to work with.
83+
*
84+
* @return void
8885
*/
8986
public function discoverCommands()
9087
{
91-
if (! empty($this->commands))
88+
if ($this->commands !== [])
9289
{
9390
return;
9491
}
9592

96-
$files = service('locator')->listFiles('Commands/');
93+
/** @var FileLocator $locator */
94+
$locator = service('locator');
95+
$files = $locator->listFiles('Commands/');
9796

9897
// If no matching command files were found, bail
99-
if (empty($files))
98+
// This should never happen in unit testing.
99+
if ($files === [])
100100
{
101-
// This should never happen in unit testing.
102-
// if it does, we have far bigger problems!
103-
// @codeCoverageIgnoreStart
104-
return;
105-
// @codeCoverageIgnoreEnd
101+
return; // @codeCoverageIgnore
106102
}
107103

108104
// Loop over each file checking to see if a command with that
109-
// alias exists in the class. If so, return it. Otherwise, try the next.
105+
// alias exists in the class.
110106
foreach ($files as $file)
111107
{
112-
$className = Services::locator()->findQualifiedNameFromPath($file);
108+
$className = $locator->findQualifiedNameFromPath($file);
109+
113110
if (empty($className) || ! class_exists($className))
114111
{
115112
continue;
@@ -124,10 +121,10 @@ public function discoverCommands()
124121
continue;
125122
}
126123

124+
/** @var BaseCommand $class */
127125
$class = new $className($this->logger, $this);
128126

129-
// Store it!
130-
if (! is_null($class->group))
127+
if (isset($class->group))
131128
{
132129
$this->commands[$class->name] = [
133130
'class' => $className,
@@ -137,7 +134,6 @@ public function discoverCommands()
137134
];
138135
}
139136

140-
$class = null;
141137
unset($class);
142138
}
143139
catch (ReflectionException $e)

0 commit comments

Comments
 (0)