Skip to content

Commit cf6360f

Browse files
committed
feat: add spark optimize command to optimize for production
1 parent 5ceaf06 commit cf6360f

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <[email protected]>
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 CodeIgniter\CLI\BaseCommand;
17+
use CodeIgniter\CLI\CLI;
18+
use CodeIgniter\Publisher\Publisher;
19+
20+
/**
21+
* Optimize for production.
22+
*/
23+
final class Optimize extends BaseCommand
24+
{
25+
/**
26+
* The group the command is lumped under
27+
* when listing commands.
28+
*
29+
* @var string
30+
*/
31+
protected $group = 'CodeIgniter';
32+
33+
/**
34+
* The Command's name
35+
*
36+
* @var string
37+
*/
38+
protected $name = 'optimize';
39+
40+
/**
41+
* The Command's short description
42+
*
43+
* @var string
44+
*/
45+
protected $description = 'Optimize for production.';
46+
47+
/**
48+
* The Command's usage
49+
*
50+
* @var string
51+
*/
52+
protected $usage = 'optimize';
53+
54+
/**
55+
* {@inheritDoc}
56+
*/
57+
public function run(array $params)
58+
{
59+
$this->enableCaching();
60+
command('cache:clear');
61+
$this->removeDevPackages();
62+
63+
return EXIT_SUCCESS;
64+
}
65+
66+
private function enableCaching(): void
67+
{
68+
$publisher = new Publisher(APPPATH, APPPATH);
69+
70+
$result = $publisher->replace(
71+
APPPATH . 'Config/Optimize.php',
72+
[
73+
'public bool $configCacheEnabled = false;' => 'public bool $configCacheEnabled = true;',
74+
'public bool $locatorCacheEnabled = false;' => 'public bool $locatorCacheEnabled = true;',
75+
]
76+
);
77+
78+
if ($result) {
79+
CLI::write(
80+
'Config Caching and FileLocator Caching are enabled in "app/Config/Optimize.php".',
81+
'green'
82+
);
83+
}
84+
}
85+
86+
private function removeDevPackages(): void
87+
{
88+
if (! defined('VENDORPATH')) {
89+
return;
90+
}
91+
92+
chdir(ROOTPATH);
93+
passthru('composer install --no-dev', $status);
94+
95+
if ($status === 0) {
96+
CLI::write('Removed Composer dev packages.', 'green');
97+
}
98+
}
99+
}

0 commit comments

Comments
 (0)