Skip to content

Commit fe7064a

Browse files
authored
refactor: migrate optimize command as modern command (#10454)
1 parent b82329e commit fe7064a

2 files changed

Lines changed: 85 additions & 48 deletions

File tree

system/Commands/Utilities/Optimize.php

Lines changed: 17 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -15,49 +15,19 @@
1515

1616
use CodeIgniter\Autoloader\FileLocator;
1717
use CodeIgniter\Autoloader\FileLocatorCached;
18-
use CodeIgniter\CLI\BaseCommand;
18+
use CodeIgniter\CLI\AbstractCommand;
19+
use CodeIgniter\CLI\Attributes\Command;
1920
use CodeIgniter\CLI\CLI;
2021
use CodeIgniter\Exceptions\RuntimeException;
2122
use CodeIgniter\Publisher\Publisher;
2223

2324
/**
2425
* Optimize for production.
2526
*/
26-
final class Optimize extends BaseCommand
27+
#[Command(name: 'optimize', description: 'Optimize for production.', group: 'CodeIgniter')]
28+
final class Optimize extends AbstractCommand
2729
{
28-
/**
29-
* The group the command is lumped under
30-
* when listing commands.
31-
*
32-
* @var string
33-
*/
34-
protected $group = 'CodeIgniter';
35-
36-
/**
37-
* The Command's name
38-
*
39-
* @var string
40-
*/
41-
protected $name = 'optimize';
42-
43-
/**
44-
* The Command's short description
45-
*
46-
* @var string
47-
*/
48-
protected $description = 'Optimize for production.';
49-
50-
/**
51-
* The Command's usage
52-
*
53-
* @var string
54-
*/
55-
protected $usage = 'optimize';
56-
57-
/**
58-
* @return int
59-
*/
60-
public function run(array $params)
30+
protected function execute(array $arguments, array $options): int
6131
{
6232
try {
6333
$this->enableCaching();
@@ -78,25 +48,24 @@ private function clearCache(): void
7848
$locator->deleteCache();
7949
CLI::write('Removed FileLocatorCache.', 'green');
8050

81-
$cache = WRITEPATH . 'cache/FactoriesCache_config';
82-
$this->removeFile($cache);
51+
$this->removeFile(WRITEPATH . 'cache/FactoriesCache_config');
8352
}
8453

85-
private function removeFile(string $cache): void
54+
private function removeFile(string $file): void
8655
{
87-
if (is_file($cache)) {
88-
$result = unlink($cache);
56+
if (! is_file($file)) {
57+
return;
58+
}
8959

90-
if ($result) {
91-
CLI::write('Removed "' . clean_path($cache) . '".', 'green');
60+
if (unlink($file)) {
61+
CLI::write(sprintf('Removed "%s".', clean_path($file)), 'green');
9262

93-
return;
94-
}
63+
return;
64+
}
9565

96-
CLI::error('Error in removing file: ' . clean_path($cache));
66+
CLI::error(sprintf('Error in removing file: %s', clean_path($file)));
9767

98-
throw new RuntimeException(__METHOD__);
99-
}
68+
throw new RuntimeException(__METHOD__);
10069
}
10170

10271
private function enableCaching(): void
@@ -122,7 +91,7 @@ private function enableCaching(): void
12291
return;
12392
}
12493

125-
CLI::error('Error in updating file: ' . clean_path($config));
94+
CLI::error(sprintf('Error in updating file: %s', clean_path($config)));
12695

12796
throw new RuntimeException(__METHOD__);
12897
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace CodeIgniter\Commands\Utilities;
15+
16+
use Closure;
17+
use CodeIgniter\Test\CIUnitTestCase;
18+
use CodeIgniter\Test\ReflectionHelper;
19+
use CodeIgniter\Test\StreamFilterTrait;
20+
use PHPUnit\Framework\Attributes\Group;
21+
22+
/**
23+
* @internal
24+
*/
25+
#[Group('Others')]
26+
final class OptimizeTest extends CIUnitTestCase
27+
{
28+
use ReflectionHelper;
29+
use StreamFilterTrait;
30+
31+
private string $file = WRITEPATH . 'cache/OptimizeTest_config';
32+
33+
protected function tearDown(): void
34+
{
35+
parent::tearDown();
36+
37+
if (is_file($this->file)) {
38+
unlink($this->file);
39+
}
40+
}
41+
42+
/**
43+
* @return Closure(string): void
44+
*/
45+
private function getRemoveFile(): Closure
46+
{
47+
return self::getPrivateMethodInvoker(new Optimize(service('commands')), 'removeFile');
48+
}
49+
50+
public function testRemoveFileDeletesTheFile(): void
51+
{
52+
file_put_contents($this->file, '<?php');
53+
54+
($this->getRemoveFile())($this->file);
55+
56+
$this->assertFileDoesNotExist($this->file);
57+
$this->assertStringContainsString('Removed', $this->getStreamFilterBuffer());
58+
}
59+
60+
public function testRemoveFileDoesNothingWhenFileIsAbsent(): void
61+
{
62+
$this->assertFileDoesNotExist($this->file);
63+
64+
($this->getRemoveFile())($this->file);
65+
66+
$this->assertSame('', $this->getStreamFilterBuffer());
67+
}
68+
}

0 commit comments

Comments
 (0)