diff --git a/CHANGELOG.md b/CHANGELOG.md index 2009671dd..dc22905e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ The format is based on Keep a Changelog. - The `DemoWeb` and `Toolkit` module templates redirect back with `back()` instead of `redirect(get_referrer() ?? base_url())` and `redirectWith(get_referrer() ?? base_url(), $data)`, so generated code gets the same-origin check. Modules generated before keep their code (#541) - Twig is a required dependency again: `twig/twig` `^3.27` is in `require` and no longer only suggested, which reverses the 3.0.3 change that made it optional while the framework supported PHP 8.0 (#532). Twig `^3.27` needs PHP 8.1, the minimum since 3.1.0. An application that pins an older Twig must allow `^3.27`. Removed `RendererException::twigNotInstalled()` and the check in `TwigAdapter` that threw it when Twig was missing, and the Twig tests no longer skip (#626) - The test suite runs on PHPUnit `^10.5` (a development dependency) and `phpunit.xml` uses its schema. It now sets `error_reporting` to `-1`: it was the text `E_ALL & ~E_DEPRECATED`, which PHP reads as `0`, so every PHP warning was hidden in the tests (#622) +- `EnvCommand::exec()`, `Environment`, `Config` and `ViewCache` read the base directory once where they asked for it several times, and `LoadHelpersStage` builds the module helpers path with `modules_dir()`. No behavior change (#633) ### Fixed - `php qt core:env` no longer prints `.env is successfully generated` and no longer tries to copy the file when `.env.example` is missing: it stops after the error message (#622) diff --git a/src/App/Stages/Boot/LoadHelpersStage.php b/src/App/Stages/Boot/LoadHelpersStage.php index f1c246f91..837d121a9 100644 --- a/src/App/Stages/Boot/LoadHelpersStage.php +++ b/src/App/Stages/Boot/LoadHelpersStage.php @@ -49,7 +49,7 @@ private function loadAppHelpers(): void private function loadModuleHelpers(): void { - $this->loadHelperFiles(App::getBaseDir() . DS . 'modules' . DS . '*' . DS . 'helpers'); + $this->loadHelperFiles(modules_dir() . DS . '*' . DS . 'helpers'); } private function loadHelperFiles(string $dir): void diff --git a/src/Config/Config.php b/src/Config/Config.php index 346fd70c0..84cf358b6 100644 --- a/src/Config/Config.php +++ b/src/Config/Config.php @@ -145,7 +145,9 @@ private function loadConfig(Setup $setup): array private function resolveFilePath(Setup $setup): ?string { - $filePath = App::getBaseDir() . DS; + $baseDir = App::getBaseDir(); + + $filePath = $baseDir . DS; if ($setup->getModule()) { $filePath = modules_dir() . DS . $setup->getModule() . DS; @@ -162,7 +164,7 @@ private function resolveFilePath(Setup $setup): ?string } if ($setup->getHierarchy()) { - $filePath = App::getBaseDir() . DS . 'shared' . DS + $filePath = $baseDir . DS . 'shared' . DS . strtolower($setup->getPathPrefix() ?? '') . DS . $setup->getFilename() . '.php'; diff --git a/src/Console/Commands/EnvCommand.php b/src/Console/Commands/EnvCommand.php index 5c7194ba3..87889deba 100644 --- a/src/Console/Commands/EnvCommand.php +++ b/src/Console/Commands/EnvCommand.php @@ -62,19 +62,21 @@ public function exec(): void { $fs = FileSystemFactory::get(); - if (!$fs->exists(App::getBaseDir() . DS . '.env.example')) { + $baseDir = App::getBaseDir(); + + if (!$fs->exists($baseDir . DS . '.env.example')) { $this->error('.env.example file not found'); return; } - if ($fs->exists(App::getBaseDir() . DS . '.env') && !$this->getOption('yes') && !$this->confirm('The operation will overwrite values of the existing .env and will create new one from .env.example. Continue?')) { + if ($fs->exists($baseDir . DS . '.env') && !$this->getOption('yes') && !$this->confirm('The operation will overwrite values of the existing .env and will create new one from .env.example. Continue?')) { $this->info('Operation was canceled!'); return; } $fs->copy( - App::getBaseDir() . DS . '.env.example', - App::getBaseDir() . DS . '.env' + $baseDir . DS . '.env.example', + $baseDir . DS . '.env' ); $this->info('.env is successfully generated'); diff --git a/src/Environment/Environment.php b/src/Environment/Environment.php index 887e28de6..400205436 100644 --- a/src/Environment/Environment.php +++ b/src/Environment/Environment.php @@ -173,7 +173,9 @@ public function updateRow(string $key, ?string $value): void */ private function resolveEnvironmentFile(): array { - $filePath = base_dir() . DS . 'shared' . DS . 'config' . DS . 'env.php'; + $baseDir = App::getBaseDir(); + + $filePath = $baseDir . DS . 'shared' . DS . 'config' . DS . 'env.php'; if (!file_exists($filePath)) { throw EnvException::fileNotFound($filePath); @@ -183,7 +185,7 @@ private function resolveEnvironmentFile(): array $appEnv = $envConfig['app_env'] ?? Env::PRODUCTION; $envFile = $this->getEnvFileName($appEnv); - if (!file_exists(App::getBaseDir() . DS . $envFile)) { + if (!file_exists($baseDir . DS . $envFile)) { throw EnvException::fileNotFound($envFile); } diff --git a/src/ResourceCache/ViewCache.php b/src/ResourceCache/ViewCache.php index 3bc54e903..7cb958980 100644 --- a/src/ResourceCache/ViewCache.php +++ b/src/ResourceCache/ViewCache.php @@ -176,7 +176,7 @@ private function getCacheDir(): string $viewCacheDir = base_dir() . DS . $configCacheDir . DS . 'views'; if ($module = request()->getCurrentModule()) { - $viewCacheDir = base_dir() . DS . $configCacheDir . DS . 'views' . DS . strtolower($module); + $viewCacheDir .= DS . strtolower($module); } return $viewCacheDir; diff --git a/tests/Unit/ResourceCache/ViewCacheTest.php b/tests/Unit/ResourceCache/ViewCacheTest.php index 91ba84b3f..8dff53d8b 100644 --- a/tests/Unit/ResourceCache/ViewCacheTest.php +++ b/tests/Unit/ResourceCache/ViewCacheTest.php @@ -5,6 +5,8 @@ use Quantum\ResourceCache\Exceptions\ResourceCacheException; use Quantum\ResourceCache\ViewCache; use Quantum\Tests\Unit\AppTestCase; +use Quantum\Router\MatchedRoute; +use Quantum\Router\Route; use Quantum\Di\Di; class ViewCacheDouble extends ViewCache @@ -154,4 +156,25 @@ public function testEnableDisableViewCache(): void $this->assertFalse($this->viewCache->isEnabled()); } + public function testCacheDirIsTheViewsDirWhenThereIsNoModule(): void + { + $this->assertSame( + base_dir() . DS . 'cache' . DS . 'views', + $this->invokePrivateMethod($this->viewCache, 'getCacheDir') + ); + } + + public function testCacheDirHasTheLowercaseModuleNameWhenThereIsAModule(): void + { + request()->setMatchedRoute(new MatchedRoute( + (new Route(['GET'], '/current-route', 'TestController', 'tests'))->module('Test'), + [] + )); + + $this->assertSame( + base_dir() . DS . 'cache' . DS . 'views' . DS . 'test', + $this->invokePrivateMethod($this->viewCache, 'getCacheDir') + ); + } + }