From fef795de46150e3845390cd0e974e43cd10a9380 Mon Sep 17 00:00:00 2001 From: Dimitri Sitchet Tomkeu Date: Mon, 16 Dec 2024 16:09:43 +0100 Subject: [PATCH 1/3] feat: ajout de la commande `klinge optimize` --- src/Cli/Commands/Utilities/Optimize.php | 135 ++++++++++++++++++++++ src/Constants/schemas/optimize.config.php | 17 +++ src/Container/Services.php | 15 ++- 3 files changed, 164 insertions(+), 3 deletions(-) create mode 100644 src/Cli/Commands/Utilities/Optimize.php create mode 100644 src/Constants/schemas/optimize.config.php diff --git a/src/Cli/Commands/Utilities/Optimize.php b/src/Cli/Commands/Utilities/Optimize.php new file mode 100644 index 00000000..bd1246a4 --- /dev/null +++ b/src/Cli/Commands/Utilities/Optimize.php @@ -0,0 +1,135 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace BlitzPHP\Cli\Commands\Utilities; + +use BlitzPHP\Autoloader\Locator; +use BlitzPHP\Autoloader\LocatorCached; +use BlitzPHP\Cache\Handlers\FileVarExportHandler; +use BlitzPHP\Cli\Console\Command; +use BlitzPHP\Publisher\Publisher; +use RuntimeException; + +/** + * Optimisation pour la production. + */ +final class Optimize extends Command +{ + /** + * @var string Groupe + */ + protected $group = 'BlitzPHP'; + + /** + * @var string Nom + */ + protected $name = 'optimize'; + + /** + * @var string Description + */ + protected $description = 'Optimise l\'application pour la production.'; + + protected $service = 'Service de configuration'; + + /** + * {@inheritDoc} + */ + public function execute(array $params) + { + try { + $this->enableCaching(); + $this->clearCache(); + $this->removeDevPackages(); + } catch (RuntimeException) { + $this->fail('La commande "klinge optimize" a échouée.')->eol(); + + return EXIT_ERROR; + } + + return EXIT_SUCCESS; + } + + private function clearCache(): void + { + $locator = new LocatorCached(new Locator(service('autoloader')), new FileVarExportHandler()); + + $locator->deleteCache(); + + $this->ok('Suppression de FileLocatorCache.')->eol(); + + $this->removeFile(FRAMEWORK_STORAGE_PATH . 'cache/FactoriesCache_config'); + } + + private function removeFile(string $cache): void + { + if (is_file($cache)) { + $result = unlink($cache); + + if ($result) { + $this->ok('"' . clean_path($cache) . '" supprimé.')->eol(); + + return; + } + + $this->fail('Erreur lors de la suppression du fichier: ' . clean_path($cache)); + + throw new RuntimeException(__METHOD__); + } + } + + private function enableCaching(): void + { + $publisher = new Publisher(APP_PATH, APP_PATH); + + $config = APP_PATH . 'Config/optimize.php'; + + $result = $publisher->replace( + $config, + [ + "'config_cache_enabled' => false," => "'config_cache_enabled' => true,", + "'locator_cache_enabled' => false," => "'locator_cache_enabled' => true,", + ] + ); + + if ($result) { + $this->ok( + 'Les options Config Caching et FileLocator Caching sont activées dans "app/Config/optimize.php".', + )->eol(); + + return; + } + + $this->fail('Erreur dans la mise à jour du fichier: ' . clean_path($config))->eol(); + + throw new RuntimeException(__METHOD__); + } + + private function removeDevPackages(): void + { + if (! defined('VENDOR_PATH')) { + return; + } + + chdir(ROOTPATH); + passthru('composer install --no-dev', $status); + + if ($status === 0) { + $this->ok('Suppression des paquets de développement Composer.')->eol(); + + return; + } + + $this->fail('Erreur lors de la suppression des paquets de développement Composer.')->eol(); + + throw new RuntimeException(__METHOD__); + } +} diff --git a/src/Constants/schemas/optimize.config.php b/src/Constants/schemas/optimize.config.php new file mode 100644 index 00000000..1c8cf299 --- /dev/null +++ b/src/Constants/schemas/optimize.config.php @@ -0,0 +1,17 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +use Nette\Schema\Expect; + +return Expect::structure([ + 'config_cache_enabled' => Expect::bool()->default(false), + 'locator_cache_enabled' => Expect::bool()->default(false), +]); diff --git a/src/Container/Services.php b/src/Container/Services.php index c2785f05..614c4ceb 100644 --- a/src/Container/Services.php +++ b/src/Container/Services.php @@ -13,7 +13,9 @@ use BlitzPHP\Autoloader\Autoloader; use BlitzPHP\Autoloader\Locator; +use BlitzPHP\Autoloader\LocatorCached; use BlitzPHP\Cache\Cache; +use BlitzPHP\Cache\Handlers\FileVarExportHandler; use BlitzPHP\Cache\ResponseCache; use BlitzPHP\Config\Config; use BlitzPHP\Contracts\Autoloader\LocatorInterface; @@ -275,12 +277,19 @@ public static function language(?string $locale = null, bool $shared = true): Tr /** * Le file locator fournit des methodes utilitaire pour chercher les fichiers non-classes dans les dossiers de namespace. * C'est une excelente methode pour charger les 'vues', 'helpers', et 'libraries'. - * - * @return Locator */ public static function locator(bool $shared = true): LocatorInterface { - if ($shared && isset(static::$instances[Locator::class])) { + if ($shared) { + if (!isset(static::$instances[Locator::class])) { + $locator = new Locator(static::autoloader()); + if (true === config('optimize.locator_cache_enabled', false)) { + static::$instances[Locator::class] = new LocatorCached($locator, new FileVarExportHandler()); + } else { + static::$instances[Locator::class] = $locator; + } + } + return static::$instances[Locator::class]; } From a58c98def7f85f745e3773df53748062ad977cc9 Mon Sep 17 00:00:00 2001 From: dimtrovich Date: Mon, 16 Dec 2024 15:15:47 +0000 Subject: [PATCH 2/3] Fix styling --- src/Cli/Commands/Utilities/Optimize.php | 6 +++--- src/Container/Services.php | 14 +++++++------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Cli/Commands/Utilities/Optimize.php b/src/Cli/Commands/Utilities/Optimize.php index bd1246a4..2ef067cd 100644 --- a/src/Cli/Commands/Utilities/Optimize.php +++ b/src/Cli/Commands/Utilities/Optimize.php @@ -58,13 +58,13 @@ public function execute(array $params) return EXIT_SUCCESS; } - private function clearCache(): void + private function clearCache(): void { - $locator = new LocatorCached(new Locator(service('autoloader')), new FileVarExportHandler()); + $locator = new LocatorCached(new Locator(service('autoloader')), new FileVarExportHandler()); $locator->deleteCache(); - $this->ok('Suppression de FileLocatorCache.')->eol(); + $this->ok('Suppression de FileLocatorCache.')->eol(); $this->removeFile(FRAMEWORK_STORAGE_PATH . 'cache/FactoriesCache_config'); } diff --git a/src/Container/Services.php b/src/Container/Services.php index 614c4ceb..4cf7e7f5 100644 --- a/src/Container/Services.php +++ b/src/Container/Services.php @@ -281,13 +281,13 @@ public static function language(?string $locale = null, bool $shared = true): Tr public static function locator(bool $shared = true): LocatorInterface { if ($shared) { - if (!isset(static::$instances[Locator::class])) { - $locator = new Locator(static::autoloader()); - if (true === config('optimize.locator_cache_enabled', false)) { - static::$instances[Locator::class] = new LocatorCached($locator, new FileVarExportHandler()); - } else { - static::$instances[Locator::class] = $locator; - } + if (! isset(static::$instances[Locator::class])) { + $locator = new Locator(static::autoloader()); + if (true === config('optimize.locator_cache_enabled', false)) { + static::$instances[Locator::class] = new LocatorCached($locator, new FileVarExportHandler()); + } else { + static::$instances[Locator::class] = $locator; + } } return static::$instances[Locator::class]; From d69c68b78c6ebccaefd99d51822fc79c4f7d8d54 Mon Sep 17 00:00:00 2001 From: Dimitri Sitchet Tomkeu Date: Mon, 16 Dec 2024 16:51:24 +0100 Subject: [PATCH 3/3] fix(tests): ajout de la config de l'optimisation dans l'environnement de test --- spec/support/application/app/Config/optimize.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 spec/support/application/app/Config/optimize.php diff --git a/spec/support/application/app/Config/optimize.php b/spec/support/application/app/Config/optimize.php new file mode 100644 index 00000000..390231a7 --- /dev/null +++ b/spec/support/application/app/Config/optimize.php @@ -0,0 +1,15 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +return [ + 'config_cache_enabled' => false, + 'locator_cache_enabled' => false, +];