Skip to content

Commit

Permalink
[1.x] Allow any driver to opt-in to cache flushing (#131)
Browse files Browse the repository at this point in the history
* [1.x] Allow any driver to opt-in to cache flushing

* rename interface

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
dshafik and taylorotwell authored Dec 27, 2024
1 parent 55adb05 commit 44a14ad
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
13 changes: 13 additions & 0 deletions src/Contracts/HasFlushableCache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Laravel\Pennant\Contracts;

interface HasFlushableCache
{
/**
* Flush the cache.
*
* @return void
*/
public function flushCache();
}
3 changes: 2 additions & 1 deletion src/Drivers/ArrayDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
use Illuminate\Support\Collection;
use Laravel\Pennant\Contracts\CanListStoredFeatures;
use Laravel\Pennant\Contracts\Driver;
use Laravel\Pennant\Contracts\HasFlushableCache;
use Laravel\Pennant\Events\UnknownFeatureResolved;
use Laravel\Pennant\Feature;
use stdClass;

class ArrayDriver implements CanListStoredFeatures, Driver
class ArrayDriver implements CanListStoredFeatures, Driver, HasFlushableCache
{
/**
* The event dispatcher.
Expand Down
7 changes: 6 additions & 1 deletion src/Drivers/Decorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Laravel\Pennant\Contracts\DefinesFeaturesExternally;
use Laravel\Pennant\Contracts\Driver;
use Laravel\Pennant\Contracts\FeatureScopeable;
use Laravel\Pennant\Contracts\HasFlushableCache;
use Laravel\Pennant\Events\AllFeaturesPurged;
use Laravel\Pennant\Events\DynamicallyRegisteringFeatureClass;
use Laravel\Pennant\Events\FeatureDeleted;
Expand All @@ -36,7 +37,7 @@
/**
* @mixin \Laravel\Pennant\PendingScopedFeatureInteraction
*/
class Decorator implements CanListStoredFeatures, Driver
class Decorator implements CanListStoredFeatures, Driver, HasFlushableCache
{
use Macroable {
__call as macroCall;
Expand Down Expand Up @@ -801,6 +802,10 @@ protected function defaultScope()
public function flushCache()
{
$this->cache = new Collection;

if ($this->driver instanceof HasFlushableCache) {
$this->driver->flushCache();
}
}

/**
Expand Down
4 changes: 0 additions & 4 deletions src/FeatureManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,6 @@ public function flushCache()
foreach ($this->stores as $driver) {
$driver->flushCache();
}

if (isset($this->stores['array'])) {
$this->stores['array']->getDriver()->flushCache();
}
}

/**
Expand Down

0 comments on commit 44a14ad

Please sign in to comment.