Skip to content

Commit

Permalink
[5.x] Custom file cache store adjustments (#10362)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonvarga authored Jun 24, 2024
1 parent fa108ea commit 1252b7a
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
15 changes: 14 additions & 1 deletion src/Extensions/FileStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

class FileStore extends LaravelFileStore implements Store
{
private ?string $dir = null;

public function path($key)
{
if (! Str::startsWith($key, 'stache::')) {
Expand All @@ -16,6 +18,17 @@ public function path($key)

$key = Str::after($key, 'stache::');

return $this->directory.'/stache/'.str_replace('::', '/', $key);
return $this->dir().str_replace('::', '/', $key);
}

private function dir()
{
if ($this->dir) {
return $this->dir;
}

return $this->dir = Str::endsWith($this->directory, '/stache')
? $this->directory.'/'
: $this->directory.'/stache/';
}
}
12 changes: 8 additions & 4 deletions src/Providers/CacheServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function boot()
private function extendFileStore()
{
$this->app->booting(function () {
/** @deprecated */
Cache::extend('statamic', function () {
return Cache::repository(new FileStore(
$this->app['files'],
Expand All @@ -45,10 +46,13 @@ private function extendFileStore()
), $this->app['config']['cache.stores.file']);
});

if (config('cache.default') === 'file') {
config(['cache.stores.statamic' => ['driver' => 'statamic']]);
config(['cache.default' => 'statamic']);
}
Cache::extend('file', function ($app, $config) {
return Cache::repository(
(new FileStore($app['files'], $config['path'], $config['permission'] ?? null))
->setLockDirectory($config['lock_path'] ?? null),
$config
);
});
});
}

Expand Down
47 changes: 47 additions & 0 deletions tests/Extensions/FileStoreTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Tests\Extensions;

use Illuminate\Cache\ArrayStore;
use Illuminate\Support\Facades\Cache;
use Orchestra\Testbench\Attributes\DefineEnvironment;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Extensions\FileStore;
use Tests\TestCase;

class FileStoreTest extends TestCase
{
#[Test]
#[DefineEnvironment('cache')]
public function it_overrides_file_driven_stores()
{
$alfa = Cache::store('alfa')->getStore();
$this->assertInstanceOf(FileStore::class, $alfa);
$this->assertEquals(storage_path('framework/cache/alfa'), $alfa->getDirectory());

$bravo = Cache::store('bravo')->getStore();
$this->assertInstanceOf(FileStore::class, $bravo);
$this->assertEquals(storage_path('framework/cache/bravo'), $bravo->getDirectory());

// Non-file stores shouldn't be modified.
$charlie = Cache::store('charlie')->getStore();
$this->assertInstanceOf(ArrayStore::class, $charlie);
}

public function cache($app)
{
$app['config']->set('cache.stores.alfa', [
'driver' => 'file',
'path' => storage_path('framework/cache/alfa'),
]);

$app['config']->set('cache.stores.bravo', [
'driver' => 'file',
'path' => storage_path('framework/cache/bravo'),
]);

$app['config']->set('cache.stores.charlie', [
'driver' => 'array',
]);
}
}

0 comments on commit 1252b7a

Please sign in to comment.