Skip to content

Commit

Permalink
feat: adds environment variable to disable structure caching
Browse files Browse the repository at this point in the history
  • Loading branch information
jaydublu2002 committed Jan 22, 2024
1 parent 324970e commit fb1367b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
1 change: 1 addition & 0 deletions config/data.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
2 changes: 2 additions & 0 deletions docs/advanced-usage/performance.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
25 changes: 16 additions & 9 deletions src/LaravelDataServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit fb1367b

Please sign in to comment.