diff --git a/config/data.php b/config/data.php index 6a0c70862..361daf59b 100644 --- a/config/data.php +++ b/config/data.php @@ -76,6 +76,7 @@ * store should be used. */ 'structure_caching' => [ + 'enabled' => env('DATA_STRUCTURE_CACHE_ENABLED', true), 'directories' => [app_path('Data')], 'cache' => [ 'store' => env('CACHE_DRIVER', 'file'), diff --git a/docs/advanced-usage/performance.md b/docs/advanced-usage/performance.md index f113e93a5..6bcf3aeb3 100644 --- a/docs/advanced-usage/performance.md +++ b/docs/advanced-usage/performance.md @@ -63,3 +63,5 @@ When using reflection discovery, the base directory and root namespace can be co ``` You can read more about reflection discovery [here](https://github.com/spatie/php-structure-discoverer#parsers). + +Caching can be disabled e.g. for development or test environments by setting `DATA_STRUCTURE_CACHE_ENABLED=false` in .env diff --git a/src/LaravelDataServiceProvider.php b/src/LaravelDataServiceProvider.php index 276c676c4..5e7e08e7c 100644 --- a/src/LaravelDataServiceProvider.php +++ b/src/LaravelDataServiceProvider.php @@ -24,15 +24,22 @@ public function configurePackage(Package $package): void public function packageRegistered(): void { - $this->app->singleton( - DataStructureCache::class, - fn () => new DataStructureCache(config('data.structure_caching.cache')) - ); - - $this->app->singleton( - DataConfig::class, - fn () => $this->app->make(DataStructureCache::class)->getConfig() ?? new DataConfig(config('data')) - ); + if (config('data.structure_caching.enabled')) { + $this->app->singleton( + DataStructureCache::class, + fn () => new DataStructureCache(config('data.structure_caching.cache')) + ); + + $this->app->singleton( + DataConfig::class, + fn () => $this->app->make(DataStructureCache::class)->getConfig() ?? new DataConfig(config('data')) + ); + } else { + $this->app->singleton( + DataConfig::class, + fn () => new DataConfig(config('data')) + ); + } /** @psalm-suppress UndefinedInterfaceMethod */ $this->app->beforeResolving(BaseData::class, function ($class, $parameters, $app) {