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] Add ability to dynamically build cache repositories on-demand using Cache::build #53454

Merged
merged 2 commits into from
Nov 11, 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
13 changes: 13 additions & 0 deletions src/Illuminate/Cache/CacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,19 @@ public function resolve($name)

$config = Arr::add($config, 'store', $name);

return $this->build($config);
}

/**
* Build a cache repository with the given configuration.
*
* @param array $config
* @return \Illuminate\Cache\Repository
*/
public function build(array $config)
{
$config = Arr::add($config, 'store', $config['name'] ?? 'ondemand');

if (isset($this->customCreators[$config['driver']])) {
return $this->callCustomCreator($config);
}
Expand Down
1 change: 1 addition & 0 deletions src/Illuminate/Support/Facades/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* @method static \Illuminate\Contracts\Cache\Repository store(string|null $name = null)
* @method static \Illuminate\Contracts\Cache\Repository driver(string|null $driver = null)
* @method static \Illuminate\Contracts\Cache\Repository resolve(string $name)
* @method static \Illuminate\Contracts\Cache\Repository build(array $config)
* @method static \Illuminate\Cache\Repository repository(\Illuminate\Contracts\Cache\Store $store, array $config = [])
* @method static void refreshEventDispatcher()
* @method static string getDefaultDriver()
Expand Down
12 changes: 12 additions & 0 deletions tests/Cache/CacheManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ public function testCustomDriverOverridesInternalDrivers()
$this->assertSame('mm(u_u)mm', $driver->flag);
}

public function testItCanBuildRepositories()
{
$app = $this->getApp([]);
$cacheManager = new CacheManager($app);

$arrayCache = $cacheManager->build(['driver' => 'array']);
$nullCache = $cacheManager->build(['driver' => 'null']);

$this->assertInstanceOf(ArrayStore::class, $arrayCache->getStore());
$this->assertInstanceOf(NullStore::class, $nullCache->getStore());
}

public function testItMakesRepositoryWhenContainerHasNoDispatcher()
{
$userConfig = [
Expand Down