Skip to content

Commit

Permalink
API Ensure everything gets flushed when flushing from sake
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Oct 23, 2024
1 parent 715c2de commit 6db6d3f
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 74 deletions.
1 change: 0 additions & 1 deletion _config/requestprocessors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ SilverStripe\Core\Injector\Injector:
TrustedProxyMiddleware: '%$SilverStripe\Control\Middleware\TrustedProxyMiddleware'
AllowedHostsMiddleware: '%$SilverStripe\Control\Middleware\AllowedHostsMiddleware'
SessionMiddleware: '%$SilverStripe\Control\Middleware\SessionMiddleware'
FlushMiddleware: '%$SilverStripe\Control\Middleware\FlushMiddleware'
ChangeDetectionMiddleware: '%$SilverStripe\Control\Middleware\ChangeDetectionMiddleware'
HTTPCacheControleMiddleware: '%$SilverStripe\Control\Middleware\HTTPCacheControlMiddleware'
CanonicalURLMiddleware: '%$SilverStripe\Control\Middleware\CanonicalURLMiddleware'
Expand Down
32 changes: 0 additions & 32 deletions src/Control/Middleware/FlushMiddleware.php

This file was deleted.

7 changes: 7 additions & 0 deletions src/Control/Middleware/HTTPCacheControlMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Kernel;
use SilverStripe\Core\Resettable;
use SilverStripe\ORM\FieldType\DBDatetime;

Expand All @@ -35,6 +36,12 @@ class HTTPCacheControlMiddleware implements HTTPMiddleware, Resettable
*/
public function process(HTTPRequest $request, callable $delegate)
{
// Disable cache when flushing
$kernel = Injector::inst()->get(Kernel::class);
if ($kernel->isFlushed()) {
$this->disableCache(true);
}

try {
$response = $delegate($request);
} catch (HTTPResponse_Exception $ex) {
Expand Down
8 changes: 8 additions & 0 deletions src/Core/CoreKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}

/**
Expand Down
8 changes: 2 additions & 6 deletions src/Core/Flushable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace SilverStripe\Core;

use SilverStripe\Control\Middleware\FlushMiddleware;

/**
* Provides an interface for classes to implement their own flushing functionality
* whenever flush=1 is requested.
Expand All @@ -12,11 +10,9 @@ interface Flushable
{

/**
* This function is triggered early in the request if the "flush" query
* parameter has been set. Each class that implements Flushable implements
* This function is triggered early in the request if the kernel gets flushed.
* Each class that implements Flushable implements
* this function which looks after it's own specific flushing functionality.
*
* @see FlushMiddleware
*/
public static function flush();
}
7 changes: 0 additions & 7 deletions src/Forms/HTMLEditor/TinyMCECombinedGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,6 @@ public function generateFilename(TinyMCEConfig $config)
return $url;
}

/**
* This function is triggered early in the request if the "flush" query
* parameter has been set. Each class that implements Flushable implements
* this function which looks after it's own specific flushing functionality.
*
* @see FlushMiddleware
*/
public static function flush()
{
$dir = dirname(static::config()->get('filename_base') ?? '');
Expand Down
26 changes: 0 additions & 26 deletions tests/php/Control/FlushMiddlewareTest.php

This file was deleted.

15 changes: 14 additions & 1 deletion tests/php/Core/KernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use ReflectionClass;
use SilverStripe\ORM\DB;
use ReflectionObject;
use SilverStripe\Core\Tests\KernelTest\TestFlushable;

class KernelTest extends SapphireTest
{
Expand Down Expand Up @@ -85,7 +86,7 @@ public function testInvalidConfigDetection()

$kernel->getConfigLoader()->getManifest();
}

public function testReplicaDatabaseVarsLoaded()
{
// Set environment variables for a fake replica database
Expand Down Expand Up @@ -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
$kernel->boot();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace SilverStripe\Control\Tests\FlushMiddlewareTest;
namespace SilverStripe\Core\Tests\KernelTest;

use SilverStripe\Core\Flushable;
use SilverStripe\Dev\TestOnly;
Expand Down

0 comments on commit 6db6d3f

Please sign in to comment.