Skip to content

Commit 6547842

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 b7be4a5 commit 6547842

File tree

4 files changed

+249
-11
lines changed

4 files changed

+249
-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
@@ -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

@@ -389,7 +405,7 @@ pass here. No problem! In your configuration, you can explicitly set this argume
389405
# same as before
390406
App\:
391407
resource: '../src/*'
392-
exclude: '../src/{Entity,Migrations,Tests}'
408+
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
393409
394410
# explicitly configure the service
395411
App\Updates\SiteUpdateManager:
@@ -409,7 +425,7 @@ pass here. No problem! In your configuration, you can explicitly set this argume
409425
<!-- ... -->
410426
411427
<!-- Same as before -->
412-
<prototype namespace="App\" resource="../src/*" exclude="../src/{Entity,Migrations,Tests}" />
428+
<prototype namespace="App\" resource="../src/*" exclude="../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}" />
413429
414430
<!-- Explicitly configure the service -->
415431
<service id="App\Updates\SiteUpdateManager">
@@ -433,12 +449,32 @@ pass here. No problem! In your configuration, you can explicitly set this argume
433449
->setPublic(false)
434450
;
435451
436-
$this->registerClasses($definition, 'App\\', '../src/*', '../src/{Entity,Migrations,Tests}');
452+
$this->registerClasses($definition, 'App\\', '../src/*', '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
437453
438454
// Explicitly configure the service
439455
$container->getDefinition(SiteUpdateManager::class)
440456
->setArgument('$adminEmail', 'manager@example.com');
441457
458+
.. code-block:: php-fluent-di
459+
460+
// config/services.php
461+
use App\Updates\SiteUpdateManager;
462+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
463+
464+
return function(ContainerConfigurator $configurator) {
465+
$container = $configurator->services()
466+
->defaults()
467+
->autowire()
468+
->autoconfigure()
469+
->private();
470+
471+
$container->load('App\\', '../src/*')
472+
->exclude('../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
473+
474+
$container->set(SiteUpdateManager::class)->arg('$adminEmail', 'manager@example.com');
475+
};
476+
477+
442478
Thanks to this, the container will pass ``manager@example.com`` to the ``$adminEmail``
443479
argument of ``__construct`` when creating the ``SiteUpdateManager`` service. The
444480
other arguments will still be autowired.
@@ -503,6 +539,22 @@ and reference it with the ``%parameter_name%`` syntax:
503539
// ...
504540
->setArgument('$adminEmail', '%admin_email%');
505541
542+
.. code-block:: php-fluent-di
543+
544+
// config/services.php
545+
use App\Updates\SiteUpdateManager;
546+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
547+
548+
return function(ContainerConfigurator $configurator) {
549+
$configurator->parameters()->set('admin_email', 'manager@example.com');
550+
551+
$container = $configurator->services();
552+
$container->set(SiteUpdateManager::class)
553+
->autowire()
554+
->arg('$adminEmail', '%admin_email%');
555+
};
556+
557+
506558
Actually, once you define a parameter, it can be referenced via the
507559
``%parameter_name%`` syntax in *any* other configuration file. Many parameters
508560
are defined in the ``config/services.yaml`` file.
@@ -613,6 +665,22 @@ But, you can control this and pass in a different logger:
613665
->setPublic(false)
614666
->setArgument('$logger', new Reference('monolog.logger.request'));
615667
668+
.. code-block:: php-fluent-di
669+
670+
// config/services.php
671+
use App\Service\MessageGenerator;
672+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
673+
use function Symfony\Component\DependencyInjection\Loader\Configurator\ref;
674+
675+
return function(ContainerConfigurator $configurator) {
676+
$container = $configurator->services();
677+
$container->set(SiteUpdateManager::class)
678+
->autowire()
679+
->autoconfigure()
680+
->private();
681+
->arg('$logger', ref('monolog.logger.request'));
682+
};
683+
616684
This tells the container that the ``$logger`` argument to ``__construct`` should use
617685
service whose id is ``monolog.logger.request``.
618686

@@ -709,6 +777,25 @@ You can also use the ``bind`` keyword to bind specific arguments by name or type
709777
])
710778
;
711779
780+
.. code-block:: php-fluent-di
781+
782+
// config/services.php
783+
use App\Controller\LuckyController;
784+
use Psr\Log\LoggerInterface;
785+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
786+
use function Symfony\Component\DependencyInjection\Loader\Configurator\ref;
787+
788+
return function(ContainerConfigurator $configurator) {
789+
$container = $configurator->services()->defaults()
790+
->bind('$adminEmail', 'manager@example.com')
791+
->bind('$requestLogger', ref('monolog.logger.request'))
792+
->bind(LoggerInterface::class, ref('monolog.logger.request'))
793+
->bind('string $adminEmail', 'manager@example.com')
794+
->bind(LoggerInterface::class.' $requestLogger', ref('monolog.logger.request'));
795+
796+
// ...
797+
};
798+
712799
By putting the ``bind`` key under ``_defaults``, you can specify the value of *any*
713800
argument for *any* service defined in this file! You can bind arguments by name
714801
(e.g. ``$adminEmail``), by type (e.g. ``Psr\Log\LoggerInterface``) or both
@@ -842,6 +929,20 @@ But, if you *do* need to make a service public, override the ``public`` setting:
842929
</services>
843930
</container>
844931
932+
.. code-block:: php-fuent-di
933+
934+
// config/services.php
935+
936+
use App\Service\MessageGenerator;
937+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
938+
939+
return function(ContainerConfigurator $configurator) {
940+
// ... same as code before
941+
942+
$container->set(MessageGenerator::class)
943+
->public();
944+
};
945+
845946
.. _service-psr4-loader:
846947

847948
Importing Many Services at once with resource
@@ -862,7 +963,7 @@ key. For example, the default Symfony configuration contains this:
862963
# this creates a service per class whose id is the fully-qualified class name
863964
App\:
864965
resource: '../src/*'
865-
exclude: '../src/{Entity,Migrations,Tests}'
966+
exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'
866967
867968
.. code-block:: xml
868969
@@ -876,7 +977,7 @@ key. For example, the default Symfony configuration contains this:
876977
<services>
877978
<!-- ... -->
878979
879-
<prototype namespace="App\" resource="../src/*" exclude="../src/{Entity,Migrations,Tests}" />
980+
<prototype namespace="App\" resource="../src/*" exclude="../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}" />
880981
</services>
881982
</container>
882983
@@ -894,7 +995,19 @@ key. For example, the default Symfony configuration contains this:
894995
->setPublic(false)
895996
;
896997
897-
$this->registerClasses($definition, 'App\\', '../src/*', '../src/{Entity,Migrations,Tests}');
998+
$this->registerClasses($definition, 'App\\', '../src/*', '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
999+
1000+
.. code-block:: php-fluent-di
1001+
1002+
// config/services.php
1003+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
1004+
1005+
return function(ContainerConfigurator $configurator) {
1006+
// ...
1007+
1008+
$container->load('App\\', '../src/*')
1009+
->exclude('../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}');
1010+
};
8981011
8991012
.. tip::
9001013

@@ -1053,6 +1166,35 @@ admin email. In this case, each needs to have a unique service id:
10531166
10541167
$container->setAlias(SiteUpdateManager::class, 'site_update_manager.superadmin')
10551168
1169+
.. code-block:: php-fluent-di
1170+
1171+
// config/services.php
1172+
use App\Updates\SiteUpdateManager;
1173+
use App\Service\MessageGenerator;
1174+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
1175+
use function Symfony\Component\DependencyInjection\Loader\Configurator\ref;
1176+
1177+
return function(ContainerConfigurator $configurator) {
1178+
// ...
1179+
1180+
$container->set('site_update_manager.superadmin', SiteUpdateManager::class)
1181+
->autowire(false)
1182+
->args([
1183+
ref(MessageGenerator::class),
1184+
ref('mailer'),
1185+
'superadmin@example.com'
1186+
]);
1187+
$container->set('site_update_manager.normal_users', SiteUpdateManager::class)
1188+
->autowire(false)
1189+
->args([
1190+
ref(MessageGenerator::class),
1191+
ref('mailer'),
1192+
'contact@example.com'
1193+
]);
1194+
$container->alias(SiteUpdateManager::class, 'site_update_manager.superadmin');
1195+
};
1196+
1197+
10561198
In this case, *two* services are registered: ``site_update_manager.superadmin``
10571199
and ``site_update_manager.normal_users``. Thanks to the alias, if you type-hint
10581200
``SiteUpdateManager`` the first (``site_update_manager.superadmin``) will be passed.

0 commit comments

Comments
 (0)