Skip to content

Commit c573c3c

Browse files
Moved the container dumping section to the compilation page
1 parent c58d597 commit c573c3c

File tree

2 files changed

+81
-80
lines changed

2 files changed

+81
-80
lines changed

components/dependency_injection/compilation.rst

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Compiling the Container
1+
Compiling the Container
22
=======================
33

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

72+
Dumping the Configuration for Performance
73+
-----------------------------------------
74+
75+
Using configuration files to manage the service container can be much easier
76+
to understand than using PHP once there are a lot of services. This ease comes
77+
at a price though when it comes to performance as the config files need to be
78+
parsed and the PHP configuration built from them. The compilation process makes
79+
the container more efficient but it takes time to run. You can have the best of both
80+
worlds though by using configuration files and then dumping and caching the resulting
81+
configuration. The ``PhpDumper`` makes dumping the compiled container easy::
82+
83+
use Symfony\Component\DependencyInjection\ContainerBuilder;
84+
use Symfony\Component\Config\FileLocator;
85+
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
86+
use Symfony\Component\DependencyInjection\Dumper\PhpDumper
87+
88+
$container = new ContainerBuilder();
89+
$loader = new XmlFileLoader($container, new FileLocator(__DIR__));
90+
$loader->load('services.xml');
91+
92+
$file = __DIR__ .'/cache/container.php';
93+
94+
if (file_exists($file)) {
95+
require_once $file;
96+
$container = new ProjectServiceContiner();
97+
} else {
98+
$container = new ContainerBuilder();
99+
//--
100+
$container->compile();
101+
102+
$dumper = new PhpDumper($container);
103+
file_put_contents($file, $dumper->dump());
104+
}
105+
106+
``ProjectServiceContiner`` is the default name given to the dumped container
107+
class, you can change this though this with the ``class`` option when you dump
108+
it::
109+
110+
// ...
111+
$file = __DIR__ .'/cache/container.php';
112+
113+
if (file_exists($file)) {
114+
require_once $file;
115+
$container = new MyCachedContainer();
116+
} else {
117+
$container = new ContainerBuilder();
118+
//--
119+
$container->compile();
120+
121+
$dumper = new PhpDumper($container);
122+
file_put_contents($file, $dumper->dump(array('class' => 'MyCachedContainer')));
123+
}
124+
125+
You will now get the speed of the PHP configured container with the ease of using
126+
configuration files. In the above example you will need to delete the cached
127+
container file whenever you make any changes. Adding a check for a variable that
128+
determines if you are in debug mode allows you to keep the speed of the cached
129+
container in production but getting an up to date configuration whilst developing
130+
your application::
131+
132+
// ...
133+
134+
// set $isDebug based on something in your project
135+
136+
$file = __DIR__ .'/cache/container.php';
137+
138+
if (!$isDebug && file_exists($file)) {
139+
require_once $file;
140+
$container = new MyCachedContainer();
141+
} else {
142+
$container = new ContainerBuilder();
143+
//--
144+
$container->compile();
145+
146+
if(!$isDebug) {
147+
$dumper = new PhpDumper($container);
148+
file_put_contents($file, $dumper->dump(array('class' => 'MyCachedContainer')));
149+
}
150+
}
151+

components/dependency_injection/introduction.rst

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -279,85 +279,6 @@ 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-
Dumping the Configuration for Performance
283-
-----------------------------------------
284-
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-
}
360-
361282
Learn more about this Component
362283
-------------------------------
363284

0 commit comments

Comments
 (0)