18
18
class OrmCacheExtension extends AbstractExtension
19
19
{
20
20
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
-
33
21
public function getConfigSchema (): Schema
34
22
{
35
23
return Expect::structure ([
@@ -54,13 +42,22 @@ public function loadConfiguration(): void
54
42
$ this ->loadSecondLevelCacheConfiguration ();
55
43
}
56
44
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
+
57
54
private function loadQueryCacheConfiguration (): void
58
55
{
59
56
$ config = $ this ->config ;
60
57
$ configurationDef = $ this ->getConfigurationDef ();
61
58
62
59
$ configurationDef ->addSetup ('setQueryCacheImpl ' , [
63
- $ this ->loadSpecificDriver ($ config ->queryCache , 'queryCache ' ),
60
+ $ this ->buildCacheDriver ($ config ->queryCache , 'queryCache ' ),
64
61
]);
65
62
}
66
63
@@ -70,7 +67,7 @@ private function loadResultCacheConfiguration(): void
70
67
$ configurationDef = $ this ->getConfigurationDef ();
71
68
72
69
$ configurationDef ->addSetup ('setResultCacheImpl ' , [
73
- $ this ->loadSpecificDriver ($ config ->resultCache , 'resultCache ' ),
70
+ $ this ->buildCacheDriver ($ config ->resultCache , 'resultCache ' ),
74
71
]);
75
72
}
76
73
@@ -80,7 +77,7 @@ private function loadHydrationCacheConfiguration(): void
80
77
$ configurationDef = $ this ->getConfigurationDef ();
81
78
82
79
$ configurationDef ->addSetup ('setHydrationCacheImpl ' , [
83
- $ this ->loadSpecificDriver ($ config ->hydrationCache , 'hydrationCache ' ),
80
+ $ this ->buildCacheDriver ($ config ->hydrationCache , 'hydrationCache ' ),
84
81
]);
85
82
}
86
83
@@ -90,7 +87,7 @@ private function loadMetadataCacheConfiguration(): void
90
87
$ configurationDef = $ this ->getConfigurationDef ();
91
88
92
89
$ configurationDef ->addSetup ('setMetadataCacheImpl ' , [
93
- $ this ->loadSpecificDriver ($ config ->metadataCache , 'metadataCache ' ),
90
+ $ this ->buildCacheDriver ($ config ->metadataCache , 'metadataCache ' ),
94
91
]);
95
92
}
96
93
@@ -101,7 +98,8 @@ private function loadSecondLevelCacheConfiguration(): void
101
98
$ configurationDef = $ this ->getConfigurationDef ();
102
99
103
100
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 );
105
103
} else {
106
104
$ regionsDef = $ builder ->addDefinition ($ this ->prefix ('regions ' ))
107
105
->setFactory (RegionsConfiguration::class)
@@ -111,7 +109,7 @@ private function loadSecondLevelCacheConfiguration(): void
111
109
->setFactory (DefaultCacheFactory::class)
112
110
->setArguments ([
113
111
$ regionsDef ,
114
- $ this ->loadSpecificDriver (null , 'secondLevelCache ' ),
112
+ $ this ->buildCacheDriver (null , 'secondLevelCache ' ),
115
113
])
116
114
->setAutowired (false );
117
115
@@ -129,49 +127,32 @@ private function loadSecondLevelCacheConfiguration(): void
129
127
130
128
/**
131
129
* @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
153
130
*/
154
- private function loadDefaultDriver ()
131
+ private function buildCacheDriver ( string | array | Statement | null $ config , string $ prefix ): Definition | string
155
132
{
156
- $ config = $ this ->config ;
133
+ $ builder = $ this ->getContainerBuilder () ;
157
134
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 );
160
140
}
161
141
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 ' ));
164
145
}
165
146
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 );
172
152
}
173
153
174
- return $ defaultDriverDef ;
154
+ // No default driver provider, fallback to Cache::class
155
+ return '@ ' . Cache::class;
175
156
}
176
157
177
158
}
0 commit comments