Skip to content

Commit 497938b

Browse files
ragboyjrxabbuh
authored andcommitted
[DependencyInjection] Fluent PHP DI Documentation
- Updated DI configuration references to include php-fluent-di - Updated _build/conf.py to include codeblock for php-fluent-di - Updated existing di resource loading to refer to latest sf 4.2 excludes Signed-off-by: RJ Garcia <rj@bighead.net>
1 parent f3ef5de commit 497938b

20 files changed

+667
-345
lines changed

components/dependency_injection.rst

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -288,17 +288,21 @@ config files:
288288
289289
.. code-block:: php
290290
291-
use Symfony\Component\DependencyInjection\Reference;
291+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
292+
293+
return function(ContainerConfigurator $configurator) {
294+
$configurator->parameters()
295+
->set('mailer.transport', 'sendmail');
296+
297+
$container = $configurator->services();
298+
299+
$container->set('mailer', 'Mailer')
300+
->args(['%mailer.transport%']);
301+
302+
$container->set('newsletter_manager', 'NewsletterManager')
303+
->call('setMailer', [ref('mailer')]);
304+
};
292305
293-
// ...
294-
$container->setParameter('mailer.transport', 'sendmail');
295-
$container
296-
->register('mailer', 'Mailer')
297-
->addArgument('%mailer.transport%');
298-
299-
$container
300-
->register('newsletter_manager', 'NewsletterManager')
301-
->addMethodCall('setMailer', [new Reference('mailer')]);
302306
303307
Learn More
304308
----------

service_container.rst

Lines changed: 96 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -220,20 +220,19 @@ each time you ask for it.
220220
221221
.. code-block:: php
222222
223-
// app/config/services.php
224-
use Symfony\Component\DependencyInjection\Definition;
223+
// config/services.php
224+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
225225
226-
// To use as default template
227-
$definition = new Definition();
226+
return function(ContainerConfigurator $configurator) {
227+
$container = $configurator->services()
228+
->defaults()
229+
->autowire()
230+
->autoconfigure()
231+
->private();
228232
229-
$definition
230-
->setAutowired(true)
231-
->setAutoconfigured(true)
232-
->setPublic(false)
233-
;
234-
235-
// $this is a reference to the current loader
236-
$this->registerClasses($definition, 'AppBundle\\', '../../src/AppBundle/*', '../../src/AppBundle/{Entity,Repository}');
233+
$container->load('AppBundle\\', '../src/AppBundle/*')
234+
->exclude('../src/AppBundle/{Entity,Repository}');
235+
};
237236
238237
.. tip::
239238

@@ -491,24 +490,18 @@ pass here. No problem! In your configuration, you can explicitly set this argume
491490
492491
.. code-block:: php
493492
494-
// app/config/services.php
495-
use AppBundle\Updates\SiteUpdateManager;
496-
use Symfony\Component\DependencyInjection\Definition;
497-
498-
// Same as before
499-
$definition = new Definition();
500-
501-
$definition
502-
->setAutowired(true)
503-
->setAutoconfigured(true)
504-
->setPublic(false)
505-
;
493+
// config/services.php
494+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
506495
507-
$this->registerClasses($definition, 'AppBundle\\', '../../src/AppBundle/*', '../../src/AppBundle/{Entity,Repository}');
496+
return function(ContainerConfigurator $configurator) {
497+
$container = $configurator->services()
498+
->defaults()
499+
->autowire()
500+
->autoconfigure()
501+
->private();
508502
509-
// Explicitly configure the service
510-
$container->getDefinition(SiteUpdateManager::class)
511-
->setArgument('$adminEmail', 'manager@example.com');
503+
$container->load('AppBundle\\', '../src/AppBundle/*')
504+
->exclude('../src/{Entity,Repository}');
512505
513506
.. versionadded:: 3.3
514507

@@ -573,12 +566,19 @@ and reference it with the ``%parameter_name%`` syntax:
573566
.. code-block:: php
574567
575568
// app/config/services.php
569+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
570+
576571
use AppBundle\Updates\SiteUpdateManager;
577-
$container->setParameter('admin_email', 'manager@example.com');
578572
579-
$container->autowire(SiteUpdateManager::class)
580-
// ...
581-
->setArgument('$adminEmail', '%admin_email%');
573+
return function(ContainerConfigurator $configurator) {
574+
$configurator->parameters()->set('admin_email', 'manager@example.com');
575+
576+
$container = $configurator->services();
577+
$container->set(SiteUpdateManager::class)
578+
->autowire()
579+
->arg('$adminEmail', '%admin_email%');
580+
};
581+
582582
583583
Actually, once you define a parameter, it can be referenced via the ``%parameter_name%``
584584
syntax in *any* other service configuration file - like ``config.yml``. Many parameters
@@ -679,13 +679,18 @@ But, you can control this and pass in a different logger:
679679
.. code-block:: php
680680
681681
// app/config/services.php
682+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
683+
682684
use AppBundle\Service\MessageGenerator;
683-
use Symfony\Component\DependencyInjection\Reference;
684685
685-
$container->autowire(MessageGenerator::class)
686-
->setAutoconfigured(true)
687-
->setPublic(false)
688-
->setArgument('$logger', new Reference('monolog.logger.request'));
686+
return function(ContainerConfigurator $configurator) {
687+
$container = $configurator->services();
688+
$container->set(MessageGenerator::class)
689+
->autowire()
690+
->autoconfigure()
691+
->private();
692+
->arg('$logger', ref('monolog.logger.request'));
693+
};
689694
690695
This tells the container that the ``$logger`` argument to ``__construct`` should use
691696
service whose id is ``monolog.logger.request``.
@@ -753,19 +758,20 @@ You can also use the ``bind`` keyword to bind specific arguments by name or type
753758
754759
.. code-block:: php
755760
756-
// config/services.php
757-
use App\Controller\LuckyController;
761+
// app/config/services.php
762+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
763+
758764
use Psr\Log\LoggerInterface;
759-
use Symfony\Component\DependencyInjection\Reference;
760765
761-
$container->register(LuckyController::class)
762-
->setPublic(true)
763-
->setBindings([
764-
'$adminEmail' => 'manager@example.com',
765-
'$requestLogger' => new Reference('monolog.logger.request'),
766-
LoggerInterface::class => new Reference('monolog.logger.request'),
767-
])
768-
;
766+
return function(ContainerConfigurator $configurator) {
767+
$container = $configurator->services()
768+
->defaults()
769+
->bind('$adminEmail', 'manager@example.com')
770+
->bind('$requestLogger', ref('monolog.logger.request'))
771+
->bind(LoggerInterface::class, ref('monolog.logger.request'))
772+
;
773+
// ...
774+
};
769775
770776
By putting the ``bind`` key under ``_defaults``, you can specify the value of *any*
771777
argument for *any* service defined in this file! You can bind arguments by name
@@ -930,6 +936,20 @@ need to make your service public, just override this setting:
930936
</services>
931937
</container>
932938
939+
.. code-block:: php
940+
941+
// app/config/services.php
942+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
943+
944+
use App\Service\MessageGenerator;
945+
946+
return function(ContainerConfigurator $configurator) {
947+
// ... same as code before
948+
949+
$container->set(MessageGenerator::class)
950+
->public();
951+
};
952+
933953
.. _service-psr4-loader:
934954

935955
Importing Many Services at once with resource
@@ -983,27 +1003,13 @@ key. For example, the default Symfony configuration contains this:
9831003
.. code-block:: php
9841004
9851005
// app/config/services.php
986-
use Symfony\Component\DependencyInjection\Definition;
987-
988-
// To use as default template
989-
$definition = new Definition();
1006+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
9901007
991-
$definition
992-
->setAutowired(true)
993-
->setAutoconfigured(true)
994-
->setPublic(false)
995-
;
996-
997-
$this->registerClasses($definition, 'AppBundle\\', '../../src/AppBundle/*', '../../src/AppBundle/{Entity,Repository}');
998-
999-
// Changes default config
1000-
$definition
1001-
->setPublic(true)
1002-
->addTag('controller.service_arguments')
1003-
;
1004-
1005-
// $this is a reference to the current loader
1006-
$this->registerClasses($definition, 'AppBundle\\Controller\\', '../../src/AppBundle/Controller/*');
1008+
return function(ContainerConfigurator $configurator) {
1009+
// ...
1010+
$container->load('AppBundle\\', '../src/AppBundle/*')
1011+
->exclude('../src/AppBundle/{Entity,Repository}');
1012+
};
10071013
10081014
.. tip::
10091015

@@ -1144,27 +1150,30 @@ admin email. In this case, each needs to have a unique service id:
11441150
.. code-block:: php
11451151
11461152
// app/config/services.php
1153+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
1154+
11471155
use AppBundle\Service\MessageGenerator;
11481156
use AppBundle\Updates\SiteUpdateManager;
1149-
use Symfony\Component\DependencyInjection\Reference;
1150-
1151-
$container->register('site_update_manager.superadmin', SiteUpdateManager::class)
1152-
->setAutowired(false)
1153-
->setArguments([
1154-
new Reference(MessageGenerator::class),
1155-
new Reference('mailer'),
1156-
'superadmin@example.com'
1157-
]);
1158-
1159-
$container->register('site_update_manager.normal_users', SiteUpdateManager::class)
1160-
->setAutowired(false)
1161-
->setArguments([
1162-
new Reference(MessageGenerator::class),
1163-
new Reference('mailer'),
1164-
'contact@example.com'
1165-
]);
1166-
1167-
$container->setAlias(SiteUpdateManager::class, 'site_update_manager.superadmin')
1157+
1158+
return function(ContainerConfigurator $configurator) {
1159+
// ...
1160+
1161+
$container->set('site_update_manager.superadmin', SiteUpdateManager::class)
1162+
->autowire(false)
1163+
->args([
1164+
ref(MessageGenerator::class),
1165+
ref('mailer'),
1166+
'superadmin@example.com'
1167+
]);
1168+
$container->set('site_update_manager.normal_users', SiteUpdateManager::class)
1169+
->autowire(false)
1170+
->args([
1171+
ref(MessageGenerator::class),
1172+
ref('mailer'),
1173+
'contact@example.com'
1174+
]);
1175+
$container->alias(SiteUpdateManager::class, 'site_update_manager.superadmin');
1176+
};
11681177
11691178
In this case, *two* services are registered: ``site_update_manager.superadmin``
11701179
and ``site_update_manager.normal_users``. Thanks to the alias, if you type-hint

service_container/3.3-di-changes.rst

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,32 @@ Symfony Standard Edition:
8787
</services>
8888
</container>
8989
90+
.. code-block:: php
91+
92+
// app/config/services.php
93+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
94+
95+
return function(ContainerConfigurator $configurator) {
96+
$container = $configurator->services()
97+
->defaults()
98+
->autowire()
99+
->autoconfigure()
100+
->private();
101+
102+
$container->load('App\\', '../src/*')
103+
->exclude('../src/{Entity,Migrations,Tests}');
104+
105+
$container->load('App\\Controller\\', '../src/Controller')
106+
->tag('controller.service_arguments');
107+
};
108+
90109
This small bit of configuration contains a paradigm shift of how services
91110
are configured in Symfony.
92111

112+
.. versionadded:: 3.4
113+
114+
PHP Fluent DI was introduced in Symfony 3.4.
115+
93116
.. _`service-33-changes-automatic-registration`:
94117

95118
1) Services are Loaded Automatically
@@ -134,7 +157,20 @@ thanks to the following config:
134157
</services>
135158
</container>
136159
160+
.. code-block:: php
161+
162+
// app/config/services.php
163+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
164+
165+
return function(ContainerConfigurator $configurator) {
166+
// ...
167+
168+
$container->load('AppBundle\\', '../src/AppBundle/*')
169+
->exclude('../src/{Entity,Repository}');
170+
};
171+
137172
This means that every class in ``src/AppBundle/`` is *available* to be used as a
173+
138174
service. And thanks to the ``_defaults`` section at the top of the file, all of
139175
these services are **autowired** and **private** (i.e. ``public: false``).
140176

@@ -329,11 +365,14 @@ The third big change is that, in a new Symfony 3.3 project, your controllers are
329365
.. code-block:: php
330366
331367
// app/config/services.php
368+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
332369
333-
// ...
370+
return function(ContainerConfigurator $configurator) {
371+
// ...
334372
335-
$definition->addTag('controller.service_arguments');
336-
$this->registerClasses($definition, 'AppBundle\\Controller\\', '../../src/AppBundle/Controller/*');
373+
$container->load('AppBundle\\Controller\\', '../src/AppBundle/Controller/*')
374+
->tag('controller.service_arguments');
375+
};
337376
338377
But, you might not even notice this. First, your controllers *can* still extend
339378
the same base ``Controller`` class or a new :ref:`AbstractController <controller-abstract-versus-controller>`.
@@ -470,6 +509,21 @@ inherited from an abstract definition:
470509
</services>
471510
</container>
472511
512+
.. code-block:: php
513+
514+
// app/config/services.php
515+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
516+
517+
use App\Domain\LoaderInterface;
518+
519+
return function(ContainerConfigurator $configurator) {
520+
// ...
521+
522+
$container->instanceof(LoaderInterface::class
523+
->public()
524+
->tag('app.domain_loader');
525+
};
526+
473527
What about Performance
474528
----------------------
475529

0 commit comments

Comments
 (0)