Skip to content

Commit cc01ad8

Browse files
committed
[DependencyInjection] Fluent PHP DI Documentation
- Updated service_container.rst 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 5bb148e commit cc01ad8

File tree

4 files changed

+253
-11
lines changed

4 files changed

+253
-11
lines changed

_build/conf.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@
118118
'rst': 'reStructuredText',
119119
'terminal': 'Terminal',
120120
'varnish3': 'Varnish 3',
121-
'varnish4': 'Varnish 4'
121+
'varnish4': 'Varnish 4',
122+
'php-fluent-di': 'PHP (Fluent)'
122123
}
123124

124125
# use PHP as the primary domain

components/dependency_injection.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,25 @@ config files:
302302
->register('newsletter_manager', 'NewsletterManager')
303303
->addMethodCall('setMailer', [new Reference('mailer')]);
304304
305+
.. code-block:: php-fluent-di
306+
307+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
308+
use function Symfony\Component\DependencyInjection\Loader\Configurator\ref;
309+
310+
return function(ContainerConfigurator $configurator) {
311+
$configurator->parameters()
312+
->set('mailer.transport', 'sendmail');
313+
314+
$container = $configurator->services();
315+
316+
$container->set('mailer', 'Mailer')
317+
->args(['%mailer.transport%']);
318+
319+
$container->set('newsletter_manager', 'NewsletterManager')
320+
->call('setMailer', ref('mailer'));
321+
};
322+
323+
305324
Learn More
306325
----------
307326

service_container.rst

Lines changed: 151 additions & 9 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,7 +175,7 @@ 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
@@ -194,7 +194,23 @@ each time you ask for it.
194194
;
195195
196196
// $this is a reference to the current loader
197-
$this->registerClasses($definition, 'App\\', '../src/*', '../src/{Entity,Migrations,Tests}');
197+
$this->registerClasses($definition, 'App\\', '../src/*', '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
198+
199+
.. code-block:: php-fluent-di
200+
201+
// config/services.php
202+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
203+
204+
return function(ContainerConfigurator $configurator) {
205+
$container = $configurator->services()
206+
->defaults()
207+
->autowire()
208+
->autoconfigure()
209+
->private();
210+
211+
$container->load('App\\', '../src/*')
212+
->exclude('../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
213+
};
198214
199215
.. tip::
200216

@@ -396,7 +412,7 @@ pass here. No problem! In your configuration, you can explicitly set this argume
396412
# same as before
397413
App\:
398414
resource: '../src/*'
399-
exclude: '../src/{Entity,Migrations,Tests}'
415+
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
400416
401417
# explicitly configure the service
402418
App\Updates\SiteUpdateManager:
@@ -416,7 +432,7 @@ pass here. No problem! In your configuration, you can explicitly set this argume
416432
<!-- ... -->
417433
418434
<!-- Same as before -->
419-
<prototype namespace="App\" resource="../src/*" exclude="../src/{Entity,Migrations,Tests}" />
435+
<prototype namespace="App\" resource="../src/*" exclude="../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}" />
420436
421437
<!-- Explicitly configure the service -->
422438
<service id="App\Updates\SiteUpdateManager">
@@ -440,12 +456,32 @@ pass here. No problem! In your configuration, you can explicitly set this argume
440456
->setPublic(false)
441457
;
442458
443-
$this->registerClasses($definition, 'App\\', '../src/*', '../src/{Entity,Migrations,Tests}');
459+
$this->registerClasses($definition, 'App\\', '../src/*', '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
444460
445461
// Explicitly configure the service
446462
$container->getDefinition(SiteUpdateManager::class)
447463
->setArgument('$adminEmail', 'manager@example.com');
448464
465+
.. code-block:: php-fluent-di
466+
467+
// config/services.php
468+
use App\Updates\SiteUpdateManager;
469+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
470+
471+
return function(ContainerConfigurator $configurator) {
472+
$container = $configurator->services()
473+
->defaults()
474+
->autowire()
475+
->autoconfigure()
476+
->private();
477+
478+
$container->load('App\\', '../src/*')
479+
->exclude('../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
480+
481+
$container->set(SiteUpdateManager::class)->arg('$adminEmail', 'manager@example.com');
482+
};
483+
484+
449485
Thanks to this, the container will pass ``manager@example.com`` to the ``$adminEmail``
450486
argument of ``__construct`` when creating the ``SiteUpdateManager`` service. The
451487
other arguments will still be autowired.
@@ -510,6 +546,22 @@ and reference it with the ``%parameter_name%`` syntax:
510546
// ...
511547
->setArgument('$adminEmail', '%admin_email%');
512548
549+
.. code-block:: php-fluent-di
550+
551+
// config/services.php
552+
use App\Updates\SiteUpdateManager;
553+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
554+
555+
return function(ContainerConfigurator $configurator) {
556+
$configurator->parameters()->set('admin_email', 'manager@example.com');
557+
558+
$container = $configurator->services();
559+
$container->set(SiteUpdateManager::class)
560+
->autowire()
561+
->arg('$adminEmail', '%admin_email%');
562+
};
563+
564+
513565
Actually, once you define a parameter, it can be referenced via the
514566
``%parameter_name%`` syntax in *any* other configuration file. Many parameters
515567
are defined in the ``config/services.yaml`` file.
@@ -620,6 +672,22 @@ But, you can control this and pass in a different logger:
620672
->setPublic(false)
621673
->setArgument('$logger', new Reference('monolog.logger.request'));
622674
675+
.. code-block:: php-fluent-di
676+
677+
// config/services.php
678+
use App\Service\MessageGenerator;
679+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
680+
use function Symfony\Component\DependencyInjection\Loader\Configurator\ref;
681+
682+
return function(ContainerConfigurator $configurator) {
683+
$container = $configurator->services();
684+
$container->set(SiteUpdateManager::class)
685+
->autowire()
686+
->autoconfigure()
687+
->private();
688+
->arg('$logger', ref('monolog.logger.request'));
689+
};
690+
623691
This tells the container that the ``$logger`` argument to ``__construct`` should use
624692
service whose id is ``monolog.logger.request``.
625693

@@ -716,6 +784,25 @@ You can also use the ``bind`` keyword to bind specific arguments by name or type
716784
])
717785
;
718786
787+
.. code-block:: php-fluent-di
788+
789+
// config/services.php
790+
use App\Controller\LuckyController;
791+
use Psr\Log\LoggerInterface;
792+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
793+
use function Symfony\Component\DependencyInjection\Loader\Configurator\ref;
794+
795+
return function(ContainerConfigurator $configurator) {
796+
$container = $configurator->services()->defaults()
797+
->bind('$adminEmail', 'manager@example.com')
798+
->bind('$requestLogger', ref('monolog.logger.request'))
799+
->bind(LoggerInterface::class, ref('monolog.logger.request'))
800+
->bind('string $adminEmail', 'manager@example.com')
801+
->bind(LoggerInterface::class.' $requestLogger', ref('monolog.logger.request'));
802+
803+
// ...
804+
};
805+
719806
By putting the ``bind`` key under ``_defaults``, you can specify the value of *any*
720807
argument for *any* service defined in this file! You can bind arguments by name
721808
(e.g. ``$adminEmail``), by type (e.g. ``Psr\Log\LoggerInterface``) or both
@@ -849,6 +936,20 @@ But, if you *do* need to make a service public, override the ``public`` setting:
849936
</services>
850937
</container>
851938
939+
.. code-block:: php-fuent-di
940+
941+
// config/services.php
942+
943+
use App\Service\MessageGenerator;
944+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
945+
946+
return function(ContainerConfigurator $configurator) {
947+
// ... same as code before
948+
949+
$container->set(MessageGenerator::class)
950+
->public();
951+
};
952+
852953
.. _service-psr4-loader:
853954

854955
Importing Many Services at once with resource
@@ -869,7 +970,7 @@ key. For example, the default Symfony configuration contains this:
869970
# this creates a service per class whose id is the fully-qualified class name
870971
App\:
871972
resource: '../src/*'
872-
exclude: '../src/{Entity,Migrations,Tests}'
973+
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
873974
874975
.. code-block:: xml
875976
@@ -883,7 +984,7 @@ key. For example, the default Symfony configuration contains this:
883984
<services>
884985
<!-- ... -->
885986
886-
<prototype namespace="App\" resource="../src/*" exclude="../src/{Entity,Migrations,Tests}" />
987+
<prototype namespace="App\" resource="../src/*" exclude="../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}" />
887988
</services>
888989
</container>
889990
@@ -901,7 +1002,19 @@ key. For example, the default Symfony configuration contains this:
9011002
->setPublic(false)
9021003
;
9031004
904-
$this->registerClasses($definition, 'App\\', '../src/*', '../src/{Entity,Migrations,Tests}');
1005+
$this->registerClasses($definition, 'App\\', '../src/*', '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
1006+
1007+
.. code-block:: php-fluent-di
1008+
1009+
// config/services.php
1010+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
1011+
1012+
return function(ContainerConfigurator $configurator) {
1013+
// ...
1014+
1015+
$container->load('App\\', '../src/*')
1016+
->exclude('../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
1017+
};
9051018
9061019
.. tip::
9071020

@@ -1060,6 +1173,35 @@ admin email. In this case, each needs to have a unique service id:
10601173
10611174
$container->setAlias(SiteUpdateManager::class, 'site_update_manager.superadmin')
10621175
1176+
.. code-block:: php-fluent-di
1177+
1178+
// config/services.php
1179+
use App\Updates\SiteUpdateManager;
1180+
use App\Service\MessageGenerator;
1181+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
1182+
use function Symfony\Component\DependencyInjection\Loader\Configurator\ref;
1183+
1184+
return function(ContainerConfigurator $configurator) {
1185+
// ...
1186+
1187+
$container->set('site_update_manager.superadmin', SiteUpdateManager::class)
1188+
->autowire(false)
1189+
->args([
1190+
ref(MessageGenerator::class),
1191+
ref('mailer'),
1192+
'superadmin@example.com'
1193+
]);
1194+
$container->set('site_update_manager.normal_users', SiteUpdateManager::class)
1195+
->autowire(false)
1196+
->args([
1197+
ref(MessageGenerator::class),
1198+
ref('mailer'),
1199+
'contact@example.com'
1200+
]);
1201+
$container->alias(SiteUpdateManager::class, 'site_update_manager.superadmin');
1202+
};
1203+
1204+
10631205
In this case, *two* services are registered: ``site_update_manager.superadmin``
10641206
and ``site_update_manager.normal_users``. Thanks to the alias, if you type-hint
10651207
``SiteUpdateManager`` the first (``site_update_manager.superadmin``) will be passed.

0 commit comments

Comments
 (0)