Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Configuration to disable events on Cache Repository #51032

Merged
merged 3 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Illuminate/Cache/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,10 @@ protected function newDynamodbClient(array $config)
*/
public function repository(Store $store, array $config = [])
{
return tap(new Repository($store, Arr::only($config, ['store'])), function ($repository) {
$this->setEventDispatcher($repository);
return tap(new Repository($store, Arr::only($config, ['store'])), function ($repository) use ($config) {
if ($config['events'] ?? true) {
$this->setEventDispatcher($repository);
}
});
}

Expand Down
30 changes: 30 additions & 0 deletions tests/Cache/CacheManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,36 @@ public function testThrowExceptionWhenUnknownStoreIsUsed()
$cacheManager->store('alien_store');
}

public function testMakesRepositoryWithoutDispatcherWhenEventsDisabled()
{
$userConfig = [
'cache' => [
'stores' => [
'my_store' => [
'driver' => 'array',
],
'my_store_without_events' => [
'driver' => 'array',
'events' => false,
],
],
],
];

$app = $this->getApp($userConfig);
$app->bind(Dispatcher::class, fn () => new Event);

$cacheManager = new CacheManager($app);

// The repository will have an event dispatcher
$repo = $cacheManager->store('my_store');
$this->assertNotNull($repo->getEventDispatcher());

// This repository will not have an event dispatcher as 'with_events' is false
$repoWithoutEvents = $cacheManager->store('my_store_without_events');
$this->assertNull($repoWithoutEvents->getEventDispatcher());
}

protected function getApp(array $userConfig)
{
$app = new Container;
Expand Down
Loading