Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ff548b2

Browse filesBrowse files
committed
Merge remote-tracking branch 'origin/2.0' into 2.0
Conflicts: components/dependency_injection/introduction.rst
2 parents 59e4233 + 8c1de8b commit ff548b2
Copy full SHA for ff548b2

File tree

Expand file treeCollapse file tree

1 file changed

+77
-5
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+77
-5
lines changed

‎components/dependency_injection/introduction.rst

Copy file name to clipboardExpand all lines: components/dependency_injection/introduction.rst
+77-5Lines changed: 77 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,81 @@ The ``newsletter_manager`` and ``mailer`` services can be set up using config fi
279279
$container->register('newsletter_manager', 'NewsletterManager')
280280
->addMethodCall('setMailer', new Reference('mailer');
281281
282-
Learn more about this Component
283-
-------------------------------
282+
Dumping the Configuration for Performance
283+
-----------------------------------------
284284

285-
* :doc:`/components/dependency_injection/definitions`
286-
* :doc:`/components/dependency_injection/factories`
287-
* :doc:`/components/dependency_injection/parentservices`
285+
Using configuration files to manage the service container can be much easier
286+
to understand than using PHP once there are a lot of services. This ease comes
287+
at a price though when it comes to performance as the config files need to be
288+
parsed and the PHP configuration built from them. You can have the best of both
289+
worlds though by using configuration files and then dumping and caching the resulting
290+
configuration. The ``PhpDumper`` makes dumping the compiled container easy::
291+
292+
use Symfony\Component\DependencyInjection\ContainerBuilder;
293+
use Symfony\Component\Config\FileLocator;
294+
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
295+
use Symfony\Component\DependencyInjection\Dumper\PhpDumper
296+
297+
$container = new ContainerBuilder();
298+
$loader = new XmlFileLoader($container, new FileLocator(__DIR__));
299+
$loader->load('services.xml');
300+
301+
$file = __DIR__ .'/cache/container.php';
302+
303+
if (file_exists($file)) {
304+
require_once $file;
305+
$container = new ProjectServiceContiner();
306+
} else {
307+
$container = new ContainerBuilder();
308+
$loader = new XmlFileLoader($container, new FileLocator(__DIR__));
309+
$loader->load('services.xml');
310+
311+
$dumper = new PhpDumper($container);
312+
file_put_contents($file, $dumper->dump());
313+
}
314+
315+
``ProjectServiceContiner`` is the default name given to the dumped container
316+
class, you can change this though this with the ``class`` option when you dump
317+
it::
318+
319+
// ...
320+
$file = __DIR__ .'/cache/container.php';
321+
322+
if (file_exists($file)) {
323+
require_once $file;
324+
$container = new MyCachedContainer();
325+
} else {
326+
$container = new ContainerBuilder();
327+
$loader = new XmlFileLoader($container, new FileLocator(__DIR__));
328+
$loader->load('services.xml');
329+
330+
$dumper = new PhpDumper($container);
331+
file_put_contents($file, $dumper->dump(array('class' => 'MyCachedContainer')));
332+
}
333+
334+
You will now get the speed of the PHP configured container with the ease of using
335+
configuration files. In the above example you will need to delete the cached
336+
container file whenever you make any changes. Adding a check for a variable that
337+
determines if you are in debug mode allows you to keep the speed of the cached
338+
container in production but getting an up to date configuration whilst developing
339+
your application::
340+
341+
// ...
342+
343+
// set $isDebug based on something in your project
344+
345+
$file = __DIR__ .'/cache/container.php';
346+
347+
if (!$isDebug && file_exists($file)) {
348+
require_once $file;
349+
$container = new MyCachedContainer();
350+
} else {
351+
$container = new ContainerBuilder();
352+
$loader = new XmlFileLoader($container, new FileLocator(__DIR__));
353+
$loader->load('services.xml');
354+
355+
if(!$isDebug) {
356+
$dumper = new PhpDumper($container);
357+
file_put_contents($file, $dumper->dump(array('class' => 'MyCachedContainer')));
358+
}
359+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.