Skip to content

Commit 10f4778

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 02465f8 commit 10f4778

File tree

4 files changed

+253
-15
lines changed

4 files changed

+253
-15
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', array(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
@@ -154,7 +154,7 @@ each time you ask for it.
154154
# this creates a service per class whose id is the fully-qualified class name
155155
App\:
156156
resource: '../src/*'
157-
exclude: '../src/{Entity,Migrations,Tests,Kernel.php}'
157+
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
158158
159159
# ...
160160
@@ -171,7 +171,7 @@ each time you ask for it.
171171
<!-- Default configuration for services in *this* file -->
172172
<defaults autowire="true" autoconfigure="true" public="false" />
173173
174-
<prototype namespace="App\" resource="../src/*" exclude="../src/{Entity,Migrations,Tests}" />
174+
<prototype namespace="App\" resource="../src/*" exclude="../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}" />
175175
</services>
176176
</container>
177177
@@ -190,7 +190,23 @@ each time you ask for it.
190190
;
191191
192192
// $this is a reference to the current loader
193-
$this->registerClasses($definition, 'App\\', '../src/*', '../src/{Entity,Migrations,Tests}');
193+
$this->registerClasses($definition, 'App\\', '../src/*', '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
194+
195+
.. code-block:: php-fluent-di
196+
197+
// config/services.php
198+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
199+
200+
return function(ContainerConfigurator $configurator) {
201+
$container = $configurator->services()
202+
->defaults()
203+
->autowire()
204+
->autoconfigure()
205+
->private();
206+
207+
$container->load('App\\', '../src/*')
208+
->exclude('../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
209+
};
194210
195211
.. tip::
196212

@@ -393,7 +409,7 @@ pass here. No problem! In your configuration, you can explicitly set this argume
393409
# same as before
394410
App\:
395411
resource: '../src/*'
396-
exclude: '../src/{Entity,Migrations,Tests}'
412+
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
397413
398414
# explicitly configure the service
399415
App\Updates\SiteUpdateManager:
@@ -413,7 +429,7 @@ pass here. No problem! In your configuration, you can explicitly set this argume
413429
<!-- ... -->
414430
415431
<!-- Same as before -->
416-
<prototype namespace="App\" resource="../src/*" exclude="../src/{Entity,Migrations,Tests}" />
432+
<prototype namespace="App\" resource="../src/*" exclude="../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}" />
417433
418434
<!-- Explicitly configure the service -->
419435
<service id="App\Updates\SiteUpdateManager">
@@ -437,12 +453,32 @@ pass here. No problem! In your configuration, you can explicitly set this argume
437453
->setPublic(false)
438454
;
439455
440-
$this->registerClasses($definition, 'App\\', '../src/*', '../src/{Entity,Migrations,Tests}');
456+
$this->registerClasses($definition, 'App\\', '../src/*', '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
441457
442458
// Explicitly configure the service
443459
$container->getDefinition(SiteUpdateManager::class)
444460
->setArgument('$adminEmail', 'manager@example.com');
445461
462+
.. code-block:: php-fluent-di
463+
464+
// config/services.php
465+
use App\Updates\SiteUpdateManager;
466+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
467+
468+
return function(ContainerConfigurator $configurator) {
469+
$container = $configurator->services()
470+
->defaults()
471+
->autowire()
472+
->autoconfigure()
473+
->private();
474+
475+
$container->load('App\\', '../src/*')
476+
->exclude('../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
477+
478+
$container->set(SiteUpdateManager::class)->arg('$adminEmail', 'manager@example.com');
479+
};
480+
481+
446482
Thanks to this, the container will pass ``manager@example.com`` to the ``$adminEmail``
447483
argument of ``__construct`` when creating the ``SiteUpdateManager`` service. The
448484
other arguments will still be autowired.
@@ -507,6 +543,22 @@ and reference it with the ``%parameter_name%`` syntax:
507543
// ...
508544
->setArgument('$adminEmail', '%admin_email%');
509545
546+
.. code-block:: php-fluent-di
547+
548+
// config/services.php
549+
use App\Updates\SiteUpdateManager;
550+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
551+
552+
return function(ContainerConfigurator $configurator) {
553+
$configurator->parameters()->set('admin_email', 'manager@example.com');
554+
555+
$container = $configurator->services();
556+
$container->set(SiteUpdateManager::class)
557+
->autowire()
558+
->arg('$adminEmail', '%admin_email%');
559+
};
560+
561+
510562
Actually, once you define a parameter, it can be referenced via the
511563
``%parameter_name%`` syntax in *any* other configuration file. Many parameters
512564
are defined in the ``config/services.yaml`` file.
@@ -617,6 +669,22 @@ But, you can control this and pass in a different logger:
617669
->setPublic(false)
618670
->setArgument('$logger', new Reference('monolog.logger.request'));
619671
672+
.. code-block:: php-fluent-di
673+
674+
// config/services.php
675+
use App\Service\MessageGenerator;
676+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
677+
use function Symfony\Component\DependencyInjection\Loader\Configurator\ref;
678+
679+
return function(ContainerConfigurator $configurator) {
680+
$container = $configurator->services();
681+
$container->set(SiteUpdateManager::class)
682+
->autowire()
683+
->autoconfigure()
684+
->private();
685+
->arg('$logger', ref('monolog.logger.request'));
686+
};
687+
620688
This tells the container that the ``$logger`` argument to ``__construct`` should use
621689
service whose id is ``monolog.logger.request``.
622690

@@ -713,6 +781,25 @@ You can also use the ``bind`` keyword to bind specific arguments by name or type
713781
))
714782
;
715783
784+
.. code-block:: php-fluent-di
785+
786+
// config/services.php
787+
use App\Controller\LuckyController;
788+
use Psr\Log\LoggerInterface;
789+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
790+
use function Symfony\Component\DependencyInjection\Loader\Configurator\ref;
791+
792+
return function(ContainerConfigurator $configurator) {
793+
$container = $configurator->services()->defaults()
794+
->bind('$adminEmail', 'manager@example.com')
795+
->bind('$requestLogger', ref('monolog.logger.request'))
796+
->bind(LoggerInterface::class, ref('monolog.logger.request'))
797+
->bind('string $adminEmail', 'manager@example.com')
798+
->bind(LoggerInterface::class.' $requestLogger', ref('monolog.logger.request'));
799+
800+
// ...
801+
};
802+
716803
By putting the ``bind`` key under ``_defaults``, you can specify the value of *any*
717804
argument for *any* service defined in this file! You can bind arguments by name
718805
(e.g. ``$adminEmail``), by type (e.g. ``Psr\Log\LoggerInterface``) or both
@@ -852,6 +939,20 @@ But, if you *do* need to make a service public, override the ``public`` setting:
852939
</services>
853940
</container>
854941
942+
.. code-block:: php-fuent-di
943+
944+
// config/services.php
945+
946+
use App\Service\MessageGenerator;
947+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
948+
949+
return function(ContainerConfigurator $configurator) {
950+
// ... same as code before
951+
952+
$container->set(MessageGenerator::class)
953+
->public();
954+
};
955+
855956
.. _service-psr4-loader:
856957

857958
Importing Many Services at once with resource
@@ -872,7 +973,7 @@ key. For example, the default Symfony configuration contains this:
872973
# this creates a service per class whose id is the fully-qualified class name
873974
App\:
874975
resource: '../src/*'
875-
exclude: '../src/{Entity,Migrations,Tests}'
976+
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
876977
877978
.. code-block:: xml
878979
@@ -886,7 +987,7 @@ key. For example, the default Symfony configuration contains this:
886987
<services>
887988
<!-- ... -->
888989
889-
<prototype namespace="App\" resource="../src/*" exclude="../src/{Entity,Migrations,Tests}" />
990+
<prototype namespace="App\" resource="../src/*" exclude="../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}" />
890991
</services>
891992
</container>
892993
@@ -904,7 +1005,19 @@ key. For example, the default Symfony configuration contains this:
9041005
->setPublic(false)
9051006
;
9061007
907-
$this->registerClasses($definition, 'App\\', '../src/*', '../src/{Entity,Migrations,Tests}');
1008+
$this->registerClasses($definition, 'App\\', '../src/*', '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
1009+
1010+
.. code-block:: php-fluent-di
1011+
1012+
// config/services.php
1013+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
1014+
1015+
return function(ContainerConfigurator $configurator) {
1016+
// ...
1017+
1018+
$container->load('App\\', '../src/*')
1019+
->exclude('../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
1020+
};
9081021
9091022
.. tip::
9101023

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

0 commit comments

Comments
 (0)