Skip to content

Commit 94e59d0

Browse files
committed
Merge branch '4.4' into 5.1
* 4.4: Improve/fix container doc
2 parents 0c6a782 + a52a0b2 commit 94e59d0

File tree

2 files changed

+40
-32
lines changed

2 files changed

+40
-32
lines changed

service_container.rst

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,16 @@ service's class or interface name. Want to :doc:`log </logging>` something? No p
3232
namespace App\Controller;
3333

3434
use Psr\Log\LoggerInterface;
35+
use Symfony\Component\HttpFoundation\Response;
3536

3637
class ProductController
3738
{
3839
/**
3940
* @Route("/products")
4041
*/
41-
public function list(LoggerInterface $logger)
42+
public function list(LoggerInterface $logger): Response
4243
{
43-
$logger->info('Look! I just used a service');
44+
$logger->info('Look, I just used a service!');
4445

4546
// ...
4647
}
@@ -101,7 +102,7 @@ it can't be re-used. Instead, you decide to create a new class::
101102

102103
class MessageGenerator
103104
{
104-
public function getHappyMessage()
105+
public function getHappyMessage(): string
105106
{
106107
$messages = [
107108
'You did it! You updated the system! Amazing!',
@@ -118,9 +119,14 @@ it can't be re-used. Instead, you decide to create a new class::
118119
Congratulations! You've created your first service class! You can use it immediately
119120
inside your controller::
120121

122+
// src/Controller/ProductController.php
121123
use App\Service\MessageGenerator;
124+
use Symfony\Component\HttpFoundation\Response;
122125

123-
public function new(MessageGenerator $messageGenerator)
126+
/**
127+
* @Route("/products/new")
128+
*/
129+
public function new(MessageGenerator $messageGenerator): Response
124130
{
125131
// thanks to the type-hint, the container will instantiate a
126132
// new MessageGenerator and pass it to you!
@@ -241,7 +247,7 @@ and use it later::
241247
$this->logger = $logger;
242248
}
243249

244-
public function getHappyMessage()
250+
public function getHappyMessage(): string
245251
{
246252
$this->logger->info('About to find a happy message!');
247253
// ...
@@ -291,8 +297,8 @@ Handling Multiple Services
291297
Suppose you also want to email a site administrator each time a site update is
292298
made. To do that, you create a new class::
293299

294-
// src/Updates/SiteUpdateManager.php
295-
namespace App\Updates;
300+
// src/Service/SiteUpdateManager.php
301+
namespace App\Service;
296302

297303
use App\Service\MessageGenerator;
298304
use Symfony\Component\Mailer\MailerInterface;
@@ -309,7 +315,7 @@ made. To do that, you create a new class::
309315
$this->mailer = $mailer;
310316
}
311317

312-
public function notifyOfSiteUpdate()
318+
public function notifyOfSiteUpdate(): bool
313319
{
314320
$happyMessage = $this->messageGenerator->getHappyMessage();
315321

@@ -322,6 +328,8 @@ made. To do that, you create a new class::
322328
$this->mailer->send($email);
323329

324330
// ...
331+
332+
return true;
325333
}
326334
}
327335

@@ -334,7 +342,7 @@ you can type-hint the new ``SiteUpdateManager`` class and use it::
334342
namespace App\Controller;
335343
336344
// ...
337-
use App\Updates\SiteUpdateManager;
345+
use App\Service\SiteUpdateManager;
338346

339347
public function new(SiteUpdateManager $siteUpdateManager)
340348
{
@@ -361,29 +369,29 @@ example, suppose you want to make the admin email configurable:
361369

362370
.. code-block:: diff
363371
364-
// src/Updates/SiteUpdateManager.php
372+
// src/Service/SiteUpdateManager.php
365373
// ...
366374
367375
class SiteUpdateManager
368376
{
369377
// ...
370378
+ private $adminEmail;
371379
372-
- public function __construct(MessageGenerator $messageGenerator, \Swift_Mailer $mailer)
373-
+ public function __construct(MessageGenerator $messageGenerator, \Swift_Mailer $mailer, $adminEmail)
380+
- public function __construct(MessageGenerator $messageGenerator, MailerInterface $mailer)
381+
+ public function __construct(MessageGenerator $messageGenerator, MailerInterface $mailer, $adminEmail)
374382
{
375383
// ...
376384
+ $this->adminEmail = $adminEmail;
377385
}
378386
379-
public function notifyOfSiteUpdate()
387+
public function notifyOfSiteUpdate(): bool
380388
{
381389
// ...
382390
383-
$message = \Swift_Message::newInstance()
391+
$email = (new Email())
384392
// ...
385-
- ->setTo('manager@example.com')
386-
+ ->setTo($this->adminEmail)
393+
- ->to('manager@example.com')
394+
+ ->to($this->adminEmail)
387395
// ...
388396
;
389397
// ...
@@ -392,7 +400,7 @@ example, suppose you want to make the admin email configurable:
392400
393401
If you make this change and refresh, you'll see an error:
394402

395-
Cannot autowire service "App\Updates\SiteUpdateManager": argument "$adminEmail"
403+
Cannot autowire service "App\Service\SiteUpdateManager": argument "$adminEmail"
396404
of method "__construct()" must have a type-hint or be given a value explicitly.
397405

398406
That makes sense! There is no way that the container knows what value you want to
@@ -412,7 +420,7 @@ pass here. No problem! In your configuration, you can explicitly set this argume
412420
exclude: '../src/{DependencyInjection,Entity,Tests,Kernel.php}'
413421
414422
# explicitly configure the service
415-
App\Updates\SiteUpdateManager:
423+
App\Service\SiteUpdateManager:
416424
arguments:
417425
$adminEmail: 'manager@example.com'
418426
@@ -436,7 +444,7 @@ pass here. No problem! In your configuration, you can explicitly set this argume
436444
/>
437445
438446
<!-- Explicitly configure the service -->
439-
<service id="App\Updates\SiteUpdateManager">
447+
<service id="App\Service\SiteUpdateManager">
440448
<argument key="$adminEmail">manager@example.com</argument>
441449
</service>
442450
</services>
@@ -447,7 +455,7 @@ pass here. No problem! In your configuration, you can explicitly set this argume
447455
// config/services.php
448456
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
449457
450-
use App\Updates\SiteUpdateManager;
458+
use App\Service\SiteUpdateManager;
451459
452460
return function(ContainerConfigurator $configurator) {
453461
// ...
@@ -839,7 +847,7 @@ setting:
839847
# ... same code as before
840848
841849
# explicitly configure the service
842-
Acme\PublicService:
850+
App\Service\PublicService:
843851
public: true
844852
845853
.. code-block:: xml
@@ -855,7 +863,7 @@ setting:
855863
<!-- ... same code as before -->
856864
857865
<!-- Explicitly configure the service -->
858-
<service id="Acme\PublicService" public="true"></service>
866+
<service id="App\Service\PublicService" public="true"></service>
859867
</services>
860868
</container>
861869
@@ -864,13 +872,13 @@ setting:
864872
// config/services.php
865873
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
866874
867-
use Acme\PublicService;
875+
use App\Service\PublicService;
868876
869877
return function(ContainerConfigurator $configurator) {
870878
// ... same as code before
871879
872880
// explicitly configure the service
873-
$services->set(PublicService::class)
881+
$services->set(Service\PublicService::class)
874882
->public()
875883
;
876884
};
@@ -1049,7 +1057,7 @@ admin email. In this case, each needs to have a unique service id:
10491057
10501058
# this is the service's id
10511059
site_update_manager.superadmin:
1052-
class: App\Updates\SiteUpdateManager
1060+
class: App\Service\SiteUpdateManager
10531061
# you CAN still use autowiring: we just want to show what it looks like without
10541062
autowire: false
10551063
# manually wire all arguments
@@ -1059,7 +1067,7 @@ admin email. In this case, each needs to have a unique service id:
10591067
- 'superadmin@example.com'
10601068
10611069
site_update_manager.normal_users:
1062-
class: App\Updates\SiteUpdateManager
1070+
class: App\Service\SiteUpdateManager
10631071
autowire: false
10641072
arguments:
10651073
- '@App\Service\MessageGenerator'
@@ -1068,7 +1076,7 @@ admin email. In this case, each needs to have a unique service id:
10681076
10691077
# Create an alias, so that - by default - if you type-hint SiteUpdateManager,
10701078
# the site_update_manager.superadmin will be used
1071-
App\Updates\SiteUpdateManager: '@site_update_manager.superadmin'
1079+
App\Service\SiteUpdateManager: '@site_update_manager.superadmin'
10721080
10731081
.. code-block:: xml
10741082
@@ -1082,19 +1090,19 @@ admin email. In this case, each needs to have a unique service id:
10821090
<services>
10831091
<!-- ... -->
10841092
1085-
<service id="site_update_manager.superadmin" class="App\Updates\SiteUpdateManager" autowire="false">
1093+
<service id="site_update_manager.superadmin" class="App\Service\SiteUpdateManager" autowire="false">
10861094
<argument type="service" id="App\Service\MessageGenerator"/>
10871095
<argument type="service" id="mailer"/>
10881096
<argument>superadmin@example.com</argument>
10891097
</service>
10901098
1091-
<service id="site_update_manager.normal_users" class="App\Updates\SiteUpdateManager" autowire="false">
1099+
<service id="site_update_manager.normal_users" class="App\Service\SiteUpdateManager" autowire="false">
10921100
<argument type="service" id="App\Service\MessageGenerator"/>
10931101
<argument type="service" id="mailer"/>
10941102
<argument>contact@example.com</argument>
10951103
</service>
10961104
1097-
<service id="App\Updates\SiteUpdateManager" alias="site_update_manager.superadmin"/>
1105+
<service id="App\Service\SiteUpdateManager" alias="site_update_manager.superadmin"/>
10981106
</services>
10991107
</container>
11001108
@@ -1104,7 +1112,7 @@ admin email. In this case, each needs to have a unique service id:
11041112
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
11051113
11061114
use App\Service\MessageGenerator;
1107-
use App\Updates\SiteUpdateManager;
1115+
use App\Service\SiteUpdateManager;
11081116
11091117
return function(ContainerConfigurator $configurator) {
11101118
// ...

testing.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ it via that alias:
589589
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
590590
591591
use App\Service\MessageGenerator;
592-
use App\Updates\SiteUpdateManager;
592+
use App\Service\SiteUpdateManager;
593593
594594
return function(ContainerConfigurator $configurator) {
595595
// ...

0 commit comments

Comments
 (0)