Skip to content

Commit 21b4d11

Browse files
Merge pull request #645 from jaydublu2002/feature-disable-cache
fix: adds environment variable to disable structure caching
2 parents 06ee679 + fb1367b commit 21b4d11

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

config/data.php

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
* store should be used.
7777
*/
7878
'structure_caching' => [
79+
'enabled' => env('DATA_STRUCTURE_CACHE_ENABLED', true),
7980
'directories' => [app_path('Data')],
8081
'cache' => [
8182
'store' => env('CACHE_DRIVER', 'file'),

docs/advanced-usage/performance.md

+2
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,5 @@ When using reflection discovery, the base directory and root namespace can be co
6363
```
6464

6565
You can read more about reflection discovery [here](https://github.com/spatie/php-structure-discoverer#parsers).
66+
67+
Caching can be disabled e.g. for development or test environments by setting `DATA_STRUCTURE_CACHE_ENABLED=false` in .env

src/LaravelDataServiceProvider.php

+16-9
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,22 @@ public function configurePackage(Package $package): void
2424

2525
public function packageRegistered(): void
2626
{
27-
$this->app->singleton(
28-
DataStructureCache::class,
29-
fn () => new DataStructureCache(config('data.structure_caching.cache'))
30-
);
31-
32-
$this->app->singleton(
33-
DataConfig::class,
34-
fn () => $this->app->make(DataStructureCache::class)->getConfig() ?? new DataConfig(config('data'))
35-
);
27+
if (config('data.structure_caching.enabled')) {
28+
$this->app->singleton(
29+
DataStructureCache::class,
30+
fn () => new DataStructureCache(config('data.structure_caching.cache'))
31+
);
32+
33+
$this->app->singleton(
34+
DataConfig::class,
35+
fn () => $this->app->make(DataStructureCache::class)->getConfig() ?? new DataConfig(config('data'))
36+
);
37+
} else {
38+
$this->app->singleton(
39+
DataConfig::class,
40+
fn () => new DataConfig(config('data'))
41+
);
42+
}
3643

3744
/** @psalm-suppress UndefinedInterfaceMethod */
3845
$this->app->beforeResolving(BaseData::class, function ($class, $parameters, $app) {

0 commit comments

Comments
 (0)