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
65 changes: 17 additions & 48 deletions system/Commands/Utilities/Optimize.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,49 +15,19 @@

use CodeIgniter\Autoloader\FileLocator;
use CodeIgniter\Autoloader\FileLocatorCached;
use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\AbstractCommand;
use CodeIgniter\CLI\Attributes\Command;
use CodeIgniter\CLI\CLI;
use CodeIgniter\Exceptions\RuntimeException;
use CodeIgniter\Publisher\Publisher;

/**
* Optimize for production.
*/
final class Optimize extends BaseCommand
#[Command(name: 'optimize', description: 'Optimize for production.', group: 'CodeIgniter')]
final class Optimize extends AbstractCommand
{
/**
* The group the command is lumped under
* when listing commands.
*
* @var string
*/
protected $group = 'CodeIgniter';

/**
* The Command's name
*
* @var string
*/
protected $name = 'optimize';

/**
* The Command's short description
*
* @var string
*/
protected $description = 'Optimize for production.';

/**
* The Command's usage
*
* @var string
*/
protected $usage = 'optimize';

/**
* @return int
*/
public function run(array $params)
protected function execute(array $arguments, array $options): int
{
try {
$this->enableCaching();
Expand All @@ -78,25 +48,24 @@ private function clearCache(): void
$locator->deleteCache();
CLI::write('Removed FileLocatorCache.', 'green');

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

private function removeFile(string $cache): void
private function removeFile(string $file): void
{
if (is_file($cache)) {
$result = unlink($cache);
if (! is_file($file)) {
return;
}

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

return;
}
return;
}

CLI::error('Error in removing file: ' . clean_path($cache));
CLI::error(sprintf('Error in removing file: %s', clean_path($file)));

throw new RuntimeException(__METHOD__);
}
throw new RuntimeException(__METHOD__);
}

private function enableCaching(): void
Expand All @@ -122,7 +91,7 @@ private function enableCaching(): void
return;
}

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

throw new RuntimeException(__METHOD__);
}
Expand Down
68 changes: 68 additions & 0 deletions tests/system/Commands/Utilities/OptimizeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

namespace CodeIgniter\Commands\Utilities;

use Closure;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\ReflectionHelper;
use CodeIgniter\Test\StreamFilterTrait;
use PHPUnit\Framework\Attributes\Group;

/**
* @internal
*/
#[Group('Others')]
final class OptimizeTest extends CIUnitTestCase
{
use ReflectionHelper;
use StreamFilterTrait;

private string $file = WRITEPATH . 'cache/OptimizeTest_config';

protected function tearDown(): void
{
parent::tearDown();

if (is_file($this->file)) {
unlink($this->file);
}
}

/**
* @return Closure(string): void
*/
private function getRemoveFile(): Closure
{
return self::getPrivateMethodInvoker(new Optimize(service('commands')), 'removeFile');
}

public function testRemoveFileDeletesTheFile(): void
{
file_put_contents($this->file, '<?php');

($this->getRemoveFile())($this->file);

$this->assertFileDoesNotExist($this->file);
$this->assertStringContainsString('Removed', $this->getStreamFilterBuffer());
}

public function testRemoveFileDoesNothingWhenFileIsAbsent(): void
{
$this->assertFileDoesNotExist($this->file);

($this->getRemoveFile())($this->file);

$this->assertSame('', $this->getStreamFilterBuffer());
}
}
Loading