Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions system/Commands/Utilities/ConfigCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
use CodeIgniter\Cache\FactoriesCache;
use CodeIgniter\CLI\AbstractCommand;
use CodeIgniter\CLI\Attributes\Command;
use CodeIgniter\CLI\CLI;
use CodeIgniter\CLI\Input\Argument;
use CodeIgniter\CLI\Output;
use CodeIgniter\Config\BaseConfig;
use Config\Optimize;
use Kint\Kint;
Expand Down Expand Up @@ -49,32 +49,33 @@ protected function execute(array $arguments, array $options): int
}

$config = config($class);
$output = $this->getOutput();

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

return EXIT_ERROR;
}

CLI::write($this->getDump($config));
$output->write($this->getDump($output, $config));

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

return EXIT_SUCCESS;
}

/**
* Renders the config object using Kint when available, otherwise var_dump().
*/
private function getDump(object $config): string
private function getDump(Output $output, object $config): string
{
if (defined('KINT_DIR') && Kint::$enabled_mode !== false) {
return $this->getKintD($config);
}

return CLI::color($this->getVarDump($config), 'cyan');
return $output->color($this->getVarDump($config), 'cyan');
}

/**
Expand Down
28 changes: 15 additions & 13 deletions system/Commands/Utilities/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

use CodeIgniter\CLI\AbstractCommand;
use CodeIgniter\CLI\Attributes\Command;
use CodeIgniter\CLI\CLI;
use CodeIgniter\CLI\Input\Argument;
use CodeIgniter\CLI\Output;
use CodeIgniter\Config\DotEnv;
use Config\Paths;

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

$output = $this->getOutput();

if ($env === '') {
CLI::write(sprintf(
$output->write(sprintf(
'Your environment is currently set as %s.',
CLI::color(service('superglobals')->server('CI_ENVIRONMENT', ENVIRONMENT), 'green'),
$output->color(service('superglobals')->server('CI_ENVIRONMENT', ENVIRONMENT), 'green'),
));

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

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

return EXIT_ERROR;
}

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

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

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

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

return EXIT_SUCCESS;
}

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

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

return false;
}
Expand Down
35 changes: 20 additions & 15 deletions system/Commands/Utilities/FilterCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@

use CodeIgniter\CLI\AbstractCommand;
use CodeIgniter\CLI\Attributes\Command;
use CodeIgniter\CLI\CLI;
use CodeIgniter\CLI\Input\Argument;
use CodeIgniter\CLI\Output;
use CodeIgniter\CLI\Table;
use CodeIgniter\Commands\Utilities\Routes\FilterCollector;

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

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

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

return EXIT_ERROR;
}

$this->showTable($filterCollector, $filters, $method, $route);
$this->showFilterClasses($filterCollector, $method, $route);
$this->showTable($output, $filterCollector, $filters, $method, $route);
$this->showFilterClasses($output, $filterCollector, $method, $route);

return EXIT_SUCCESS;
}
Expand All @@ -74,12 +76,13 @@ protected function execute(array $arguments, array $options): int
* @param array{before: list<string>, after: list<string>} $filters
*/
private function showTable(
Output $output,
FilterCollector $filterCollector,
array $filters,
string $method,
string $route,
): void {
$merged = $this->mergeFilters($filterCollector->getRequiredFilters(), $filters);
$merged = $this->mergeFilters($output, $filterCollector->getRequiredFilters(), $filters);

$thead = ['Method', 'Route', 'Before Filters', 'After Filters'];
$tbody = [
Expand All @@ -91,27 +94,29 @@ private function showTable(
],
];

CLI::table($tbody, $thead);
$output->write((new Table($tbody, $thead))->render());
}

private function showFilterClasses(
Output $output,
FilterCollector $filterCollector,
string $method,
string $route,
): void {
$merged = $this->mergeFilters(
$output,
$filterCollector->getRequiredFilterClasses(),
$filterCollector->getClasses($method, $route),
);

$lastPosition = array_key_last($merged);

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

if ($position !== $lastPosition) {
CLI::newLine();
$output->newLine();
}
}
}
Expand All @@ -125,11 +130,11 @@ private function showFilterClasses(
*
* @return array{before: list<string>, after: list<string>}
*/
private function mergeFilters(array $required, array $filters): array
private function mergeFilters(Output $output, array $required, array $filters): array
{
return [
'before' => array_merge($this->highlight($required['before']), $filters['before']),
'after' => array_merge($filters['after'], $this->highlight($required['after'])),
'before' => array_merge($this->highlight($output, $required['before']), $filters['before']),
'after' => array_merge($filters['after'], $this->highlight($output, $required['after'])),
];
}

Expand All @@ -140,8 +145,8 @@ private function mergeFilters(array $required, array $filters): array
*
* @return list<string>
*/
private function highlight(array $items): array
private function highlight(Output $output, array $items): array
{
return array_map(static fn (string $item): string => CLI::color($item, 'yellow'), $items);
return array_map(static fn (string $item): string => $output->color($item, 'yellow'), $items);
}
}
4 changes: 2 additions & 2 deletions system/Commands/Utilities/Namespaces.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

use CodeIgniter\CLI\AbstractCommand;
use CodeIgniter\CLI\Attributes\Command;
use CodeIgniter\CLI\CLI;
use CodeIgniter\CLI\Input\Option;
use CodeIgniter\CLI\Table;
use Config\Autoload;

/**
Expand Down Expand Up @@ -63,7 +63,7 @@ protected function execute(array $arguments, array $options): int
(int) $options['max-length'],
);

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

return EXIT_SUCCESS;
}
Expand Down
36 changes: 19 additions & 17 deletions system/Commands/Utilities/Optimize.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use CodeIgniter\Autoloader\FileLocatorCached;
use CodeIgniter\CLI\AbstractCommand;
use CodeIgniter\CLI\Attributes\Command;
use CodeIgniter\CLI\CLI;
use CodeIgniter\CLI\Output;
use CodeIgniter\Exceptions\RuntimeException;
use CodeIgniter\Publisher\Publisher;

Expand All @@ -29,20 +29,22 @@ final class Optimize extends AbstractCommand
{
protected function execute(array $arguments, array $options): int
{
$output = $this->getOutput();

try {
$this->enableCaching();
$this->clearCache();
$this->removeDevPackages();
$this->enableCaching($output);
$this->clearCache($output);
$this->removeDevPackages($output);

return EXIT_SUCCESS;
} catch (RuntimeException) {
CLI::error('The "spark optimize" failed.');
$output->error('The "spark optimize" failed.');

return EXIT_ERROR;
}
}

private function clearCache(): void
private function clearCache(Output $output): void
{
$locator = service('locator');

Expand All @@ -51,29 +53,29 @@ private function clearCache(): void
}

$locator->deleteCache();
CLI::write('Removed FileLocatorCache.', 'green');
$output->write('Removed FileLocatorCache.', 'green');

$this->removeFile(WRITEPATH . 'cache/FactoriesCache_config');
$this->removeFile($output, WRITEPATH . 'cache/FactoriesCache_config');
}

private function removeFile(string $file): void
private function removeFile(Output $output, string $file): void
{
if (! is_file($file)) {
return;
}

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

return;
}

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

throw new RuntimeException(__METHOD__);
}

private function enableCaching(): void
private function enableCaching(Output $output): void
{
$publisher = new Publisher(APPPATH, APPPATH);

Expand All @@ -88,20 +90,20 @@ private function enableCaching(): void
);

if ($result) {
CLI::write(
$output->write(
'Config Caching and FileLocator Caching are enabled in "app/Config/Optimize.php".',
'green',
);

return;
}

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

throw new RuntimeException(__METHOD__);
}

private function removeDevPackages(): void
private function removeDevPackages(Output $output): void
{
if (! defined('VENDORPATH')) {
return;
Expand All @@ -111,12 +113,12 @@ private function removeDevPackages(): void
passthru('composer install --no-dev', $status);

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

return;
}

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

throw new RuntimeException(__METHOD__);
}
Expand Down
3 changes: 1 addition & 2 deletions system/Commands/Utilities/PhpIniCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use CodeIgniter\CLI\AbstractCommand;
use CodeIgniter\CLI\Attributes\Command;
use CodeIgniter\CLI\CLI;
use CodeIgniter\CLI\Input\Argument;
use CodeIgniter\Security\CheckPhpIni;

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

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

return EXIT_ERROR;
}
Expand Down
Loading
Loading