@@ -161,11 +161,13 @@ it::
161
161
}
162
162
163
163
You will now get the speed of the PHP configured container with the ease of using
164
- configuration files. In the above example you will need to delete the cached
165
- container file whenever you make any changes. Adding a check for a variable that
166
- determines if you are in debug mode allows you to keep the speed of the cached
167
- container in production but getting an up to date configuration whilst developing
168
- your application::
164
+ configuration files. Additionally dumping the container in this way further optimizes
165
+ how the services are created by the container.
166
+
167
+ In the above example you will need to delete the cached container file whenever
168
+ you make any changes. Adding a check for a variable that determines if you are
169
+ in debug mode allows you to keep the speed of the cached container in production
170
+ but getting an up to date configuration whilst developing your application::
169
171
170
172
// ...
171
173
@@ -187,3 +189,44 @@ your application::
187
189
}
188
190
}
189
191
192
+ This could be further improved by only recompiling the container in debug
193
+ mode when changes have been made to its configuration rather than on every
194
+ request. This can be done by caching the resource files used to configure
195
+ the container in the way describe in ":doc: `/components/conf/caching `"
196
+ in the config component documentation.
197
+
198
+ You do not need to work out which files to cache as the container builder
199
+ keeps track of all the resources used to configure it, not just the configuration
200
+ files but the extension classes and compiler passes as well. This means that
201
+ any changes to any of these files will invalidate the cache and trigger the
202
+ container being rebuilt. You just need to ask the container for these resources
203
+ and use them as metadata for the cache::
204
+
205
+ // ...
206
+
207
+ // set $isDebug based on something in your project
208
+
209
+ $file = __DIR__ .'/cache/container.php';
210
+ $containerConfigCache = new ConfigCache($file, $isDebug);
211
+
212
+ if (!$cache->isFresh()) {
213
+ $containerBuilder = new ContainerBuilder();
214
+ //--
215
+ $container->compile();
216
+
217
+ $dumper = new PhpDumper($containerBuilder);
218
+ $containerConfigCache->write(
219
+ $dumper->dump(array('class' => 'MyCachedContainer')),
220
+ $containerBuilder->getResources()
221
+ );
222
+ }
223
+
224
+ require_once $file;
225
+ $container = new MyCachedContainer();
226
+
227
+ Now the cached dumped container is used regardless of whether debug mode is on or not.
228
+ The difference is that the ``ConfigCache `` is set to debug mode with its second
229
+ constructor argument. When the cache is not in debug mode the cached container
230
+ will always be used if it exists. In debug mode, an additional metadata file
231
+ is written with the timestamps of all the resource files. These are then checked
232
+ to see if the files have changed, if they have the cache will be considered stale.
0 commit comments