Skip to content

Commit 7784219

Browse files
authored
[11.x] Add ability to dynamically build cache repositories on-demand using Cache::build (#53454)
* Add Cache::build method * Add test
1 parent ecf67a6 commit 7784219

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

src/Illuminate/Cache/CacheManager.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,19 @@ public function resolve($name)
9090

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

93+
return $this->build($config);
94+
}
95+
96+
/**
97+
* Build a cache repository with the given configuration.
98+
*
99+
* @param array $config
100+
* @return \Illuminate\Cache\Repository
101+
*/
102+
public function build(array $config)
103+
{
104+
$config = Arr::add($config, 'store', $config['name'] ?? 'ondemand');
105+
93106
if (isset($this->customCreators[$config['driver']])) {
94107
return $this->callCustomCreator($config);
95108
}

src/Illuminate/Support/Facades/Cache.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* @method static \Illuminate\Contracts\Cache\Repository store(string|null $name = null)
77
* @method static \Illuminate\Contracts\Cache\Repository driver(string|null $driver = null)
88
* @method static \Illuminate\Contracts\Cache\Repository resolve(string $name)
9+
* @method static \Illuminate\Contracts\Cache\Repository build(array $config)
910
* @method static \Illuminate\Cache\Repository repository(\Illuminate\Contracts\Cache\Store $store, array $config = [])
1011
* @method static void refreshEventDispatcher()
1112
* @method static string getDefaultDriver()

tests/Cache/CacheManagerTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ public function testCustomDriverOverridesInternalDrivers()
5959
$this->assertSame('mm(u_u)mm', $driver->flag);
6060
}
6161

62+
public function testItCanBuildRepositories()
63+
{
64+
$app = $this->getApp([]);
65+
$cacheManager = new CacheManager($app);
66+
67+
$arrayCache = $cacheManager->build(['driver' => 'array']);
68+
$nullCache = $cacheManager->build(['driver' => 'null']);
69+
70+
$this->assertInstanceOf(ArrayStore::class, $arrayCache->getStore());
71+
$this->assertInstanceOf(NullStore::class, $nullCache->getStore());
72+
}
73+
6274
public function testItMakesRepositoryWhenContainerHasNoDispatcher()
6375
{
6476
$userConfig = [

0 commit comments

Comments
 (0)