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, +]; diff --git a/src/Cli/Commands/Utilities/Optimize.php b/src/Cli/Commands/Utilities/Optimize.php new file mode 100644 index 00000000..2ef067cd --- /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..4cf7e7f5 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]; }