Skip to content

Commit

Permalink
[5.x] Auto stache watcher setting (#10354)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonvarga authored Jun 24, 2024
1 parent a9b4746 commit 875667a
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 8 deletions.
2 changes: 1 addition & 1 deletion config/stache.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
|
*/

'watcher' => env('STATAMIC_STACHE_WATCHER', true),
'watcher' => env('STATAMIC_STACHE_WATCHER', 'auto'),

/*
|--------------------------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion src/Assets/AssetContainerContents.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use League\Flysystem\DirectoryListing;
use Statamic\Facades\Stache;
use Statamic\Statamic;
use Statamic\Support\Str;

Expand Down Expand Up @@ -313,6 +314,6 @@ private function key()

private function ttl()
{
return config('statamic.stache.watcher') ? 0 : null;
return Stache::isWatcherEnabled() ? 0 : null;
}
}
1 change: 1 addition & 0 deletions src/Facades/Stache.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* @method static mixed|null buildDate()
* @method static self disableUpdatingIndexes()
* @method static bool shouldUpdateIndexes()
* @method static bool isWatcherEnabled()
*
* @see \Statamic\Stache\Stache
*/
Expand Down
14 changes: 13 additions & 1 deletion src/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Statamic\Facades\Addon;
use Statamic\Facades\Preference;
use Statamic\Facades\Site;
use Statamic\Facades\Stache;
use Statamic\Facades\Token;
use Statamic\Fields\FieldsetRecursionStack;
use Statamic\Sites\Sites;
Expand Down Expand Up @@ -196,7 +197,7 @@ protected function addAboutCommandInfo()
AboutCommand::add('Statamic', [
'Version' => fn () => Statamic::version().' '.(Statamic::pro() ? '<fg=yellow;options=bold>PRO</>' : 'Solo'),
'Addons' => $addons->count(),
'Stache Watcher' => config('statamic.stache.watcher') ? 'Enabled' : 'Disabled',
'Stache Watcher' => fn () => $this->stacheWatcher(),
'Static Caching' => config('statamic.static_caching.strategy') ?: 'Disabled',
'Sites' => fn () => $this->sitesAboutCommandInfo(),
]);
Expand All @@ -206,6 +207,17 @@ protected function addAboutCommandInfo()
}
}

private function stacheWatcher()
{
$status = Stache::isWatcherEnabled() ? 'Enabled' : 'Disabled';

if (config('statamic.stache.watcher') === 'auto') {
$status .= ' (auto)';
}

return $status;
}

private function sitesAboutCommandInfo()
{
if (($sites = Site::all())->count() === 1) {
Expand Down
9 changes: 9 additions & 0 deletions src/Stache/Stache.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,13 @@ public function duplicates()

return $this->duplicates = (new Duplicates($this))->load();
}

public function isWatcherEnabled(): bool
{
$config = config('statamic.stache.watcher');

return $config === 'auto'
? app()->isLocal()
: (bool) $config;
}
}
2 changes: 1 addition & 1 deletion src/Stache/Stores/CollectionsStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ private function updateEntriesWithinIndex($index, $ids)

public function handleFileChanges()
{
if ($this->fileChangesHandled || ! config('statamic.stache.watcher')) {
if ($this->fileChangesHandled || ! Stache::isWatcherEnabled()) {
return;
}

Expand Down
3 changes: 2 additions & 1 deletion src/Stache/Stores/ContainerAssetsStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Facades\Cache;
use Statamic\Facades\AssetContainer;
use Statamic\Facades\Stache;
use Statamic\Statamic;
use Statamic\Support\Str;

Expand All @@ -25,7 +26,7 @@ public function handleFileChanges()

$this->fileChangesHandled = true;

if (! config('statamic.stache.watcher')) {
if (! Stache::isWatcherEnabled()) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Stache/Stores/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public function handleFileChanges()
// This whole process can be disabled to save overhead, at the expense of needing to update
// the cache manually. If the Control Panel is being used, or the cache is cleared when
// deployed, for example, this will happen naturally and disabling is a good idea.
if (! config('statamic.stache.watcher')) {
if (! Stache::isWatcherEnabled()) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Stache/Stores/TaxonomyTermsStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ protected function makeTerm($taxonomy, $slug)

public function handleFileChanges()
{
if ($this->fileChangesHandled || ! config('statamic.stache.watcher')) {
if ($this->fileChangesHandled || ! Stache::isWatcherEnabled()) {
return;
}

Expand Down
30 changes: 29 additions & 1 deletion tests/Stache/StacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@

use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Collection;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Statamic\Stache\Stache;
use Statamic\Stache\Stores\ChildStore;
use Statamic\Stache\Stores\CollectionsStore;
use Statamic\Stache\Stores\EntriesStore;
use Tests\TestCase;

class StacheTest extends TestCase
{
protected $stache;

public function setUp(): void
{
parent::setUp();
$this->stache = new Stache;
}

Expand Down Expand Up @@ -125,4 +127,30 @@ public function it_can_record_its_build_time()
{
$this->markTestIncomplete();
}

#[Test]
#[DataProvider('watcherProvider')]
public function it_can_determine_if_watcher_is_enabled($environment, $config, $expected)
{
app()['env'] = $environment;

config(['statamic.stache.watcher' => $config]);

$this->assertEquals($expected, $this->stache->isWatcherEnabled());
}

public static function watcherProvider()
{
return [
['local', 'config' => true, 'expected' => true],
['production', 'config' => true, 'expected' => true],
['local', 'config' => false, 'expected' => false],
['production', 'config' => false, 'expected' => false],
['local', 'config' => 'auto', 'expected' => true],
['production', 'config' => 'auto', 'expected' => false],
['other', 'config' => 'auto', 'expected' => false],
['local', 'config' => null, 'expected' => false],
['production', 'config' => null, 'expected' => false],
];
}
}
1 change: 1 addition & 0 deletions tests/Stache/Stores/AssetContainersStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public function it_saves_to_disk()
Facades\Stache::shouldReceive('shouldUpdateIndexes')->andReturnTrue();
Facades\Stache::shouldReceive('duplicates')->andReturn(optional());
Facades\Stache::shouldReceive('store')->with('users')->andReturn((new UsersStore((new Stache)->sites(['en']), app('files')))->directory($this->tempDir));
Facades\Stache::shouldReceive('isWatcherEnabled')->andReturnTrue();

$container = Facades\AssetContainer::make('new')
->title('New Container');
Expand Down

0 comments on commit 875667a

Please sign in to comment.