Skip to content

Commit 301ffb8

Browse files
authored
Merge pull request #8610 from kenjis/feat-add-spark-optimize
feat: add `spark optimize` command
2 parents e131c46 + 46e3348 commit 301ffb8

File tree

6 files changed

+175
-0
lines changed

6 files changed

+175
-0
lines changed

system/Autoloader/FileLocatorCached.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ private function saveCache(): void
8585
*/
8686
public function deleteCache(): void
8787
{
88+
$this->cacheUpdated = false;
8889
$this->cacheHandler->delete($this->cacheKey);
8990
}
9091

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
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\Autoloader\FileLocator;
17+
use CodeIgniter\Autoloader\FileLocatorCached;
18+
use CodeIgniter\CLI\BaseCommand;
19+
use CodeIgniter\CLI\CLI;
20+
use CodeIgniter\Publisher\Publisher;
21+
use RuntimeException;
22+
23+
/**
24+
* Optimize for production.
25+
*/
26+
final class Optimize extends BaseCommand
27+
{
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+
* {@inheritDoc}
59+
*/
60+
public function run(array $params)
61+
{
62+
try {
63+
$this->enableCaching();
64+
$this->clearCache();
65+
$this->removeDevPackages();
66+
} catch (RuntimeException) {
67+
CLI::error('The "spark optimize" failed.');
68+
69+
return EXIT_ERROR;
70+
}
71+
72+
return EXIT_SUCCESS;
73+
}
74+
75+
private function clearCache(): void
76+
{
77+
$locator = new FileLocatorCached(new FileLocator(service('autoloader')));
78+
$locator->deleteCache();
79+
CLI::write('Removed FileLocatorCache.', 'green');
80+
81+
$cache = WRITEPATH . 'cache/FactoriesCache_config';
82+
$this->removeFile($cache);
83+
}
84+
85+
private function removeFile(string $cache): void
86+
{
87+
if (is_file($cache)) {
88+
$result = unlink($cache);
89+
90+
if ($result) {
91+
CLI::write('Removed "' . clean_path($cache) . '".', 'green');
92+
93+
return;
94+
}
95+
96+
CLI::error('Error in removing file: ' . clean_path($cache));
97+
98+
throw new RuntimeException(__METHOD__);
99+
}
100+
}
101+
102+
private function enableCaching(): void
103+
{
104+
$publisher = new Publisher(APPPATH, APPPATH);
105+
106+
$config = APPPATH . 'Config/Optimize.php';
107+
108+
$result = $publisher->replace(
109+
$config,
110+
[
111+
'public bool $configCacheEnabled = false;' => 'public bool $configCacheEnabled = true;',
112+
'public bool $locatorCacheEnabled = false;' => 'public bool $locatorCacheEnabled = true;',
113+
]
114+
);
115+
116+
if ($result) {
117+
CLI::write(
118+
'Config Caching and FileLocator Caching are enabled in "app/Config/Optimize.php".',
119+
'green'
120+
);
121+
122+
return;
123+
}
124+
125+
CLI::error('Error in updating file: ' . clean_path($config));
126+
127+
throw new RuntimeException(__METHOD__);
128+
}
129+
130+
private function removeDevPackages(): void
131+
{
132+
if (! defined('VENDORPATH')) {
133+
return;
134+
}
135+
136+
chdir(ROOTPATH);
137+
passthru('composer install --no-dev', $status);
138+
139+
if ($status === 0) {
140+
CLI::write('Removed Composer dev packages.', 'green');
141+
142+
return;
143+
}
144+
145+
CLI::error('Error in removing Composer dev packages.');
146+
147+
throw new RuntimeException(__METHOD__);
148+
}
149+
}

user_guide_src/source/changelogs/v4.5.0.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Enhancements
2424
Commands
2525
========
2626

27+
- Added ``spark optimize`` command to optimize configuration for production environment.
28+
See :ref:`spark_optimize` for the details.
2729
- Added ``spark make:test`` command to generate a skeleton test file. See
2830
:ref:`cli-generators-make-test` for the details.
2931
- Added ``spark config:check`` command to check Config values. See

user_guide_src/source/concepts/autoloader.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,18 @@ You can use the ``spark cache:clear`` command:
187187
188188
Or simply delete the **writable/cache/FileLocatorCache** file.
189189

190+
.. note::
191+
The ``spark optimize`` command clears the cache.
192+
190193
How to Enable FileLocator Caching
191194
=================================
192195

193196
Set the following property to ``true`` in **app/Config/Optimize.php**::
194197

195198
public bool $locatorCacheEnabled = true;
196199

200+
Or you can enable it with the ``spark optimize`` command.
201+
197202
.. note::
198203
This property cannot be overridden by
199204
:ref:`environment variables <configuration-classes-and-environment-variables>`.

user_guide_src/source/concepts/factories.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,9 @@ You can use the ``spark cache:clear`` command:
313313
314314
Or simply delete the **writable/cache/FactoriesCache_config** file.
315315

316+
.. note::
317+
Since v4.5.0, the ``spark optimize`` command clears the cache.
318+
316319
How to Enable Config Caching
317320
============================
318321

@@ -322,6 +325,8 @@ Set the following property to ``true`` in **app/Config/Optimize.php**::
322325

323326
public bool $configCacheEnabled = true;
324327

328+
Since v4.5.0, you can enable this with the ``spark optimize`` command.
329+
325330
.. note::
326331
This property cannot be overridden by
327332
:ref:`environment variables <configuration-classes-and-environment-variables>`.

user_guide_src/source/installation/deployment.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ things you can do to make your application run more efficiently.
1515

1616
This section describes the optimization features that CodeIgniter provides.
1717

18+
.. _spark_optimize:
19+
20+
spark optimize
21+
==============
22+
23+
.. versionadded:: 4.5.0
24+
25+
The ``spark optimize`` command performs the following optimizations:
26+
27+
- `Removing Dev Packages`_
28+
- Enabling `Config Caching`_
29+
- Enabling `FileLocator Caching`_
30+
1831
Composer Optimization
1932
=====================
2033

0 commit comments

Comments
 (0)