1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Blitz PHP framework. |
5
|
|
|
* |
6
|
|
|
* (c) 2022 Dimitri Sitchet Tomkeu <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace BlitzPHP\Cli\Commands\Utilities; |
13
|
|
|
|
14
|
|
|
use BlitzPHP\Autoloader\Locator; |
15
|
|
|
use BlitzPHP\Autoloader\LocatorCached; |
16
|
|
|
use BlitzPHP\Cache\Handlers\FileVarExportHandler; |
17
|
|
|
use BlitzPHP\Cli\Console\Command; |
18
|
|
|
use BlitzPHP\Publisher\Publisher; |
19
|
|
|
use RuntimeException; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Optimisation pour la production. |
23
|
|
|
*/ |
24
|
|
|
final class Optimize extends Command |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var string Groupe |
28
|
|
|
*/ |
29
|
|
|
protected $group = 'BlitzPHP'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string Nom |
33
|
|
|
*/ |
34
|
|
|
protected $name = 'optimize'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string Description |
38
|
|
|
*/ |
39
|
|
|
protected $description = 'Optimise l\'application pour la production.'; |
40
|
|
|
|
41
|
|
|
protected $service = 'Service de configuration'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritDoc} |
45
|
|
|
*/ |
46
|
|
|
public function execute(array $params) |
47
|
|
|
{ |
48
|
|
|
try { |
49
|
|
|
$this->enableCaching(); |
50
|
|
|
$this->clearCache(); |
51
|
|
|
$this->removeDevPackages(); |
52
|
|
|
} catch (RuntimeException) { |
53
|
|
|
$this->fail('La commande "klinge optimize" a échouée.')->eol(); |
54
|
|
|
|
55
|
|
|
return EXIT_ERROR; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return EXIT_SUCCESS; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private function clearCache(): void |
62
|
|
|
{ |
63
|
|
|
$locator = new LocatorCached(new Locator(service('autoloader')), new FileVarExportHandler()); |
64
|
|
|
|
65
|
|
|
$locator->deleteCache(); |
66
|
|
|
|
67
|
|
|
$this->ok('Suppression de FileLocatorCache.')->eol(); |
68
|
|
|
|
69
|
|
|
$this->removeFile(FRAMEWORK_STORAGE_PATH . 'cache/FactoriesCache_config'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private function removeFile(string $cache): void |
73
|
|
|
{ |
74
|
|
|
if (is_file($cache)) { |
75
|
|
|
$result = unlink($cache); |
76
|
|
|
|
77
|
|
|
if ($result) { |
78
|
|
|
$this->ok('"' . clean_path($cache) . '" supprimé.')->eol(); |
79
|
|
|
|
80
|
|
|
return; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$this->fail('Erreur lors de la suppression du fichier: ' . clean_path($cache)); |
84
|
|
|
|
85
|
|
|
throw new RuntimeException(__METHOD__); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private function enableCaching(): void |
90
|
|
|
{ |
91
|
|
|
$publisher = new Publisher(APP_PATH, APP_PATH); |
92
|
|
|
|
93
|
|
|
$config = APP_PATH . 'Config/optimize.php'; |
94
|
|
|
|
95
|
|
|
$result = $publisher->replace( |
96
|
|
|
$config, |
97
|
|
|
[ |
98
|
|
|
"'config_cache_enabled' => false," => "'config_cache_enabled' => true,", |
99
|
|
|
"'locator_cache_enabled' => false," => "'locator_cache_enabled' => true,", |
100
|
|
|
] |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
if ($result) { |
104
|
|
|
$this->ok( |
105
|
|
|
'Les options Config Caching et FileLocator Caching sont activées dans "app/Config/optimize.php".', |
106
|
|
|
)->eol(); |
107
|
|
|
|
108
|
|
|
return; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$this->fail('Erreur dans la mise à jour du fichier: ' . clean_path($config))->eol(); |
112
|
|
|
|
113
|
|
|
throw new RuntimeException(__METHOD__); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
private function removeDevPackages(): void |
117
|
|
|
{ |
118
|
|
|
if (! defined('VENDOR_PATH')) { |
119
|
|
|
return; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
chdir(ROOTPATH); |
123
|
|
|
passthru('composer install --no-dev', $status); |
124
|
|
|
|
125
|
|
|
if ($status === 0) { |
126
|
|
|
$this->ok('Suppression des paquets de développement Composer.')->eol(); |
127
|
|
|
|
128
|
|
|
return; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$this->fail('Erreur lors de la suppression des paquets de développement Composer.')->eol(); |
132
|
|
|
|
133
|
|
|
throw new RuntimeException(__METHOD__); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|