This commit is contained in:
Guy Sartorelli 2024-10-21 21:07:43 +00:00 committed by GitHub
commit c7138ebaec
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 24 additions and 38 deletions

View File

@ -3,9 +3,6 @@
namespace SilverStripe\Control\Middleware; namespace SilverStripe\Control\Middleware;
use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\BaseKernel;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Flushable;
use SilverStripe\Core\Injector\Injector; use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Kernel; use SilverStripe\Core\Kernel;
@ -17,16 +14,10 @@ class FlushMiddleware implements HTTPMiddleware
public function process(HTTPRequest $request, callable $delegate) public function process(HTTPRequest $request, callable $delegate)
{ {
$kernel = Injector::inst()->get(Kernel::class); $kernel = Injector::inst()->get(Kernel::class);
if ((method_exists($kernel, 'isFlushed') && $kernel->isFlushed())) { if ($kernel->isFlushed()) {
// Disable cache when flushing // Disable cache when flushing
HTTPCacheControlMiddleware::singleton()->disableCache(true); HTTPCacheControlMiddleware::singleton()->disableCache(true);
foreach (ClassInfo::implementorsOf(Flushable::class) as $class) {
/** @var Flushable|string $class */
$class::flush();
} }
}
return $delegate($request); return $delegate($request);
} }
} }

View File

@ -52,6 +52,14 @@ class CoreKernel extends BaseKernel
$this->validateDatabase(); $this->validateDatabase();
$this->setBooted(true); $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();
}
}
} }
/** /**

View File

@ -1,26 +0,0 @@
<?php
namespace SilverStripe\Control\Tests;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Kernel;
use SilverStripe\Control\Tests\FlushMiddlewareTest\TestFlushable;
use SilverStripe\Dev\FunctionalTest;
class FlushMiddlewareTest extends FunctionalTest
{
/**
* Assert that classes that implement flushable are called
*/
public function testImplementorsAreCalled()
{
TestFlushable::$flushed = false;
Injector::inst()->get(Kernel::class)->boot(true);
$this->get('/');
$this->assertTrue(TestFlushable::$flushed);
// reset the kernel Flush flag
Injector::inst()->get(Kernel::class)->boot();
}
}

View File

@ -15,6 +15,7 @@ use SilverStripe\Core\Environment;
use ReflectionClass; use ReflectionClass;
use SilverStripe\ORM\DB; use SilverStripe\ORM\DB;
use ReflectionObject; use ReflectionObject;
use SilverStripe\Core\Tests\KernelTest\TestFlushable;
class KernelTest extends SapphireTest class KernelTest extends SapphireTest
{ {
@ -113,4 +114,16 @@ class KernelTest extends SapphireTest
'password' => 'hi_people', 'password' => 'hi_people',
], $configs['replica_01']); ], $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
$kernel->boot();
}
} }

View File

@ -1,6 +1,6 @@
<?php <?php
namespace SilverStripe\Control\Tests\FlushMiddlewareTest; namespace SilverStripe\Core\Tests\KernelTest;
use SilverStripe\Core\Flushable; use SilverStripe\Core\Flushable;
use SilverStripe\Dev\TestOnly; use SilverStripe\Dev\TestOnly;