Skip to content

Commit

Permalink
Allow disabling structure cache
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanassche committed Feb 9, 2024
1 parent 55f085a commit ea7ad7e
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions config/data.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
* store should be used.
*/
'structure_caching' => [
'enabled' => true,
'directories' => [app_path('Data')],
'cache' => [
'store' => env('CACHE_DRIVER', 'file'),
Expand Down
1 change: 1 addition & 0 deletions docs/installation-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ return [
* store should be used.
*/
'structure_caching' => [
'enabled' => true,
'directories' => [app_path('Data')],
'cache' => [
'store' => env('CACHE_DRIVER', 'file'),
Expand Down
6 changes: 6 additions & 0 deletions src/Commands/DataStructuresCacheCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ public function handle(
DataStructureCache $dataStructureCache,
DataClassFactory $dataClassFactory,
): void {
if(config('data.structure_caching.enabled') === false){
$this->error('Data structure caching is not enabled');

return;
}

$this->components->info('Caching data structures...');

$dataClasses = DataClassFinder::fromConfig(config('data.structure_caching'))->classes();
Expand Down
8 changes: 7 additions & 1 deletion src/LaravelDataServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ public function packageRegistered(): void

$this->app->singleton(
DataConfig::class,
fn () => $this->app->make(DataStructureCache::class)->getConfig() ?? DataConfig::createFromConfig(config('data'))
function () {
if (! config('data.structure_caching.enabled')) {
return DataConfig::createFromConfig(config('data'));
}

return $this->app->make(DataStructureCache::class)->getConfig() ?? DataConfig::createFromConfig(config('data'));
}
);

$this->app->beforeResolving(BaseData::class, function ($class, $parameters, $app) {
Expand Down
2 changes: 1 addition & 1 deletion src/Support/Caching/DataStructureCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class DataStructureCache
public function __construct(
protected array $cacheConfig,
) {
$this->store = cache()->store($this->cacheConfig['store'])->getStore();
$this->store = cache()->store($this->cacheConfig['store'])?->getStore();
$this->prefix = $this->cacheConfig['prefix'] ? "{$this->cacheConfig['prefix']}." : '';
}

Expand Down
2 changes: 2 additions & 0 deletions tests/DataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Illuminate\Contracts\Support\Responsable;
use Illuminate\Validation\ValidationException;
use Spatie\LaravelData\Attributes\DataCollectionOf;
use Spatie\LaravelData\Concerns\AppendableData;
use Spatie\LaravelData\Concerns\BaseData;
use Spatie\LaravelData\Concerns\ContextableData;
Expand All @@ -20,6 +21,7 @@
use Spatie\LaravelData\Contracts\ValidateableData as ValidateableDataContract;
use Spatie\LaravelData\Contracts\WrappableData as WrappableDataContract;
use Spatie\LaravelData\Data;
use Spatie\LaravelData\DataCollection;
use Spatie\LaravelData\Dto;
use Spatie\LaravelData\Resource;
use Spatie\LaravelData\Tests\Fakes\SimpleData;
Expand Down
11 changes: 11 additions & 0 deletions tests/Support/Caching/CachedDataConfigTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Cache;
use Mockery\MockInterface;
use Spatie\LaravelData\Support\Caching\CachedDataConfig;
use Spatie\LaravelData\Support\Caching\DataStructureCache;
Expand Down Expand Up @@ -66,3 +67,13 @@ function (MockInterface $spy) use ($dataClass) {
->toBeInstanceOf(DataClass::class)
->name->toBe(SimpleData::class);
});

it('can disable caching', function (){
config()->set('data.structure_caching.enabled', false);

Cache::expects('get')->once();

$data = SimpleData::from('Hello world');

cache()->get('something-just-to-test-the-mock');
});

0 comments on commit ea7ad7e

Please sign in to comment.