Skip to content

Commit

Permalink
[11.x] Add ability to dynamically build cache repositories on-demand …
Browse files Browse the repository at this point in the history
…using `Cache::build` (#53454)

* Add Cache::build method

* Add test
  • Loading branch information
stevebauman authored Nov 11, 2024
1 parent ecf67a6 commit 7784219
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
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

0 comments on commit 7784219

Please sign in to comment.