Skip to content

Commit d00a3c8

Browse files
committed
Fixed usage of default configuration in PHP
1 parent f218dcd commit d00a3c8

File tree

3 files changed

+96
-30
lines changed

3 files changed

+96
-30
lines changed

service_container.rst

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -215,13 +215,19 @@ each time you ask for it.
215215
.. code-block:: php
216216
217217
// app/config/services.php
218-
// _defaults and loading entire directories is not possible with PHP configuration
219-
// you need to define your services one-by-one
220-
use AppBundle\Service\MessageGenerator;
218+
use Symfony\Component\DependencyInjection\Definition;
221219
222-
$container->autowire(MessageGenerator::class)
220+
// To use as default template
221+
$definition = new Definition();
222+
223+
$definition
224+
->setAutowired(true)
223225
->setAutoconfigured(true)
224-
->setPublic(false);
226+
->setPublic(false)
227+
;
228+
229+
// $this is a reference to the current loader
230+
$this->registerClasses($definition, 'AppBundle\\', '../../src/AppBundle/*', '../../src/AppBundle/{Entity,Repository}');
225231
226232
Thanks to this configuration, you can automatically use any classes from the
227233
``src/AppBundle`` directory as a service, without needing to manually configure
@@ -475,12 +481,21 @@ pass here. No problem! In your configuration, you can explicitly set this argume
475481
476482
// app/config/services.php
477483
use AppBundle\Updates\SiteUpdateManager;
484+
use Symfony\Component\DependencyInjection\Definition;
478485
479-
// _defaults and importing directories does not work in PHP
480-
// but registering a service explicitly does
481-
$container->autowire(SiteUpdateManager::class)
486+
// Same as before
487+
$definition = new Definition();
488+
489+
$definition
490+
->setAutowired(true)
482491
->setAutoconfigured(true)
483492
->setPublic(false)
493+
;
494+
495+
$this->registerClasses($definition, 'AppBundle\\', '../../src/AppBundle/*', '../../src/AppBundle/{Entity,Repository}');
496+
497+
// Explicitly configure the service
498+
$container->getDefinition(SiteUpdateManager::class)
484499
->setArgument('$adminEmail', 'manager@example.com');
485500
486501
.. versionadded:: 3.3
@@ -860,6 +875,31 @@ key. For example, the default Symfony configuration contains this:
860875
</services>
861876
</container>
862877
878+
.. code-block:: php
879+
880+
// app/config/services.php
881+
use Symfony\Component\DependencyInjection\Definition;
882+
883+
// To use as default template
884+
$definition = new Definition();
885+
886+
$definition
887+
->setAutowired(true)
888+
->setAutoconfigured(true)
889+
->setPublic(false)
890+
;
891+
892+
$this->registerClasses($definition, 'AppBundle\\', '../../src/AppBundle/*', '../../src/AppBundle/{Entity,Repository}');
893+
894+
// Changes default config
895+
$definition
896+
->setPublic(true)
897+
->addTag('controller.service_arguments')
898+
;
899+
900+
// $this is a reference to the current loader
901+
$this->registerClasses($definition, 'AppBundle\\Controller\\', '../../src/AppBundle/Controller/*');
902+
863903
This can be used to quickly make many classes available as services and apply some
864904
default configuration. The ``id`` of each service is its fully-qualified class name.
865905
You can override any service that's imported by using its id (class name) below

service_container/3.3-di-changes.rst

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -81,21 +81,37 @@ Symfony Standard Edition:
8181
<prototype namespace="AppBundle\Controller\" resource="../../src/AppBundle/Controller" public="true">
8282
<tag name="controller.service_arguments" />
8383
</prototype>
84+
85+
<!-- add more services, or override services that need manual wiring -->
8486
</services>
8587
</container>
8688
8789
.. code-block:: php
8890
8991
// app/config/services.php
92+
use Symfony\Component\DependencyInjection\Definition;
9093
91-
// _defaults and loading entire directories is not possible with PHP configuration
92-
// you need to define your services one-by-one
93-
use AppBundle\Controller\DefaultController;
94+
// To use as default template
95+
$definition = new Definition();
9496
95-
$container->autowire(DefaultController::class)
97+
$definition
98+
->setAutowired(true)
9699
->setAutoconfigured(true)
100+
->setPublic(false)
101+
;
102+
103+
$this->registerClasses($definition, 'AppBundle\\', '../../src/AppBundle/*', '../../src/AppBundle/{Entity,Repository}');
104+
105+
// Changes default config
106+
$definition
107+
->setPublic(true)
97108
->addTag('controller.service_arguments')
98-
->setPublic(true);
109+
;
110+
111+
// $this is a reference to the current loader
112+
$this->registerClasses($definition, 'AppBundle\\Controller\\', '../../src/AppBundle/Controller/*');
113+
114+
// add more services, or override services that need manual wiring
99115
100116
This small bit of configuration contains a paradigm shift of how services
101117
are configured in Symfony.
@@ -147,9 +163,18 @@ thanks to the following config:
147163
.. code-block:: php
148164
149165
// app/config/services.php
166+
use Symfony\Component\DependencyInjection\Definition;
150167
151-
// services cannot be automatically loaded with PHP configuration
152-
// you need to define your services one-by-one
168+
// To use as default template
169+
$definition = new Definition();
170+
171+
$definition
172+
->setAutowired(true)
173+
->setAutoconfigured(true)
174+
->setPublic(false)
175+
;
176+
177+
$this->registerClasses($definition, 'AppBundle\\', '../../src/AppBundle/*', '../../src/AppBundle/{Entity,Repository}');
153178
154179
This means that every class in ``src/AppBundle/`` is *available* to be used as a
155180
service. And thanks to the ``_defaults`` section at the top of the file, all of
@@ -347,15 +372,14 @@ The third big change is that, in a new Symfony 3.3 project, your controllers are
347372
.. code-block:: php
348373
349374
// app/config/services.php
375+
use Symfony\Component\DependencyInjection\Definition;
350376
351-
// loading entire directories is not possible with PHP configuration
352-
// you need to define your services one-by-one
353-
use AppBundle\Controller\DefaultController;
377+
// To use as default template
378+
$definition = new Definition();
354379
355-
$container->autowire(DefaultController::class)
356-
->setAutoconfigured(true)
357-
->addTag('controller.service_arguments')
358-
->setPublic(true);
380+
$definition->setPublic(true);
381+
382+
$this->registerClasses($definition, 'AppBundle\\Controller\\', '../../src/AppBundle/Controller/*');
359383
360384
But, you might not even notice this. First, your controllers *can* still extend
361385
the same base ``Controller`` class or a new :ref:`AbstractController <controller-abstract-versus-controller>`.
@@ -642,7 +666,7 @@ You're now ready to automatically register all services in ``src/AppBundle/``
642666
+ AppBundle\:
643667
+ resource: '../../src/AppBundle/*'
644668
+ exclude: '../../src/AppBundle/{Entity,Repository}'
645-
+
669+
+
646670
+ AppBundle\Controller\:
647671
+ resource: '../../src/AppBundle/Controller'
648672
+ public: true

service_container/configurators.rst

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,20 +166,22 @@ all the classes are already loaded as services. All you need to do is specify th
166166
.. code-block:: php
167167
168168
// app/config/services.php
169-
use AppBundle\Mail\EmailConfigurator;
170-
use AppBundle\Mail\EmailFormatterManager;
171169
use AppBundle\Mail\GreetingCardManager;
172170
use AppBundle\Mail\NewsletterManager;
171+
use Symfony\Component\DependencyInjection\Definition;
173172
use Symfony\Component\DependencyInjection\Reference;
174173
175-
// ...
176-
$container->autowire(EmailFormatterManager::class);
177-
$container->autowire(EmailConfigurator::class);
174+
// Same as before
175+
$definition = new Definition();
176+
177+
$definition->setAutowired(true);
178+
179+
$this->registerClasses($definition, 'AppBundle\\', '../../src/AppBundle/*');
178180
179-
$container->autowire(NewsletterManager::class)
181+
$container->getDefinition(NewsletterManager::class)
180182
->setConfigurator(array(new Reference(EmailConfigurator::class), 'configure'));
181183
182-
$container->autowire(GreetingCardManager::class)
184+
$container->getDefinition(GreetingCardManager::class)
183185
->setConfigurator(array(new Reference(EmailConfigurator::class), 'configure'));
184186
185187

0 commit comments

Comments
 (0)