Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Events/UrlInvalidated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Statamic\Events;

use Statamic\StaticCaching\Cacher;

class UrlInvalidated extends Event
{
public $url;

public function __construct($url, $domain = null)
{
$domain ??= app(Cacher::class)->getBaseUrl();

$this->url = $domain.$url;
}
}
3 changes: 3 additions & 0 deletions src/StaticCaching/Cachers/ApplicationCacher.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Statamic\StaticCaching\Cachers;

use Illuminate\Http\Request;
use Statamic\Events\UrlInvalidated;

class ApplicationCacher extends AbstractCacher
{
Expand Down Expand Up @@ -108,5 +109,7 @@ public function invalidateUrl($url, $domain = null)
$this->cache->forget($this->normalizeKey('responses:'.$key));
$this->forgetUrl($key);
});

UrlInvalidated::dispatch($url, $domain);
}
}
3 changes: 3 additions & 0 deletions src/StaticCaching/Cachers/FileCacher.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Contracts\Cache\Repository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Statamic\Events\UrlInvalidated;
use Statamic\Facades\File;
use Statamic\Facades\Site;
use Statamic\StaticCaching\Replacers\CsrfTokenReplacer;
Expand Down Expand Up @@ -122,6 +123,8 @@ public function invalidateUrl($url, $domain = null)
$this->writer->delete($this->getFilePath($value, $site));
$this->forgetUrl($key, $domain);
});

UrlInvalidated::dispatch($url, $domain);
}

public function getCachePaths()
Expand Down
5 changes: 5 additions & 0 deletions src/StaticCaching/Cachers/NullCacher.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ public function getUrls($domain = null)
{
return collect();
}

public function getBaseUrl()
{
//
}
}
34 changes: 34 additions & 0 deletions tests/StaticCaching/ApplicationCacherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use Illuminate\Contracts\Cache\Repository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Event;
use Statamic\Events\UrlInvalidated;
use Statamic\StaticCaching\Cacher;
use Statamic\StaticCaching\Cachers\ApplicationCacher;
use Tests\TestCase;

Expand Down Expand Up @@ -105,6 +108,37 @@ public function invalidating_a_url_will_invalidate_all_query_string_versions_too
$this->assertNotNull($cache->get('static-cache:responses:two'));
}

/**
* @test
*
* @dataProvider invalidateEventProvider
*/
public function invalidating_a_url_dispatches_event($domain, $expectedUrl)
{
Event::fake();

$cache = app(Repository::class);
$cacher = new ApplicationCacher($cache, ['base_url' => 'http://base.com']);

// Put it in the container so that the event can resolve it.
$this->instance(Cacher::class, $cacher);

$cacher->invalidateUrl('/foo', $domain);

Event::assertDispatched(UrlInvalidated::class, function ($event) use ($expectedUrl) {
return $event->url === $expectedUrl;
});
}

public function invalidateEventProvider()
{
return [
'no domain' => [null, 'http://base.com/foo'],
'configured base domain' => ['http://base.com', 'http://base.com/foo'],
'another domain' => ['http://another.com', 'http://another.com/foo'],
];
}

/** @test */
public function it_flushes()
{
Expand Down
35 changes: 35 additions & 0 deletions tests/StaticCaching/FileCacherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
namespace Tests\StaticCaching;

use Illuminate\Contracts\Cache\Repository;
use Illuminate\Support\Facades\Event;
use Statamic\Events\UrlInvalidated;
use Statamic\Facades\Site;
use Statamic\StaticCaching\Cacher;
use Statamic\StaticCaching\Cachers\FileCacher;
use Statamic\StaticCaching\Cachers\Writer;
use Tests\TestCase;
Expand Down Expand Up @@ -292,6 +295,38 @@ public function invalidating_a_url_deletes_the_file_and_removes_the_url_when_usi
$this->assertEquals(['one' => '/one'], $cacher->getUrls('http://domain.de')->all());
}

/**
* @test
*
* @dataProvider invalidateEventProvider
*/
public function invalidating_a_url_dispatches_event($domain, $expectedUrl)
{
Event::fake();

$writer = \Mockery::spy(Writer::class);
$cache = app(Repository::class);
$cacher = $this->fileCacher(['base_url' => 'http://base.com'], $writer, $cache);

// Put it in the container so that the event can resolve it.
$this->instance(Cacher::class, $cacher);

$cacher->invalidateUrl('/foo', $domain);

Event::assertDispatched(UrlInvalidated::class, function ($event) use ($expectedUrl) {
return $event->url === $expectedUrl;
});
}

public function invalidateEventProvider()
{
return [
'no domain' => [null, 'http://base.com/foo'],
'configured base domain' => ['http://base.com', 'http://base.com/foo'],
'another domain' => ['http://another.com', 'http://another.com/foo'],
];
}

private function cacheKey($domain)
{
return 'static-cache:'.md5($domain).'.urls';
Expand Down