Skip to content

Commit

Permalink
Merge branch 'moving_container_dumping_section' of https://github.com…
Browse files Browse the repository at this point in the history
…/richardmiller/symfony-docs into richardmiller-moving_container_dumping_section
  • Loading branch information
weaverryan committed May 25, 2012
2 parents 3ece131 + a178c03 commit 59e4233
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 80 deletions.
82 changes: 81 additions & 1 deletion components/dependency_injection/compilation.rst
@@ -1,4 +1,4 @@
Compiling the Container
Compiling the Container
=======================

The service container can be compiled for various reasons. These reasons
Expand Down Expand Up @@ -109,3 +109,83 @@ but are processed when the container's ``compile`` method is called.
You should instead use a compiler pass which works with the full container
after the extensions have been processed.

Dumping the Configuration for Performance
-----------------------------------------

Using configuration files to manage the service container can be much easier
to understand than using PHP once there are a lot of services. This ease comes
at a price though when it comes to performance as the config files need to be
parsed and the PHP configuration built from them. The compilation process makes
the container more efficient but it takes time to run. You can have the best of both
worlds though by using configuration files and then dumping and caching the resulting
configuration. The ``PhpDumper`` makes dumping the compiled container easy::

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Dumper\PhpDumper

$container = new ContainerBuilder();
$loader = new XmlFileLoader($container, new FileLocator(__DIR__));
$loader->load('services.xml');

$file = __DIR__ .'/cache/container.php';

if (file_exists($file)) {
require_once $file;
$container = new ProjectServiceContiner();
} else {
$container = new ContainerBuilder();
//--
$container->compile();

$dumper = new PhpDumper($container);
file_put_contents($file, $dumper->dump());
}

``ProjectServiceContiner`` is the default name given to the dumped container
class, you can change this though this with the ``class`` option when you dump
it::

// ...
$file = __DIR__ .'/cache/container.php';

if (file_exists($file)) {
require_once $file;
$container = new MyCachedContainer();
} else {
$container = new ContainerBuilder();
//--
$container->compile();

$dumper = new PhpDumper($container);
file_put_contents($file, $dumper->dump(array('class' => 'MyCachedContainer')));
}

You will now get the speed of the PHP configured container with the ease of using
configuration files. In the above example you will need to delete the cached
container file whenever you make any changes. Adding a check for a variable that
determines if you are in debug mode allows you to keep the speed of the cached
container in production but getting an up to date configuration whilst developing
your application::

// ...

// set $isDebug based on something in your project

$file = __DIR__ .'/cache/container.php';

if (!$isDebug && file_exists($file)) {
require_once $file;
$container = new MyCachedContainer();
} else {
$container = new ContainerBuilder();
//--
$container->compile();

if(!$isDebug)
$dumper = new PhpDumper($container);
file_put_contents($file, $dumper->dump(array('class' => 'MyCachedContainer')));
}
}

79 changes: 0 additions & 79 deletions components/dependency_injection/introduction.rst
Expand Up @@ -279,85 +279,6 @@ The ``newsletter_manager`` and ``mailer`` services can be set up using config fi
$container->register('newsletter_manager', 'NewsletterManager')
->addMethodCall('setMailer', new Reference('mailer');
Dumping the Configuration for Performance
-----------------------------------------

Using configuration files to manage the service container can be much easier
to understand than using PHP once there are a lot of services. This ease comes
at a price though when it comes to performance as the config files need to be
parsed and the PHP configuration built from them. You can have the best of both
worlds though by using configuration files and then dumping and caching the resulting
configuration. The ``PhpDumper`` makes dumping the compiled container easy::

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Dumper\PhpDumper

$container = new ContainerBuilder();
$loader = new XmlFileLoader($container, new FileLocator(__DIR__));
$loader->load('services.xml');

$file = __DIR__ .'/cache/container.php';

if (file_exists($file)) {
require_once $file;
$container = new ProjectServiceContiner();
} else {
$container = new ContainerBuilder();
$loader = new XmlFileLoader($container, new FileLocator(__DIR__));
$loader->load('services.xml');

$dumper = new PhpDumper($container);
file_put_contents($file, $dumper->dump());
}

``ProjectServiceContiner`` is the default name given to the dumped container
class, you can change this though this with the ``class`` option when you dump
it::

// ...
$file = __DIR__ .'/cache/container.php';

if (file_exists($file)) {
require_once $file;
$container = new MyCachedContainer();
} else {
$container = new ContainerBuilder();
$loader = new XmlFileLoader($container, new FileLocator(__DIR__));
$loader->load('services.xml');

$dumper = new PhpDumper($container);
file_put_contents($file, $dumper->dump(array('class' => 'MyCachedContainer')));
}

You will now get the speed of the PHP configured container with the ease of using
configuration files. In the above example you will need to delete the cached
container file whenever you make any changes. Adding a check for a variable that
determines if you are in debug mode allows you to keep the speed of the cached
container in production but getting an up to date configuration whilst developing
your application::

// ...

// set $isDebug based on something in your project

$file = __DIR__ .'/cache/container.php';

if (!$isDebug && file_exists($file)) {
require_once $file;
$container = new MyCachedContainer();
} else {
$container = new ContainerBuilder();
$loader = new XmlFileLoader($container, new FileLocator(__DIR__));
$loader->load('services.xml');

if(!$isDebug) {
$dumper = new PhpDumper($container);
file_put_contents($file, $dumper->dump(array('class' => 'MyCachedContainer')));
}
}

Learn more about this Component
-------------------------------

Expand Down

0 comments on commit 59e4233

Please sign in to comment.