Skip to content

Commit ea5a2df

Browse files
committed
[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> Replaced php with php-fluent-di config Signed-off-by: RJ Garcia <rj@bighead.net> Wording adjustments from PR Review Signed-off-by: RJ Garcia <rj@bighead.net> removing extra comma Signed-off-by: RJ Garcia <rj@bighead.net> Updated to use namespace instead of use Signed-off-by: RJ Garcia <rj@bighead.net>
1 parent 0c1aa7d commit ea5a2df

20 files changed

+652
-347
lines changed

components/dependency_injection.rst

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

service_container.rst

Lines changed: 104 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ each time you ask for it.
158158
# this creates a service per class whose id is the fully-qualified class name
159159
App\:
160160
resource: '../src/*'
161-
exclude: '../src/{Entity,Migrations,Tests,Kernel.php}'
161+
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
162162
163163
# ...
164164
@@ -175,26 +175,25 @@ each time you ask for it.
175175
<!-- Default configuration for services in *this* file -->
176176
<defaults autowire="true" autoconfigure="true" public="false" />
177177
178-
<prototype namespace="App\" resource="../src/*" exclude="../src/{Entity,Migrations,Tests}" />
178+
<prototype namespace="App\" resource="../src/*" exclude="../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}" />
179179
</services>
180180
</container>
181181
182182
.. code-block:: php
183183
184184
// config/services.php
185-
use Symfony\Component\DependencyInjection\Definition;
185+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
186186
187-
// To use as default template
188-
$definition = new Definition();
187+
return function(ContainerConfigurator $configurator) {
188+
$container = $configurator->services()
189+
->defaults()
190+
->autowire()
191+
->autoconfigure()
192+
->private();
189193
190-
$definition
191-
->setAutowired(true)
192-
->setAutoconfigured(true)
193-
->setPublic(false)
194-
;
195-
196-
// $this is a reference to the current loader
197-
$this->registerClasses($definition, 'App\\', '../src/*', '../src/{Entity,Migrations,Tests}');
194+
$container->load('App\\', '../src/*')
195+
->exclude('../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
196+
};
198197
199198
.. tip::
200199

@@ -396,7 +395,7 @@ pass here. No problem! In your configuration, you can explicitly set this argume
396395
# same as before
397396
App\:
398397
resource: '../src/*'
399-
exclude: '../src/{Entity,Migrations,Tests}'
398+
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
400399
401400
# explicitly configure the service
402401
App\Updates\SiteUpdateManager:
@@ -416,7 +415,7 @@ pass here. No problem! In your configuration, you can explicitly set this argume
416415
<!-- ... -->
417416
418417
<!-- Same as before -->
419-
<prototype namespace="App\" resource="../src/*" exclude="../src/{Entity,Migrations,Tests}" />
418+
<prototype namespace="App\" resource="../src/*" exclude="../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}" />
420419
421420
<!-- Explicitly configure the service -->
422421
<service id="App\Updates\SiteUpdateManager">
@@ -428,23 +427,23 @@ pass here. No problem! In your configuration, you can explicitly set this argume
428427
.. code-block:: php
429428
430429
// config/services.php
430+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
431+
431432
use App\Updates\SiteUpdateManager;
432-
use Symfony\Component\DependencyInjection\Definition;
433433
434-
// Same as before
435-
$definition = new Definition();
434+
return function(ContainerConfigurator $configurator) {
435+
$container = $configurator->services()
436+
->defaults()
437+
->autowire()
438+
->autoconfigure()
439+
->private();
436440
437-
$definition
438-
->setAutowired(true)
439-
->setAutoconfigured(true)
440-
->setPublic(false)
441-
;
441+
$container->load('App\\', '../src/*')
442+
->exclude('../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
442443
443-
$this->registerClasses($definition, 'App\\', '../src/*', '../src/{Entity,Migrations,Tests}');
444+
$container->set(SiteUpdateManager::class)->arg('$adminEmail', 'manager@example.com');
445+
};
444446
445-
// Explicitly configure the service
446-
$container->getDefinition(SiteUpdateManager::class)
447-
->setArgument('$adminEmail', 'manager@example.com');
448447
449448
Thanks to this, the container will pass ``manager@example.com`` to the ``$adminEmail``
450449
argument of ``__construct`` when creating the ``SiteUpdateManager`` service. The
@@ -503,12 +502,19 @@ and reference it with the ``%parameter_name%`` syntax:
503502
.. code-block:: php
504503
505504
// config/services.php
505+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
506+
506507
use App\Updates\SiteUpdateManager;
507-
$container->setParameter('admin_email', 'manager@example.com');
508508
509-
$container->autowire(SiteUpdateManager::class)
510-
// ...
511-
->setArgument('$adminEmail', '%admin_email%');
509+
return function(ContainerConfigurator $configurator) {
510+
$configurator->parameters()->set('admin_email', 'manager@example.com');
511+
512+
$container = $configurator->services();
513+
$container->set(SiteUpdateManager::class)
514+
->autowire()
515+
->arg('$adminEmail', '%admin_email%');
516+
};
517+
512518
513519
Actually, once you define a parameter, it can be referenced via the
514520
``%parameter_name%`` syntax in *any* other configuration file. Many parameters
@@ -612,13 +618,18 @@ But, you can control this and pass in a different logger:
612618
.. code-block:: php
613619
614620
// config/services.php
621+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
622+
615623
use App\Service\MessageGenerator;
616-
use Symfony\Component\DependencyInjection\Reference;
617624
618-
$container->autowire(MessageGenerator::class)
619-
->setAutoconfigured(true)
620-
->setPublic(false)
621-
->setArgument('$logger', new Reference('monolog.logger.request'));
625+
return function(ContainerConfigurator $configurator) {
626+
$container = $configurator->services();
627+
$container->set(SiteUpdateManager::class)
628+
->autowire()
629+
->autoconfigure()
630+
->private();
631+
->arg('$logger', ref('monolog.logger.request'));
632+
};
622633
623634
This tells the container that the ``$logger`` argument to ``__construct`` should use
624635
service whose id is ``monolog.logger.request``.
@@ -700,21 +711,21 @@ You can also use the ``bind`` keyword to bind specific arguments by name or type
700711
.. code-block:: php
701712
702713
// config/services.php
714+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
715+
703716
use App\Controller\LuckyController;
704-
use Symfony\Component\DependencyInjection\Reference;
705717
use Psr\Log\LoggerInterface;
706718
707-
$container->register(LuckyController::class)
708-
->setPublic(true)
709-
->setBindings([
710-
'$adminEmail' => 'manager@example.com',
711-
'$requestLogger' => new Reference('monolog.logger.request'),
712-
LoggerInterface::class => new Reference('monolog.logger.request'),
713-
// optionally you can define both the name and type of the argument to match
714-
'string $adminEmail' => 'manager@example.com',
715-
LoggerInterface::class.' $requestLogger' => new Reference('monolog.logger.request'),
716-
])
717-
;
719+
return function(ContainerConfigurator $configurator) {
720+
$container = $configurator->services()->defaults()
721+
->bind('$adminEmail', 'manager@example.com')
722+
->bind('$requestLogger', ref('monolog.logger.request'))
723+
->bind(LoggerInterface::class, ref('monolog.logger.request'))
724+
->bind('string $adminEmail', 'manager@example.com')
725+
->bind(LoggerInterface::class.' $requestLogger', ref('monolog.logger.request'));
726+
727+
// ...
728+
};
718729
719730
By putting the ``bind`` key under ``_defaults``, you can specify the value of *any*
720731
argument for *any* service defined in this file! You can bind arguments by name
@@ -849,6 +860,20 @@ But, if you *do* need to make a service public, override the ``public`` setting:
849860
</services>
850861
</container>
851862
863+
.. code-block:: php
864+
865+
// config/services.php
866+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
867+
868+
use App\Service\MessageGenerator;
869+
870+
return function(ContainerConfigurator $configurator) {
871+
// ... same as code before
872+
873+
$container->set(MessageGenerator::class)
874+
->public();
875+
};
876+
852877
.. _service-psr4-loader:
853878

854879
Importing Many Services at once with resource
@@ -869,7 +894,7 @@ key. For example, the default Symfony configuration contains this:
869894
# this creates a service per class whose id is the fully-qualified class name
870895
App\:
871896
resource: '../src/*'
872-
exclude: '../src/{Entity,Migrations,Tests}'
897+
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
873898
874899
.. code-block:: xml
875900
@@ -883,25 +908,21 @@ key. For example, the default Symfony configuration contains this:
883908
<services>
884909
<!-- ... -->
885910
886-
<prototype namespace="App\" resource="../src/*" exclude="../src/{Entity,Migrations,Tests}" />
911+
<prototype namespace="App\" resource="../src/*" exclude="../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}" />
887912
</services>
888913
</container>
889914
890915
.. code-block:: php
891916
892917
// config/services.php
893-
use Symfony\Component\DependencyInjection\Definition;
894-
895-
// To use as default template
896-
$definition = new Definition();
918+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
897919
898-
$definition
899-
->setAutowired(true)
900-
->setAutoconfigured(true)
901-
->setPublic(false)
902-
;
920+
return function(ContainerConfigurator $configurator) {
921+
// ...
903922
904-
$this->registerClasses($definition, 'App\\', '../src/*', '../src/{Entity,Migrations,Tests}');
923+
$container->load('App\\', '../src/*')
924+
->exclude('../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
925+
};
905926
906927
.. tip::
907928

@@ -1038,27 +1059,31 @@ admin email. In this case, each needs to have a unique service id:
10381059
.. code-block:: php
10391060
10401061
// config/services.php
1062+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
1063+
10411064
use App\Updates\SiteUpdateManager;
10421065
use App\Service\MessageGenerator;
1043-
use Symfony\Component\DependencyInjection\Reference;
1044-
1045-
$container->register('site_update_manager.superadmin', SiteUpdateManager::class)
1046-
->setAutowired(false)
1047-
->setArguments([
1048-
new Reference(MessageGenerator::class),
1049-
new Reference('mailer'),
1050-
'superadmin@example.com'
1051-
]);
1052-
1053-
$container->register('site_update_manager.normal_users', SiteUpdateManager::class)
1054-
->setAutowired(false)
1055-
->setArguments([
1056-
new Reference(MessageGenerator::class),
1057-
new Reference('mailer'),
1058-
'contact@example.com'
1059-
]);
1060-
1061-
$container->setAlias(SiteUpdateManager::class, 'site_update_manager.superadmin')
1066+
1067+
return function(ContainerConfigurator $configurator) {
1068+
// ...
1069+
1070+
$container->set('site_update_manager.superadmin', SiteUpdateManager::class)
1071+
->autowire(false)
1072+
->args([
1073+
ref(MessageGenerator::class),
1074+
ref('mailer'),
1075+
'superadmin@example.com'
1076+
]);
1077+
$container->set('site_update_manager.normal_users', SiteUpdateManager::class)
1078+
->autowire(false)
1079+
->args([
1080+
ref(MessageGenerator::class),
1081+
ref('mailer'),
1082+
'contact@example.com'
1083+
]);
1084+
$container->alias(SiteUpdateManager::class, 'site_update_manager.superadmin');
1085+
};
1086+
10621087
10631088
In this case, *two* services are registered: ``site_update_manager.superadmin``
10641089
and ``site_update_manager.normal_users``. Thanks to the alias, if you type-hint

0 commit comments

Comments
 (0)