Skip to content

Commit fa84a28

Browse files
committed
refactor: write the Utilities commands through $this->getOutput()
1 parent 58efb74 commit fa84a28

9 files changed

Lines changed: 98 additions & 83 deletions

File tree

‎system/Commands/Utilities/ConfigCheck.php‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
use CodeIgniter\Cache\FactoriesCache;
1717
use CodeIgniter\CLI\AbstractCommand;
1818
use CodeIgniter\CLI\Attributes\Command;
19-
use CodeIgniter\CLI\CLI;
2019
use CodeIgniter\CLI\Input\Argument;
20+
use CodeIgniter\CLI\Output;
2121
use CodeIgniter\Config\BaseConfig;
2222
use Config\Optimize;
2323
use Kint\Kint;
@@ -49,32 +49,33 @@ protected function execute(array $arguments, array $options): int
4949
}
5050

5151
$config = config($class);
52+
$output = $this->getOutput();
5253

5354
if ($config === null) {
54-
CLI::error(sprintf('Config class "%s" not found.', $class));
55+
$output->error(sprintf('Config class "%s" not found.', $class));
5556

5657
return EXIT_ERROR;
5758
}
5859

59-
CLI::write($this->getDump($config));
60+
$output->write($this->getDump($output, $config));
6061

61-
CLI::newLine();
62-
$state = CLI::color($configCacheEnabled ? 'enabled' : 'disabled', 'green');
63-
CLI::write(sprintf('Config caching: %s', $state));
62+
$output->newLine();
63+
$state = $output->color($configCacheEnabled ? 'enabled' : 'disabled', 'green');
64+
$output->write(sprintf('Config caching: %s', $state));
6465

6566
return EXIT_SUCCESS;
6667
}
6768

6869
/**
6970
* Renders the config object using Kint when available, otherwise var_dump().
7071
*/
71-
private function getDump(object $config): string
72+
private function getDump(Output $output, object $config): string
7273
{
7374
if (defined('KINT_DIR') && Kint::$enabled_mode !== false) {
7475
return $this->getKintD($config);
7576
}
7677

77-
return CLI::color($this->getVarDump($config), 'cyan');
78+
return $output->color($this->getVarDump($config), 'cyan');
7879
}
7980

8081
/**

‎system/Commands/Utilities/Environment.php‎

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
use CodeIgniter\CLI\AbstractCommand;
1717
use CodeIgniter\CLI\Attributes\Command;
18-
use CodeIgniter\CLI\CLI;
1918
use CodeIgniter\CLI\Input\Argument;
19+
use CodeIgniter\CLI\Output;
2020
use CodeIgniter\Config\DotEnv;
2121
use Config\Paths;
2222

@@ -52,10 +52,12 @@ protected function execute(array $arguments, array $options): int
5252
$env = $arguments['environment'];
5353
assert(is_string($env));
5454

55+
$output = $this->getOutput();
56+
5557
if ($env === '') {
56-
CLI::write(sprintf(
58+
$output->write(sprintf(
5759
'Your environment is currently set as %s.',
58-
CLI::color(service('superglobals')->server('CI_ENVIRONMENT', ENVIRONMENT), 'green'),
60+
$output->color(service('superglobals')->server('CI_ENVIRONMENT', ENVIRONMENT), 'green'),
5961
));
6062

6163
return EXIT_SUCCESS;
@@ -64,14 +66,14 @@ protected function execute(array $arguments, array $options): int
6466
$env = strtolower($env);
6567

6668
if ($env === 'testing') {
67-
CLI::error('The "testing" environment is reserved for PHPUnit testing.', 'light_gray', 'red');
68-
CLI::error('You will not be able to run spark under a "testing" environment.', 'light_gray', 'red');
69+
$output->error('The "testing" environment is reserved for PHPUnit testing.', 'light_gray', 'red');
70+
$output->error('You will not be able to run spark under a "testing" environment.', 'light_gray', 'red');
6971

7072
return EXIT_ERROR;
7173
}
7274

7375
if (! in_array($env, self::$knownTypes, true)) {
74-
CLI::error(sprintf(
76+
$output->error(sprintf(
7577
'Invalid environment type "%s". Expected one of "%s".',
7678
$env,
7779
implode('" and "', self::$knownTypes),
@@ -80,8 +82,8 @@ protected function execute(array $arguments, array $options): int
8082
return EXIT_ERROR;
8183
}
8284

83-
if (! $this->writeNewEnvironmentToEnvFile($env)) {
84-
CLI::error('Error in writing new environment to .env file.', 'light_gray', 'red');
85+
if (! $this->writeNewEnvironmentToEnvFile($output, $env)) {
86+
$output->error('Error in writing new environment to .env file.', 'light_gray', 'red');
8587

8688
return EXIT_ERROR;
8789
}
@@ -93,24 +95,24 @@ protected function execute(array $arguments, array $options): int
9395
service('superglobals')->unsetServer('CI_ENVIRONMENT');
9496
(new DotEnv((new Paths())->envDirectory ?? ROOTPATH))->load(); // @phpstan-ignore nullCoalesce.property
9597

96-
CLI::write(sprintf('Environment is successfully changed to "%s".', $env), 'green');
97-
CLI::write('The ENVIRONMENT constant will be changed in the next script execution.');
98+
$output->write(sprintf('Environment is successfully changed to "%s".', $env), 'green');
99+
$output->write('The ENVIRONMENT constant will be changed in the next script execution.');
98100

99101
return EXIT_SUCCESS;
100102
}
101103

102104
/**
103105
* @see https://regex101.com/r/4sSORp/1 for the regex in action
104106
*/
105-
private function writeNewEnvironmentToEnvFile(string $newEnv): bool
107+
private function writeNewEnvironmentToEnvFile(Output $output, string $newEnv): bool
106108
{
107109
$baseEnv = ROOTPATH . 'env';
108110
$envFile = ((new Paths())->envDirectory ?? ROOTPATH) . '.env'; // @phpstan-ignore nullCoalesce.property
109111

110112
if (! is_file($envFile)) {
111113
if (! is_file($baseEnv)) {
112-
CLI::write('Both default shipped `env` file and custom `.env` are missing.', 'yellow');
113-
CLI::write('It is impossible to write the new environment type.', 'yellow');
114+
$output->write('Both default shipped `env` file and custom `.env` are missing.', 'yellow');
115+
$output->write('It is impossible to write the new environment type.', 'yellow');
114116

115117
return false;
116118
}

‎system/Commands/Utilities/FilterCheck.php‎

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515

1616
use CodeIgniter\CLI\AbstractCommand;
1717
use CodeIgniter\CLI\Attributes\Command;
18-
use CodeIgniter\CLI\CLI;
1918
use CodeIgniter\CLI\Input\Argument;
19+
use CodeIgniter\CLI\Output;
20+
use CodeIgniter\CLI\Table;
2021
use CodeIgniter\Commands\Utilities\Routes\FilterCollector;
2122

2223
/**
@@ -53,19 +54,20 @@ protected function execute(array $arguments, array $options): int
5354
$filterCollector = new FilterCollector();
5455

5556
$filters = $filterCollector->get($method, $route);
57+
$output = $this->getOutput();
5658

5759
// PageNotFoundException
5860
if ($filters['before'] === ['<unknown>']) {
59-
CLI::error(sprintf(
61+
$output->error(sprintf(
6062
"Can't find a route: %s",
61-
CLI::color(sprintf('"%s %s"', strtoupper($method), $route), 'black', 'light_gray'),
63+
$output->color(sprintf('"%s %s"', strtoupper($method), $route), 'black', 'light_gray'),
6264
));
6365

6466
return EXIT_ERROR;
6567
}
6668

67-
$this->showTable($filterCollector, $filters, $method, $route);
68-
$this->showFilterClasses($filterCollector, $method, $route);
69+
$this->showTable($output, $filterCollector, $filters, $method, $route);
70+
$this->showFilterClasses($output, $filterCollector, $method, $route);
6971

7072
return EXIT_SUCCESS;
7173
}
@@ -74,12 +76,13 @@ protected function execute(array $arguments, array $options): int
7476
* @param array{before: list<string>, after: list<string>} $filters
7577
*/
7678
private function showTable(
79+
Output $output,
7780
FilterCollector $filterCollector,
7881
array $filters,
7982
string $method,
8083
string $route,
8184
): void {
82-
$merged = $this->mergeFilters($filterCollector->getRequiredFilters(), $filters);
85+
$merged = $this->mergeFilters($output, $filterCollector->getRequiredFilters(), $filters);
8386

8487
$thead = ['Method', 'Route', 'Before Filters', 'After Filters'];
8588
$tbody = [
@@ -91,27 +94,29 @@ private function showTable(
9194
],
9295
];
9396

94-
CLI::table($tbody, $thead);
97+
$output->write((new Table($tbody, $thead))->render());
9598
}
9699

97100
private function showFilterClasses(
101+
Output $output,
98102
FilterCollector $filterCollector,
99103
string $method,
100104
string $route,
101105
): void {
102106
$merged = $this->mergeFilters(
107+
$output,
103108
$filterCollector->getRequiredFilterClasses(),
104109
$filterCollector->getClasses($method, $route),
105110
);
106111

107112
$lastPosition = array_key_last($merged);
108113

109114
foreach ($merged as $position => $classes) {
110-
CLI::write(sprintf('%s Filter Classes:', ucfirst($position)), 'cyan');
111-
CLI::write(implode(' → ', $classes));
115+
$output->write(sprintf('%s Filter Classes:', ucfirst($position)), 'cyan');
116+
$output->write(implode(' → ', $classes));
112117

113118
if ($position !== $lastPosition) {
114-
CLI::newLine();
119+
$output->newLine();
115120
}
116121
}
117122
}
@@ -125,11 +130,11 @@ private function showFilterClasses(
125130
*
126131
* @return array{before: list<string>, after: list<string>}
127132
*/
128-
private function mergeFilters(array $required, array $filters): array
133+
private function mergeFilters(Output $output, array $required, array $filters): array
129134
{
130135
return [
131-
'before' => array_merge($this->highlight($required['before']), $filters['before']),
132-
'after' => array_merge($filters['after'], $this->highlight($required['after'])),
136+
'before' => array_merge($this->highlight($output, $required['before']), $filters['before']),
137+
'after' => array_merge($filters['after'], $this->highlight($output, $required['after'])),
133138
];
134139
}
135140

@@ -140,8 +145,8 @@ private function mergeFilters(array $required, array $filters): array
140145
*
141146
* @return list<string>
142147
*/
143-
private function highlight(array $items): array
148+
private function highlight(Output $output, array $items): array
144149
{
145-
return array_map(static fn (string $item): string => CLI::color($item, 'yellow'), $items);
150+
return array_map(static fn (string $item): string => $output->color($item, 'yellow'), $items);
146151
}
147152
}

‎system/Commands/Utilities/Namespaces.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
use CodeIgniter\CLI\AbstractCommand;
1717
use CodeIgniter\CLI\Attributes\Command;
18-
use CodeIgniter\CLI\CLI;
1918
use CodeIgniter\CLI\Input\Option;
19+
use CodeIgniter\CLI\Table;
2020
use Config\Autoload;
2121

2222
/**
@@ -63,7 +63,7 @@ protected function execute(array $arguments, array $options): int
6363
(int) $options['max-length'],
6464
);
6565

66-
CLI::table($tbody, ['Namespace', 'Path', 'Found?']);
66+
$this->getOutput()->write((new Table($tbody, ['Namespace', 'Path', 'Found?']))->render());
6767

6868
return EXIT_SUCCESS;
6969
}

‎system/Commands/Utilities/Optimize.php‎

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use CodeIgniter\Autoloader\FileLocatorCached;
1818
use CodeIgniter\CLI\AbstractCommand;
1919
use CodeIgniter\CLI\Attributes\Command;
20-
use CodeIgniter\CLI\CLI;
20+
use CodeIgniter\CLI\Output;
2121
use CodeIgniter\Exceptions\RuntimeException;
2222
use CodeIgniter\Publisher\Publisher;
2323

@@ -29,20 +29,22 @@ final class Optimize extends AbstractCommand
2929
{
3030
protected function execute(array $arguments, array $options): int
3131
{
32+
$output = $this->getOutput();
33+
3234
try {
33-
$this->enableCaching();
34-
$this->clearCache();
35-
$this->removeDevPackages();
35+
$this->enableCaching($output);
36+
$this->clearCache($output);
37+
$this->removeDevPackages($output);
3638

3739
return EXIT_SUCCESS;
3840
} catch (RuntimeException) {
39-
CLI::error('The "spark optimize" failed.');
41+
$output->error('The "spark optimize" failed.');
4042

4143
return EXIT_ERROR;
4244
}
4345
}
4446

45-
private function clearCache(): void
47+
private function clearCache(Output $output): void
4648
{
4749
$locator = service('locator');
4850

@@ -51,29 +53,29 @@ private function clearCache(): void
5153
}
5254

5355
$locator->deleteCache();
54-
CLI::write('Removed FileLocatorCache.', 'green');
56+
$output->write('Removed FileLocatorCache.', 'green');
5557

56-
$this->removeFile(WRITEPATH . 'cache/FactoriesCache_config');
58+
$this->removeFile($output, WRITEPATH . 'cache/FactoriesCache_config');
5759
}
5860

59-
private function removeFile(string $file): void
61+
private function removeFile(Output $output, string $file): void
6062
{
6163
if (! is_file($file)) {
6264
return;
6365
}
6466

6567
if (unlink($file)) {
66-
CLI::write(sprintf('Removed "%s".', clean_path($file)), 'green');
68+
$output->write(sprintf('Removed "%s".', clean_path($file)), 'green');
6769

6870
return;
6971
}
7072

71-
CLI::error(sprintf('Error in removing file: %s', clean_path($file)));
73+
$output->error(sprintf('Error in removing file: %s', clean_path($file)));
7274

7375
throw new RuntimeException(__METHOD__);
7476
}
7577

76-
private function enableCaching(): void
78+
private function enableCaching(Output $output): void
7779
{
7880
$publisher = new Publisher(APPPATH, APPPATH);
7981

@@ -88,20 +90,20 @@ private function enableCaching(): void
8890
);
8991

9092
if ($result) {
91-
CLI::write(
93+
$output->write(
9294
'Config Caching and FileLocator Caching are enabled in "app/Config/Optimize.php".',
9395
'green',
9496
);
9597

9698
return;
9799
}
98100

99-
CLI::error(sprintf('Error in updating file: %s', clean_path($config)));
101+
$output->error(sprintf('Error in updating file: %s', clean_path($config)));
100102

101103
throw new RuntimeException(__METHOD__);
102104
}
103105

104-
private function removeDevPackages(): void
106+
private function removeDevPackages(Output $output): void
105107
{
106108
if (! defined('VENDORPATH')) {
107109
return;
@@ -111,12 +113,12 @@ private function removeDevPackages(): void
111113
passthru('composer install --no-dev', $status);
112114

113115
if ($status === 0) {
114-
CLI::write('Removed Composer dev packages.', 'green');
116+
$output->write('Removed Composer dev packages.', 'green');
115117

116118
return;
117119
}
118120

119-
CLI::error('Error in removing Composer dev packages.');
121+
$output->error('Error in removing Composer dev packages.');
120122

121123
throw new RuntimeException(__METHOD__);
122124
}

‎system/Commands/Utilities/PhpIniCheck.php‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
use CodeIgniter\CLI\AbstractCommand;
1717
use CodeIgniter\CLI\Attributes\Command;
18-
use CodeIgniter\CLI\CLI;
1918
use CodeIgniter\CLI\Input\Argument;
2019
use CodeIgniter\Security\CheckPhpIni;
2120

@@ -45,7 +44,7 @@ protected function execute(array $arguments, array $options): int
4544
$section = $arguments['section'];
4645

4746
if ($section !== '' && $section !== 'opcache') {
48-
CLI::error('You must specify a correct argument.');
47+
$this->getOutput()->error('You must specify a correct argument.');
4948

5049
return EXIT_ERROR;
5150
}

0 commit comments

Comments
 (0)