Skip to content

Commit 677bd46

Browse files
committed
OrmCache: simplify cache drivers
1 parent 430d5c1 commit 677bd46

File tree

1 file changed

+33
-52
lines changed

1 file changed

+33
-52
lines changed

src/DI/OrmCacheExtension.php

+33-52
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,6 @@
1818
class OrmCacheExtension extends AbstractExtension
1919
{
2020

21-
/** @var Definition|string|null */
22-
private $defaultDriverDef;
23-
24-
private function getServiceSchema(): Schema
25-
{
26-
return Expect::anyOf(
27-
Expect::string(),
28-
Expect::array(),
29-
Expect::type(Statement::class)
30-
)->nullable();
31-
}
32-
3321
public function getConfigSchema(): Schema
3422
{
3523
return Expect::structure([
@@ -54,13 +42,22 @@ public function loadConfiguration(): void
5442
$this->loadSecondLevelCacheConfiguration();
5543
}
5644

45+
private function getServiceSchema(): Schema
46+
{
47+
return Expect::anyOf(
48+
Expect::string(),
49+
Expect::array(),
50+
Expect::type(Statement::class)
51+
)->nullable();
52+
}
53+
5754
private function loadQueryCacheConfiguration(): void
5855
{
5956
$config = $this->config;
6057
$configurationDef = $this->getConfigurationDef();
6158

6259
$configurationDef->addSetup('setQueryCacheImpl', [
63-
$this->loadSpecificDriver($config->queryCache, 'queryCache'),
60+
$this->buildCacheDriver($config->queryCache, 'queryCache'),
6461
]);
6562
}
6663

@@ -70,7 +67,7 @@ private function loadResultCacheConfiguration(): void
7067
$configurationDef = $this->getConfigurationDef();
7168

7269
$configurationDef->addSetup('setResultCacheImpl', [
73-
$this->loadSpecificDriver($config->resultCache, 'resultCache'),
70+
$this->buildCacheDriver($config->resultCache, 'resultCache'),
7471
]);
7572
}
7673

@@ -80,7 +77,7 @@ private function loadHydrationCacheConfiguration(): void
8077
$configurationDef = $this->getConfigurationDef();
8178

8279
$configurationDef->addSetup('setHydrationCacheImpl', [
83-
$this->loadSpecificDriver($config->hydrationCache, 'hydrationCache'),
80+
$this->buildCacheDriver($config->hydrationCache, 'hydrationCache'),
8481
]);
8582
}
8683

@@ -90,7 +87,7 @@ private function loadMetadataCacheConfiguration(): void
9087
$configurationDef = $this->getConfigurationDef();
9188

9289
$configurationDef->addSetup('setMetadataCacheImpl', [
93-
$this->loadSpecificDriver($config->metadataCache, 'metadataCache'),
90+
$this->buildCacheDriver($config->metadataCache, 'metadataCache'),
9491
]);
9592
}
9693

@@ -101,7 +98,8 @@ private function loadSecondLevelCacheConfiguration(): void
10198
$configurationDef = $this->getConfigurationDef();
10299

103100
if ($config->secondLevelCache !== null) {
104-
$cacheConfigurationDef = $this->getHelper()->getDefinitionFromConfig($config->secondLevelCache, $this->prefix('cacheConfiguration'));
101+
$cacheConfigurationDef = $builder->addDefinition($this->prefix('cacheConfiguration'))
102+
->setFactory($config->secondLevelCache);
105103
} else {
106104
$regionsDef = $builder->addDefinition($this->prefix('regions'))
107105
->setFactory(RegionsConfiguration::class)
@@ -111,7 +109,7 @@ private function loadSecondLevelCacheConfiguration(): void
111109
->setFactory(DefaultCacheFactory::class)
112110
->setArguments([
113111
$regionsDef,
114-
$this->loadSpecificDriver(null, 'secondLevelCache'),
112+
$this->buildCacheDriver(null, 'secondLevelCache'),
115113
])
116114
->setAutowired(false);
117115

@@ -129,49 +127,32 @@ private function loadSecondLevelCacheConfiguration(): void
129127

130128
/**
131129
* @param string|mixed[]|Statement|null $config
132-
* @return Definition|string
133-
*/
134-
private function loadSpecificDriver($config, string $prefix)
135-
{
136-
if ($config !== null && $config !== []) { // Nette converts explicit null to an empty array
137-
$driverName = $this->prefix($prefix);
138-
$driverDef = $this->getHelper()->getDefinitionFromConfig($config, $driverName);
139-
140-
// If service is extension specific, then disable autowiring
141-
if ($driverDef instanceof Definition && $driverDef->getName() === $driverName) {
142-
$driverDef->setAutowired(false);
143-
}
144-
145-
return $driverDef;
146-
}
147-
148-
return $this->loadDefaultDriver();
149-
}
150-
151-
/**
152-
* @return Definition|string
153130
*/
154-
private function loadDefaultDriver()
131+
private function buildCacheDriver(string|array|Statement|null $config, string $prefix): Definition|string
155132
{
156-
$config = $this->config;
133+
$builder = $this->getContainerBuilder();
157134

158-
if ($this->defaultDriverDef !== null) {
159-
return $this->defaultDriverDef;
135+
// Driver is defined
136+
if ($config !== null && $config !== []) { // Nette converts explicit null to an empty array
137+
return $builder->addDefinition($this->prefix($prefix))
138+
->setFactory($config)
139+
->setAutowired(false);
160140
}
161141

162-
if ($config->defaultDriver === null || $config->defaultDriver === []) { // Nette converts explicit null to an empty array
163-
return $this->defaultDriverDef = '@' . Cache::class;
142+
// If there is default cache, don't create it
143+
if ($builder->hasDefinition($this->prefix('defaultCache'))) {
144+
return $builder->getDefinition($this->prefix('defaultCache'));
164145
}
165146

166-
$defaultDriverName = $this->prefix('defaultCache');
167-
$this->defaultDriverDef = $defaultDriverDef = $this->getHelper()->getDefinitionFromConfig($config->defaultDriver, $defaultDriverName);
168-
169-
// If service is extension specific, then disable autowiring
170-
if ($defaultDriverDef instanceof Definition && $defaultDriverDef->getName() === $defaultDriverName) {
171-
$defaultDriverDef->setAutowired(false);
147+
// Create default driver
148+
if ($this->config->defaultDriver !== null && $this->config->defaultDriver !== []) { // Nette converts explicit null to an empty array
149+
return $builder->addDefinition($this->prefix('defaultCache'))
150+
->setFactory($this->config->defaultDriver)
151+
->setAutowired(false);
172152
}
173153

174-
return $defaultDriverDef;
154+
// No default driver provider, fallback to Cache::class
155+
return '@' . Cache::class;
175156
}
176157

177158
}

0 commit comments

Comments
 (0)