Skip to content

Commit 59e4233

Browse files
committed
Merge branch 'moving_container_dumping_section' of https://github.com/richardmiller/symfony-docs into richardmiller-moving_container_dumping_section
2 parents 3ece131 + a178c03 commit 59e4233

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
@@ -109,3 +109,83 @@ but are processed when the container's ``compile`` method is called.
109109
You should instead use a compiler pass which works with the full container
110110
after the extensions have been processed.
111111

112+
Dumping the Configuration for Performance
113+
-----------------------------------------
114+
115+
Using configuration files to manage the service container can be much easier
116+
to understand than using PHP once there are a lot of services. This ease comes
117+
at a price though when it comes to performance as the config files need to be
118+
parsed and the PHP configuration built from them. The compilation process makes
119+
the container more efficient but it takes time to run. You can have the best of both
120+
worlds though by using configuration files and then dumping and caching the resulting
121+
configuration. The ``PhpDumper`` makes dumping the compiled container easy::
122+
123+
use Symfony\Component\DependencyInjection\ContainerBuilder;
124+
use Symfony\Component\Config\FileLocator;
125+
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
126+
use Symfony\Component\DependencyInjection\Dumper\PhpDumper
127+
128+
$container = new ContainerBuilder();
129+
$loader = new XmlFileLoader($container, new FileLocator(__DIR__));
130+
$loader->load('services.xml');
131+
132+
$file = __DIR__ .'/cache/container.php';
133+
134+
if (file_exists($file)) {
135+
require_once $file;
136+
$container = new ProjectServiceContiner();
137+
} else {
138+
$container = new ContainerBuilder();
139+
//--
140+
$container->compile();
141+
142+
$dumper = new PhpDumper($container);
143+
file_put_contents($file, $dumper->dump());
144+
}
145+
146+
``ProjectServiceContiner`` is the default name given to the dumped container
147+
class, you can change this though this with the ``class`` option when you dump
148+
it::
149+
150+
// ...
151+
$file = __DIR__ .'/cache/container.php';
152+
153+
if (file_exists($file)) {
154+
require_once $file;
155+
$container = new MyCachedContainer();
156+
} else {
157+
$container = new ContainerBuilder();
158+
//--
159+
$container->compile();
160+
161+
$dumper = new PhpDumper($container);
162+
file_put_contents($file, $dumper->dump(array('class' => 'MyCachedContainer')));
163+
}
164+
165+
You will now get the speed of the PHP configured container with the ease of using
166+
configuration files. In the above example you will need to delete the cached
167+
container file whenever you make any changes. Adding a check for a variable that
168+
determines if you are in debug mode allows you to keep the speed of the cached
169+
container in production but getting an up to date configuration whilst developing
170+
your application::
171+
172+
// ...
173+
174+
// set $isDebug based on something in your project
175+
176+
$file = __DIR__ .'/cache/container.php';
177+
178+
if (!$isDebug && file_exists($file)) {
179+
require_once $file;
180+
$container = new MyCachedContainer();
181+
} else {
182+
$container = new ContainerBuilder();
183+
//--
184+
$container->compile();
185+
186+
if(!$isDebug)
187+
$dumper = new PhpDumper($container);
188+
file_put_contents($file, $dumper->dump(array('class' => 'MyCachedContainer')));
189+
}
190+
}
191+

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)