Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions spec/support/application/app/Config/optimize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

/**
* This file is part of Blitz PHP framework.
*
* (c) 2022 Dimitri Sitchet Tomkeu <devcode.dst@gmail.com>
*
* 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,
];
135 changes: 135 additions & 0 deletions src/Cli/Commands/Utilities/Optimize.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

/**
* This file is part of Blitz PHP framework.
*
* (c) 2022 Dimitri Sitchet Tomkeu <devcode.dst@gmail.com>
*
* 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__);
}
}
17 changes: 17 additions & 0 deletions src/Constants/schemas/optimize.config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/**
* This file is part of Blitz PHP framework.
*
* (c) 2022 Dimitri Sitchet Tomkeu <devcode.dst@gmail.com>
*
* 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),
]);
15 changes: 12 additions & 3 deletions src/Container/Services.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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];
}

Expand Down
Loading