Skip to content
Open
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
53 changes: 42 additions & 11 deletions formwork/src/Commands/CacheCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@

use Formwork\Cache\FilesCache;
use Formwork\Cms\App;
use Formwork\Services\Loaders\ConfigServiceLoader;
use Formwork\Utils\Arr;
use Formwork\Utils\FileSystem;
use League\CLImate\CLImate;
use League\CLImate\Exceptions\InvalidArgumentException;
use Psr\Log\LoggerInterface;

/**
* @since 2.1.0
Expand All @@ -20,6 +23,8 @@ final class CacheCommand implements CommandInterface

private App $app;

private ?LoggerInterface $logger = null;

/**
* @var array<string, array{description: string}>
*/
Expand Down Expand Up @@ -65,6 +70,10 @@ public function __invoke(?array $argv = null): never
$this->app = App::instance();
$this->app->load();

if ($this->app->hasService(LoggerInterface::class)) {
$this->logger = $this->app->getService(LoggerInterface::class);
}

if (count($argv) < 2 || $this->climate->arguments->defined('help', $argv)) {
$this->help($argv);
exit(0);
Expand Down Expand Up @@ -103,21 +112,19 @@ public function clear(string $type, array $argv = []): void
{
switch ($type) {
case 'all':
$this->clearConfigCache();
$this->clearImagesCache();
$this->clearPagesCache();
$this->clearCaches(['pages' => true, 'images' => true, 'config' => true]);
$this->climate->green('All caches cleared.');
break;
case 'config':
$this->clearConfigCache();
$this->clearCaches(['config' => true]);
$this->climate->green('Config cache cleared.');
break;
case 'pages':
$this->clearPagesCache();
$this->clearCaches(['pages' => true]);
$this->climate->green('Pages cache cleared.');
break;
case 'images':
$this->clearImagesCache();
$this->clearCaches(['images' => true]);
$this->climate->green('Images cache cleared.');
break;
default:
Expand Down Expand Up @@ -236,13 +243,29 @@ private function pagesCacheStats(): void
}

/**
* Clear config cache
* Clear specified caches
*
* @param array<string, bool> $types
*/
private function clearConfigCache(): void
private function clearCaches(array $types): void
{
$path = ROOT_PATH . '/cache/config/';
FileSystem::delete($path, recursive: true);
FileSystem::createDirectory($path, recursive: true);
$types = Arr::filter($types, fn($clear) => $clear);

foreach (array_keys($types) as $type) {
switch ($type) {
case 'pages':
$this->clearPagesCache();
break;
case 'images':
$this->clearImagesCache();
break;
case 'config':
$this->clearConfigCache();
break;
}
}

$this->logger?->notice('Cache cleared ({types}) from CLI', ['types' => implode(', ', array_keys($types))]);
}

/**
Expand Down Expand Up @@ -271,6 +294,14 @@ private function clearPagesCache(): void
}
}

/**
* Clear config cache
*/
private function clearConfigCache(): void
{
ConfigServiceLoader::clearCache();
}

/**
* Invalidate pages cache
*/
Expand Down
70 changes: 61 additions & 9 deletions formwork/src/Panel/Controllers/CacheController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,87 @@
use Formwork\Http\JsonResponse;
use Formwork\Http\Response;
use Formwork\Router\RouteParams;
use Formwork\Services\Container;
use Formwork\Services\Loaders\ConfigServiceLoader;
use Formwork\Utils\Arr;
use Formwork\Utils\FileSystem;
use Psr\Log\LoggerInterface;

final class CacheController extends AbstractController
{
public function __construct(
private Container $container,
private AbstractCache $pagesCache,
private ?LoggerInterface $logger = null,
) {
$this->container->call(parent::__construct(...));
}

/**
* Cache@clear action
*/
public function clear(RouteParams $routeParams, AbstractCache $cache): JsonResponse|Response
public function clear(RouteParams $routeParams): JsonResponse|Response
{
if (!$this->hasPermission('panel.cache.clear')) {
return $this->forward(ErrorsController::class, 'forbidden');
}

switch ($type = $routeParams->get('type', 'default')) {
case 'default':
$this->clearPagesCache($cache);
if ($this->config->get('system.images.clearCacheByDefault')) {
$this->clearImagesCache();
}
$this->clearCaches([
'pages' => true,
'images' => $this->config->get('system.images.clearCacheByDefault'),
]);
return JsonResponse::success($this->translate('panel.cache.cleared'), data: compact('type'));
case 'all':
$this->clearCaches(['pages' => true, 'images' => true, 'config' => true]);
return JsonResponse::success($this->translate('panel.cache.cleared.all'), data: compact('type'));
case 'pages':
$this->clearPagesCache($cache);
$this->clearCaches(['pages' => true]);
return JsonResponse::success($this->translate('panel.cache.cleared.pages'), data: compact('type'));
case 'images':
$this->clearImagesCache();
$this->clearCaches(['images' => true]);
return JsonResponse::success($this->translate('panel.cache.cleared.images'), data: compact('type'));
case 'config':
$this->clearCaches(['config' => true]);
return JsonResponse::success($this->translate('panel.cache.cleared.config'), data: compact('type'));
default:
return JsonResponse::error($this->translate('panel.cache.error'));
}
}

/**
* Clear specified caches
*
* @param array<string, bool> $types
*/
private function clearCaches(array $types): void
{
$types = Arr::filter($types, fn($clear) => $clear);

foreach (array_keys($types) as $type) {
switch ($type) {
case 'pages':
$this->clearPagesCache();
break;
case 'images':
$this->clearImagesCache();
break;
case 'config':
$this->clearConfigCache();
break;
}
}

$this->logger?->notice('Cache cleared ({types}) by user {user}', ['types' => implode(', ', array_keys($types)), 'user' => $this->panel->user()->username()]);
}

/**
* Clear pages cache
*/
private function clearPagesCache(AbstractCache $cache): void
private function clearPagesCache(): void
{
$cache->clear();
$this->pagesCache->clear();
if ($this->site->contentPath() !== null) {
FileSystem::touch($this->site->contentPath());
}
Expand All @@ -57,4 +101,12 @@ private function clearImagesCache(): void
FileSystem::delete($path, recursive: true);
FileSystem::createDirectory($path, recursive: true);
}

/**
* Clear config cache
*/
private function clearConfigCache(): void
{
ConfigServiceLoader::clearCache();
}
}
23 changes: 19 additions & 4 deletions formwork/src/Services/Loaders/ConfigServiceLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@

final class ConfigServiceLoader implements ServiceLoaderInterface
{
/**
* Config cache path
*/
private const string CACHE_PATH = ROOT_PATH . '/cache/config/';

public function __construct(
private Request $request,
) {}

public function load(Container $container): Config
{
$cachePath = ROOT_PATH . '/cache/config/';
$cacheFile = FileSystem::joinPaths($cachePath, "config.{$this->request->host()}.php");
$cacheFile = FileSystem::joinPaths(self::CACHE_PATH, "config.{$this->request->host()}.php");

if (!FileSystem::isDirectory($cachePath, assertExists: false)) {
FileSystem::createDirectory($cachePath, recursive: true);
if (!FileSystem::isDirectory(self::CACHE_PATH, assertExists: false)) {
FileSystem::createDirectory(self::CACHE_PATH, recursive: true);
}

if (
Expand Down Expand Up @@ -57,4 +61,15 @@ public function load(Container $container): Config

return $config;
}

/**
* Clear config cache
*
* @internal
*/
public static function clearCache(): void
{
FileSystem::delete(self::CACHE_PATH, recursive: true);
FileSystem::createDirectory(self::CACHE_PATH, recursive: true);
}
}
11 changes: 11 additions & 0 deletions panel/src/ts/components/views/dashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export class Dashboard {
const clearCacheCommand = $("[data-view=dashboard] [data-command=clear-cache]");
const clearPagesCacheCommand = $("[data-view=dashboard] [data-command=clear-pages-cache]");
const clearImagesCacheCommand = $("[data-view=dashboard] [data-command=clear-images-cache]");
const clearConfigCacheCommand = $("[data-view=dashboard] [data-command=clear-config-cache]");
const clearAllCacheCommand = $("[data-view=dashboard] [data-command=clear-all-cache]");

const chart = $(".dashboard-chart");

const clearCache = (type?: string) => {
Expand Down Expand Up @@ -38,6 +41,14 @@ export class Dashboard {
clearImagesCacheCommand.addEventListener("click", () => clearCache("images"));
}

if (clearConfigCacheCommand) {
clearConfigCacheCommand.addEventListener("click", () => clearCache("config"));
}

if (clearAllCacheCommand) {
clearAllCacheCommand.addEventListener("click", () => clearCache("all"));
}

if (chart) {
const chartData = chart.dataset.chartData;
if (chartData) {
Expand Down
4 changes: 4 additions & 0 deletions panel/translations/de.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ panel.backup.error.cannotDownload.invalidFilename: Ungültige Sicherungsdatei
panel.backup.error.cannotMake: Sicherung kann nicht erstellt werden. %s.
panel.backup.ready: Sicherung bereit. Download wird gestartet...
panel.cache.clear: Cache leeren
panel.cache.clear.all: Alle Caches löschen
panel.cache.clear.config: Konfigurations-Cache löschen
panel.cache.clear.images: Bilder-Cache löschen
panel.cache.clear.pages: Seiten-Cache löschen
panel.cache.cleared: Cache geleert
panel.cache.cleared.all: Alle Caches wurden geleert
panel.cache.cleared.config: Konfigurations-Cache wurde geleert
panel.cache.cleared.images: Bilder-Cache geleert
panel.cache.cleared.pages: Seiten-Cache geleert
panel.cache.error: Cache kann nicht gelöscht werden
Expand Down
4 changes: 4 additions & 0 deletions panel/translations/el.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ panel.backup.error.cannotDownload.invalidFilename: Μη έγκυρο αρχεί
panel.backup.error.cannotMake: Δεν είναι δυνατή η δημιουργία αντιγράφου ασφαλείας. %s.
panel.backup.ready: Το αντίγραφο ασφαλείας είναι έτοιμο. Ξεκινά η λήψη...
panel.cache.clear: Εκκαθάριση cache
panel.cache.clear.all: Εκκαθάριση όλων των cache
panel.cache.clear.config: Εκκαθάριση cache ρυθμίσεων
panel.cache.clear.images: Εκκαθάριση cache εικόνων
panel.cache.clear.pages: Εκκαθάριση cache σελίδων
panel.cache.cleared: Η cache εκκαθαρίστηκε
panel.cache.cleared.all: Όλες οι cache εκκαθαρίστηκαν
panel.cache.cleared.config: Η cache ρυθμίσεων εκκαθαρίστηκε
panel.cache.cleared.images: Η cache εικόνων εκκαθαρίστηκε
panel.cache.cleared.pages: Η cache σελίδων εκκαθαρίστηκε
panel.cache.error: Δεν είναι δυνατή η εκκαθάριση της cache
Expand Down
4 changes: 4 additions & 0 deletions panel/translations/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ panel.backup.error.cannotDownload.invalidFilename: Invalid backup file
panel.backup.error.cannotMake: Cannot make backup. %s.
panel.backup.ready: Backup ready. Starting download...
panel.cache.clear: Clear cache
panel.cache.clear.all: Clear all caches
panel.cache.clear.config: Clear config cache
panel.cache.clear.images: Clear images cache
panel.cache.clear.pages: Clear pages cache
panel.cache.cleared: Cache cleared
panel.cache.cleared.all: All caches cleared
panel.cache.cleared.config: Config cache cleared
panel.cache.cleared.images: Images cache cleared
panel.cache.cleared.pages: Pages cache cleared
panel.cache.error: Cannot clear cache
Expand Down
4 changes: 4 additions & 0 deletions panel/translations/es.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ panel.backup.error.cannotDownload.invalidFilename: Archivo de respaldo inválido
panel.backup.error.cannotMake: No se puede crear el respaldo. %s.
panel.backup.ready: Respaldo listo. Iniciando descarga...
panel.cache.clear: Limpiar caché
panel.cache.clear.all: Limpiar todas las cachés
panel.cache.clear.config: Limpiar caché de configuración
panel.cache.clear.images: Limpiar caché de imágenes
panel.cache.clear.pages: Limpiar caché de páginas
panel.cache.cleared: Caché limpiada
panel.cache.cleared.all: Todas las cachés limpiadas
panel.cache.cleared.config: Caché de configuración limpiada
panel.cache.cleared.images: Caché de imágenes limpiada
panel.cache.cleared.pages: Caché de páginas limpiada
panel.cache.error: No se puede limpiar la caché
Expand Down
4 changes: 4 additions & 0 deletions panel/translations/fr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ panel.backup.error.cannotDownload.invalidFilename: Fichier de sauvegarde invalid
panel.backup.error.cannotMake: Impossible de faire une sauvegarde. %s.
panel.backup.ready: Sauvegarde prête. Démarrage du téléchargement…
panel.cache.clear: Vider le cache
panel.cache.clear.all: Vider tous les caches
panel.cache.clear.config: Vider le cache de configuration
panel.cache.clear.images: Vider le cache des images
panel.cache.clear.pages: Vider le cache des pages
panel.cache.cleared: Cache effacé
panel.cache.cleared.all: Tous les caches effacés
panel.cache.cleared.config: Cache de configuration effacé
panel.cache.cleared.images: Cache des images effacé
panel.cache.cleared.pages: Cache des pages effacé
panel.cache.error: Impossible de vider le cache
Expand Down
4 changes: 4 additions & 0 deletions panel/translations/hu.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ panel.backup.error.cannotDownload.invalidFilename: Érvénytelen biztonsági men
panel.backup.error.cannotMake: Nem lehet biztonsági mentést készíteni. %s.
panel.backup.ready: A biztonsági mentés elkészült. Letöltés indítása...
panel.cache.clear: Gyorsítótár törlése
panel.cache.clear.all: Az összes gyorsítótár törlése
panel.cache.clear.config: A konfigurációs gyorsítótár törlése
panel.cache.clear.images: Képek gyorsítótárának törlése
panel.cache.clear.pages: Oldalak gyorsítótárának törlése
panel.cache.cleared: Gyorsítótár törölve
panel.cache.cleared.all: Az összes gyorsítótár törölve
panel.cache.cleared.config: A konfigurációs gyorsítótár törölve
panel.cache.cleared.images: Képek gyorsítótára törölve
panel.cache.cleared.pages: Oldalak gyorsítótára törölve
panel.cache.error: Nem lehet törölni a gyorsítótárat
Expand Down
4 changes: 4 additions & 0 deletions panel/translations/it.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ panel.backup.error.cannotDownload.invalidFilename: File di backup non valido.
panel.backup.error.cannotMake: Impossibile eseguire il backup. %s.
panel.backup.ready: Backup pronto. Inizio a scaricare...
panel.cache.clear: Svuota cache
panel.cache.clear.all: Svuota tutte le cache
panel.cache.clear.config: Svuota cache configurazione
panel.cache.clear.images: Svuota cache immagini
panel.cache.clear.pages: Svuota cache pagine
panel.cache.cleared: Cache svuotata
panel.cache.cleared.all: Tutte le cache svuotate
panel.cache.cleared.config: Cache configurazione svuotata
panel.cache.cleared.images: Cache immagini svuotata
panel.cache.cleared.pages: Cache pagine svuotata
panel.cache.error: Impossibile svuotare la cache
Expand Down
4 changes: 4 additions & 0 deletions panel/translations/nl.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ panel.backup.error.cannotDownload.invalidFilename: Ongeldig back-upbestand
panel.backup.error.cannotMake: Kan backup niet aanmaken. %s.
panel.backup.ready: Backup gereed. Start downloaden...
panel.cache.clear: Cache legen
panel.cache.clear.all: Alle caches legen
panel.cache.clear.config: Configuratiecache legen
panel.cache.clear.images: Cache voor afbeeldingen legen
panel.cache.clear.pages: Cache voor pagina's legen
panel.cache.cleared: Cache geleegd
panel.cache.cleared.all: Alle caches geleegd
panel.cache.cleared.config: Configuratiecache geleegd
panel.cache.cleared.images: Afbeeldingscache geleegd
panel.cache.cleared.pages: Pagina-cache geleegd
panel.cache.error: Kan cache niet legen
Expand Down
Loading