19
19
*/
20
20
class Container implements ContainerInterface, \ArrayAccess
21
21
{
22
- /** @var string[] Lists the names of classes used by different container entries */
23
- protected $ types = [];
24
-
25
- /** @var array[] Cached container entry parameters for initializing the entries */
26
- protected $ parameters = [];
22
+ /** @var array[] Information about cached container entries */
23
+ protected $ entries = [];
27
24
28
25
/** @var EntryInterface[] Cached entries used to resolve values */
29
26
private $ entryCache ;
@@ -55,27 +52,27 @@ public function setDelegate(ContainerInterface $delegate): void
55
52
56
53
/**
57
54
* Returns PHP code for a cached container that can be loaded quickly on runtime.
55
+ * @param callable $encoder Encoder to turn the cached entry data into PHP code or null for default
58
56
* @return string The PHP code for the cached container
59
57
* @throws ContainerException If the container contains entries that cannot be cached
60
58
*/
61
- public function getCacheFile (): string
59
+ public function getCacheFile (callable $ encoder = null ): string
62
60
{
63
- $ this ->loadCacheParameters ();
61
+ if (\is_null ($ encoder )) {
62
+ $ encoder = function ($ value ): string {
63
+ return var_export ($ value , true );
64
+ };
65
+ }
64
66
65
- ksort ($ this ->types );
66
- ksort ($ this ->parameters );
67
+ $ this ->loadCacheParameters ();
68
+ ksort ($ this ->entries );
69
+ $ encoded = $ encoder ($ this ->entries );
67
70
68
- $ template = <<<' TEMPLATE'
71
+ return <<<TEMPLATE
69
72
<?php return new class extends \Simply\Container\Container {
70
- protected $types = ['TYPES'];
71
- protected $parameters = ['PARAMETERS'];
73
+ protected \$entries = $ encoded;
72
74
};
73
75
TEMPLATE ;
74
-
75
- return strtr ($ template , [
76
- "['TYPES'] " => var_export ($ this ->types , true ),
77
- "['PARAMETERS'] " => var_export ($ this ->parameters , true ),
78
- ]);
79
76
}
80
77
81
78
/**
@@ -84,7 +81,7 @@ public function getCacheFile(): string
84
81
*/
85
82
private function loadCacheParameters (): void
86
83
{
87
- foreach ($ this ->types as $ id => $ class ) {
84
+ foreach ($ this ->entries as $ id => $ data ) {
88
85
if (!isset ($ this ->entryCache [$ id ])) {
89
86
continue ;
90
87
}
@@ -95,7 +92,7 @@ private function loadCacheParameters(): void
95
92
throw new ContainerException ("Unable to cache entry ' $ id', the cache parameters are not static " );
96
93
}
97
94
98
- $ this ->parameters [$ id ] = $ parameters ;
95
+ $ this ->entries [$ id ] = [ \get_class ( $ this -> entryCache [ $ id ]), $ parameters] ;
99
96
}
100
97
}
101
98
@@ -127,11 +124,11 @@ private function isConstantValue($value): bool
127
124
*/
128
125
public function addEntry (string $ id , EntryInterface $ type ): void
129
126
{
130
- if (isset ($ this ->types [$ id ])) {
127
+ if (isset ($ this ->entries [$ id ])) {
131
128
throw new ContainerException ("Entry for identifier ' $ id' already exists " );
132
129
}
133
130
134
- $ this ->types [$ id ] = \get_class ( $ type ) ;
131
+ $ this ->entries [$ id ] = [] ;
135
132
$ this ->entryCache [$ id ] = $ type ;
136
133
}
137
134
@@ -165,20 +162,21 @@ public function get($id)
165
162
* @param string $id The entry identifier to look for
166
163
* @return EntryInterface The container entry for the given identifier
167
164
* @throws NotFoundExceptionInterface If the entry cannot be found
165
+ * @throws ContainerException If the cached type is not a valid entry type
168
166
*/
169
167
private function getEntry (string $ id ): EntryInterface
170
168
{
171
169
if (isset ($ this ->entryCache [$ id ])) {
172
170
return $ this ->entryCache [$ id ];
173
171
}
174
172
175
- if (isset ($ this ->types [$ id ])) {
176
- /** @var EntryInterface $entryClass */
177
- $ entryClass = $ this ->types [$ id ];
178
- return $ entryClass ::createFromCacheParameters ($ this ->parameters [$ id ]);
173
+ if (!isset ($ this ->entries [$ id ])) {
174
+ throw new NotFoundException ("No entry was found for the identifier ' $ id' " );
179
175
}
180
176
181
- throw new NotFoundException ("No entry was found for the identifier ' $ id' " );
177
+ /** @var EntryInterface $class */
178
+ [$ class , $ parameters ] = $ this ->entries [$ id ];
179
+ return $ class ::createFromCacheParameters ($ parameters );
182
180
}
183
181
184
182
/**
@@ -188,7 +186,7 @@ private function getEntry(string $id): EntryInterface
188
186
*/
189
187
public function has ($ id ): bool
190
188
{
191
- return isset ($ this ->types [$ id ]);
189
+ return isset ($ this ->entries [$ id ]);
192
190
}
193
191
194
192
/**
@@ -231,8 +229,7 @@ public function offsetSet($offset, $value): void
231
229
public function offsetUnset ($ offset ): void
232
230
{
233
231
unset(
234
- $ this ->types [$ offset ],
235
- $ this ->parameters [$ offset ],
232
+ $ this ->entries [$ offset ],
236
233
$ this ->entryCache [$ offset ],
237
234
$ this ->valueCache [$ offset ]
238
235
);
0 commit comments