Skip to content

Commit

Permalink
Merge pull request #2 from ryanmitchell/feature/support-query-scopes
Browse files Browse the repository at this point in the history
Support the addition of a query scope
  • Loading branch information
martyf authored Jan 16, 2024
2 parents fca2b46 + 40ab4c6 commit d37fb90
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
11 changes: 11 additions & 0 deletions config/statamic-scheduled-cache-invalidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

return [

'query_scopes' => [
// collection_handle => \Path\To\Scope::class,
// or
// 'all' => \Path\To\Scope::class,
],

];
13 changes: 9 additions & 4 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use MityDigital\StatamicScheduledCacheInvalidator\Console\Commands\RunCommand;
use Statamic\Providers\AddonServiceProvider;
use Statamic\StaticCaching\Invalidator;

class ServiceProvider extends AddonServiceProvider
{
Expand All @@ -14,8 +13,14 @@ class ServiceProvider extends AddonServiceProvider

public function bootAddon()
{
$invalidator = app(Invalidator::class);
//dd($invalidator);
parent::bootAddon();
$this->mergeConfigFrom(__DIR__.'/../config/statamic-scheduled-cache-invalidator.php', 'statamic-scheduled-cache-invalidator');

if ($this->app->runningInConsole()) {

$this->publishes([
__DIR__.'/../config/statamic-scheduled-cache-invalidator.php' => config_path('statamic-scheduled-cache-invalidator.php'),
], 'statamic-scheduled-cache-invalidator');

}
}
}
8 changes: 7 additions & 1 deletion src/Support/ScheduledCacheInvalidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ public function getEntries(): \Illuminate\Support\Collection
$entries = Entry::query()
->where('collection', $collection->handle())
->where('published', true)
->whereTime($collection->sortField() ?? 'date', $now)
->where(function ($query) use ($collection, $now) {
$query->whereTime($collection->sortField() ?? 'date', $now);

if ($scope = config('statamic-scheduled-cache-invalidator.query_scopes.'.$collection->handle(), config('statamic-scheduled-cache-invalidator.query_scopes.all'))) {
app($scope)->apply($query, ['collection' => $collection, 'now' => $now]);
}
})
->get();

// this triggers any publish status changes in the stache
Expand Down
26 changes: 26 additions & 0 deletions tests/Support/ScheduledCacheInvalidatorTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

use MityDigital\StatamicScheduledCacheInvalidator\Support\ScheduledCacheInvalidator;
use Mockery\MockInterface;
use Statamic\Query\Scopes\Scope;

use function Spatie\PestPluginTestTime\testTime;

Expand Down Expand Up @@ -62,3 +64,27 @@
// should have nothing returned - it's not dated
expect($support->getEntries())->toHaveCount(0);
});

it('supports query scopes', function () {
// get the support
$support = app(ScheduledCacheInvalidator::class);

// freeze time to be ON publish - this is when the undated entry has a "date" param
testTime()->freeze('2023-12-07 00:00:00');

config()->set('statamic-scheduled-cache-invalidator.query_scopes.all', TestScope::class);

$this->partialMock(TestScope::class, function (MockInterface $mock) {
$mock->shouldReceive('apply')->times(2);
});

// should have nothing returned - it's not dated
expect($support->getEntries())->toHaveCount(0);
});

class TestScope extends Scope
{
public function apply($query, $params)
{
}
}

0 comments on commit d37fb90

Please sign in to comment.