diff --git a/README.md b/README.md index 4b94b20..8834a31 100644 --- a/README.md +++ b/README.md @@ -231,7 +231,21 @@ This request will return all posts with the status `active` and associated with ### Caching -In your filter class, you can control caching by using the `setUseCache` method. Set the `$useCache` property to `true` to enable caching, or `false` to disable it. You can also customise the duration of the cache by modifying the `$cacheExpiration` property.` +In your filter class, you can control caching by using the `enableCaching` static method. Set the `$useCache` static property to `true` to enable caching, or `false` to disable it. You can also customise the duration of the cache by modifying the `$cacheExpiration` property.` + +```php +// AppServiceProvider.php + +/** + * Bootstrap any application services. + * + * @return void + */ +public function boot(): void +{ + Filter::enableCaching(true); // Control caching +} +``` ```php namespace App\Filters; @@ -251,8 +265,7 @@ class PostFilter extends Filter $filter = new PostFilter(request(), cache()); // Control caching -$filter->setUseCache(true); -$filter->cacheExpiration = 1440; // Cache duration in minutes +$filter->setCacheExpiration(1440); // Cache duration in minutes ``` ## Testing diff --git a/src/Filterable/Filter.php b/src/Filterable/Filter.php index aeee392..a33d415 100644 --- a/src/Filterable/Filter.php +++ b/src/Filterable/Filter.php @@ -118,7 +118,7 @@ abstract class Filter implements FilterInterface * * @var bool */ - protected bool $useCache = true; + protected static bool $useCache = true; /** * Expiration minutes for the cache. @@ -197,7 +197,7 @@ protected function applyForUserFilter(): void */ protected function applyFilterables(): void { - if (! $this->shouldUseCache()) { + if (! self::shouldUseCache()) { $this->applyFiltersToQuery(); return; @@ -467,13 +467,11 @@ public function setBuilder(Builder $builder): self * * @param bool $useCache * - * @return self + * @return void */ - public function setUseCache(bool $useCache): self + public static function enableCaching(bool $useCache): void { - $this->useCache = $useCache; - - return $this; + self::$useCache = $useCache; } /** @@ -519,8 +517,8 @@ public function setCacheHandler(Cache $cache): self * * @return bool */ - public function shouldUseCache(): bool + public static function shouldUseCache(): bool { - return $this->useCache; + return self::$useCache; } } diff --git a/src/Filterable/Interfaces/Filter.php b/src/Filterable/Interfaces/Filter.php index fd03f04..82624b4 100644 --- a/src/Filterable/Interfaces/Filter.php +++ b/src/Filterable/Interfaces/Filter.php @@ -14,7 +14,7 @@ * This class is used to apply filters to an Eloquent query builder instance. * It provides methods to set and apply filters, cache the results, and filter results for a specific authenticated user. * - * @package Filterable + * @package Filterable\Interfaces * * @property \Illuminate\Database\Eloquent\Builder $builder The Eloquent builder instance. * @property \Illuminate\Http\Request $request The current HTTP request. @@ -174,9 +174,9 @@ public function setBuilder(Builder $builder): self; * * @param bool $useCache * - * @return self + * @return void */ - public function setUseCache(bool $useCache): self; + public static function enableCaching(bool $useCache): void; /** * Clear the cache. @@ -206,5 +206,5 @@ public function setCacheHandler(Cache $cache): self; * * @return bool */ - public function shouldUseCache(): bool; + public static function shouldUseCache(): bool; } diff --git a/src/Filterable/Interfaces/Handler.php b/src/Filterable/Interfaces/Handler.php index fff04e4..57d223a 100644 --- a/src/Filterable/Interfaces/Handler.php +++ b/src/Filterable/Interfaces/Handler.php @@ -5,6 +5,11 @@ use Closure; use Illuminate\Database\Eloquent\Builder; +/** + * Interface Filterable + * + * @package Filterable\Interfaces + */ interface Handler { /** diff --git a/tests/FilterTest.php b/tests/FilterTest.php index 26eeb60..0e706bf 100644 --- a/tests/FilterTest.php +++ b/tests/FilterTest.php @@ -42,11 +42,15 @@ protected function tearDown(): void { m::close(); + Filter::enableCaching(false); + parent::tearDown(); } public function testAppliesFiltersDynamicallyBasedOnRequest(): void { + Filter::enableCaching(false); + $request = Request::create('/?name=' . urlencode('John Doe'), 'GET'); $model = new MockFilterable(); $builder = $model->newQuery(); @@ -56,7 +60,6 @@ public function testAppliesFiltersDynamicallyBasedOnRequest(): void // Assuming 'name' filter translates to a method call $filter = new MockFilter($request, $cache); - $filter->setUseCache(false); $results = $filter->apply($builder); @@ -70,6 +73,8 @@ public function testAppliesFiltersDynamicallyBasedOnRequest(): void public function testAppliesFiltersManuallyThroughModel(): void { + Filter::enableCaching(false); + $request = Request::create('/?name=' . urlencode('John Doe'), 'GET'); $model = new MockFilterable(); $builder = $model->newQuery(); @@ -79,7 +84,6 @@ public function testAppliesFiltersManuallyThroughModel(): void // Assuming 'name' filter translates to a method call $filter = new MockFilter($request, $cache); - $filter->setUseCache(false); $results = $model->filter($filter); @@ -93,6 +97,8 @@ public function testAppliesFiltersManuallyThroughModel(): void public function testAppliesFiltersDynamicallyBasedOnRequestWithCustomMethodNames(): void { + Filter::enableCaching(false); + $request = Request::create('/?name=' . urlencode('Jane Doe'), 'GET'); $model = new MockFilterable(); $builder = $model->newQuery(); @@ -112,8 +118,6 @@ public function filterByName($name) } }; - $filter->setUseCache(false); - $results = $filter->apply($builder); $this->assertEquals( @@ -126,6 +130,8 @@ public function filterByName($name) public function testHandlesCachingCorrectly(): void { + Filter::enableCaching(true); + $request = new Request(); $cache = m::spy(Repository::class); $model = new MockFilterable(); @@ -140,8 +146,6 @@ public function testHandlesCachingCorrectly(): void public function __construct($request, $cache) { parent::__construct($request, $cache); - - $this->useCache = true; // Ensure caching is enabled } protected function testFilter($value) @@ -160,6 +164,8 @@ protected function testFilter($value) public function testHandlesCachingCorrectlyWhenDisabled(): void { + Filter::enableCaching(false); + $request = new Request(); $cache = m::spy(Repository::class); $model = new MockFilterable(); @@ -177,8 +183,6 @@ protected function testFilter($value) } }; - $filter->setUseCache(false); - $results = $filter->apply($builder); $this->assertSame($builder, $results); @@ -187,6 +191,8 @@ protected function testFilter($value) public function testHandlesCachingCorrectlyWhenForced(): void { + Filter::enableCaching(true); + $request = new Request(); $cache = m::spy(Repository::class); $model = new MockFilterable(); @@ -204,8 +210,6 @@ protected function testFilter($value) } }; - $filter->setUseCache(true); - $results = $filter->apply($builder); $this->assertSame($builder, $results); @@ -214,6 +218,8 @@ protected function testFilter($value) public function testHandlesCachingCorrectlyWhenForcedWithCustomTtl(): void { + Filter::enableCaching(true); + $request = new Request(); $cache = m::spy(Repository::class); $model = new MockFilterable(); @@ -233,7 +239,6 @@ protected function testFilter($value) } }; - $filter->setUseCache(true); $filter->setCacheExpiration(60); $results = $filter->apply($builder); @@ -244,6 +249,8 @@ protected function testFilter($value) public function testClearsCacheCorrectly(): void { + Filter::enableCaching(true); + $request = new Request(); $cache = m::mock(Repository::class); $cache->shouldReceive('forget')->once(); @@ -262,6 +269,8 @@ public function testClearsCacheCorrectly(): void public function testAppliesPreFiltersCorrectly(): void { + Filter::enableCaching(true); + $request = new Request(); $cache = m::mock(Repository::class); $model = new MockFilterable(); @@ -277,7 +286,7 @@ public function applyPreFilters(): void } }; - // $filter->setUseCache(false); + // $filter->enableCaching(false); $filter->apply($builder);