From 72510902f0b5c71a55e53234a18254d57fe5dac5 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Tue, 22 Oct 2024 09:58:55 +1300 Subject: [PATCH] FIX Ensure everything gets flushed when flushing from sake --- src/Control/Middleware/FlushMiddleware.php | 11 +------- src/Core/CoreKernel.php | 8 ++++++ tests/php/Control/FlushMiddlewareTest.php | 26 ------------------- tests/php/Core/KernelTest.php | 15 ++++++++++- .../KernelTest}/TestFlushable.php | 2 +- 5 files changed, 24 insertions(+), 38 deletions(-) delete mode 100644 tests/php/Control/FlushMiddlewareTest.php rename tests/php/{Control/FlushMiddlewareTest => Core/KernelTest}/TestFlushable.php (81%) diff --git a/src/Control/Middleware/FlushMiddleware.php b/src/Control/Middleware/FlushMiddleware.php index 374063eec36..64cd4279aba 100644 --- a/src/Control/Middleware/FlushMiddleware.php +++ b/src/Control/Middleware/FlushMiddleware.php @@ -3,9 +3,6 @@ namespace SilverStripe\Control\Middleware; use SilverStripe\Control\HTTPRequest; -use SilverStripe\Core\BaseKernel; -use SilverStripe\Core\ClassInfo; -use SilverStripe\Core\Flushable; use SilverStripe\Core\Injector\Injector; use SilverStripe\Core\Kernel; @@ -17,16 +14,10 @@ class FlushMiddleware implements HTTPMiddleware public function process(HTTPRequest $request, callable $delegate) { $kernel = Injector::inst()->get(Kernel::class); - if ((method_exists($kernel, 'isFlushed') && $kernel->isFlushed())) { + if ($kernel->isFlushed()) { // Disable cache when flushing HTTPCacheControlMiddleware::singleton()->disableCache(true); - - foreach (ClassInfo::implementorsOf(Flushable::class) as $class) { - /** @var Flushable|string $class */ - $class::flush(); - } } - return $delegate($request); } } diff --git a/src/Core/CoreKernel.php b/src/Core/CoreKernel.php index ca9946f68a3..5626fd08082 100644 --- a/src/Core/CoreKernel.php +++ b/src/Core/CoreKernel.php @@ -52,6 +52,14 @@ public function boot($flush = false) $this->validateDatabase(); $this->setBooted(true); + + // Flush everything else that can be flushed, now that we're booted. + if ($flush) { + foreach (ClassInfo::implementorsOf(Flushable::class) as $class) { + /** @var Flushable|string $class */ + $class::flush(); + } + } } /** diff --git a/tests/php/Control/FlushMiddlewareTest.php b/tests/php/Control/FlushMiddlewareTest.php deleted file mode 100644 index a553d14ba3f..00000000000 --- a/tests/php/Control/FlushMiddlewareTest.php +++ /dev/null @@ -1,26 +0,0 @@ -get(Kernel::class)->boot(true); - $this->get('/'); - $this->assertTrue(TestFlushable::$flushed); - - // reset the kernel Flush flag - Injector::inst()->get(Kernel::class)->boot(); - } -} diff --git a/tests/php/Core/KernelTest.php b/tests/php/Core/KernelTest.php index c779f349521..1f0e9ca16d4 100644 --- a/tests/php/Core/KernelTest.php +++ b/tests/php/Core/KernelTest.php @@ -15,6 +15,7 @@ use ReflectionClass; use SilverStripe\ORM\DB; use ReflectionObject; +use SilverStripe\Core\Tests\KernelTest\TestFlushable; class KernelTest extends SapphireTest { @@ -85,7 +86,7 @@ public function testInvalidConfigDetection() $kernel->getConfigLoader()->getManifest(); } - + public function testReplicaDatabaseVarsLoaded() { // Set environment variables for a fake replica database @@ -113,4 +114,16 @@ public function testReplicaDatabaseVarsLoaded() 'password' => 'hi_people', ], $configs['replica_01']); } + + public function testImplementorsAreCalled() + { + TestFlushable::$flushed = false; + + $kernel = Injector::inst()->get(Kernel::class); + $kernel->boot(true); + $this->assertTrue(TestFlushable::$flushed); + + // reset the kernel Flush flag + Injector::inst()->get(Kernel::class)->boot(); + } } diff --git a/tests/php/Control/FlushMiddlewareTest/TestFlushable.php b/tests/php/Core/KernelTest/TestFlushable.php similarity index 81% rename from tests/php/Control/FlushMiddlewareTest/TestFlushable.php rename to tests/php/Core/KernelTest/TestFlushable.php index f1c290e1651..8b1f2d87ae5 100644 --- a/tests/php/Control/FlushMiddlewareTest/TestFlushable.php +++ b/tests/php/Core/KernelTest/TestFlushable.php @@ -1,6 +1,6 @@