Skip to content

Commit 3759fd0

Browse files
committed
Merge branch '4.4' into 5.1
* 4.4: Missing Namespace and comment from service container and testing context
2 parents 1a80dc5 + 725ba41 commit 3759fd0

File tree

8 files changed

+40
-0
lines changed

8 files changed

+40
-0
lines changed

service_container/3.3-di-changes.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,8 @@ But in Symfony 3.3, thanks to autowiring, all you need to do is type-hint the
264264
argument with ``InvoiceGenerator``::
265265

266266
// src/Service/InvoiceMailer.php
267+
namespace App\Service;
268+
267269
// ...
268270

269271
class InvoiceMailer
@@ -388,6 +390,9 @@ and autowiring like any other service.
388390
To make life even easier, it's now possible to autowire arguments to your controller
389391
action methods, like you can with the constructor of services. For example::
390392

393+
// src/Controller/InvoiceController.php
394+
namespace App\Controller;
395+
391396
use Psr\Log\LoggerInterface;
392397

393398
class InvoiceController extends AbstractController
@@ -417,6 +422,8 @@ this file. For example, suppose you want to create an event subscriber. First, y
417422
create the class::
418423

419424
// src/EventSubscriber/SetHeaderSusbcriber.php
425+
namespace App\EventSubscriber;
426+
420427
// ...
421428

422429
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

service_container/autowiring.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ the alphabet.
2424

2525
Start by creating a ROT13 transformer class::
2626

27+
// src/Util/Rot13Transformer.php
2728
namespace App\Util;
2829

2930
class Rot13Transformer
@@ -36,6 +37,7 @@ Start by creating a ROT13 transformer class::
3637

3738
And now a Twitter client using this transformer::
3839

40+
// src/Service/TwitterClient.php
3941
namespace App\Service;
4042

4143
use App\Util\Rot13Transformer;
@@ -122,6 +124,7 @@ both services:
122124
123125
Now, you can use the ``TwitterClient`` service immediately in a controller::
124126

127+
// src/Controller/DefaultController.php
125128
namespace App\Controller;
126129

127130
use App\Service\TwitterClient;
@@ -153,6 +156,9 @@ Autowiring Logic Explained
153156

154157
Autowiring works by reading the ``Rot13Transformer`` *type-hint* in ``TwitterClient``::
155158

159+
// src/Service/TwitterClient.php
160+
namespace App\Service;
161+
156162
// ...
157163
use App\Util\Rot13Transformer;
158164

@@ -272,6 +278,7 @@ of concrete classes as it replaces your dependencies with other objects.
272278

273279
To follow this best practice, suppose you decide to create a ``TransformerInterface``::
274280

281+
// src/Util/TransformerInterface.php
275282
namespace App\Util;
276283

277284
interface TransformerInterface
@@ -371,6 +378,7 @@ Dealing with Multiple Implementations of the Same Type
371378
Suppose you create a second class - ``UppercaseTransformer`` that implements
372379
``TransformerInterface``::
373380

381+
// src/Util/UppercaseTransformer.php
374382
namespace App\Util;
375383

376384
class UppercaseTransformer implements TransformerInterface
@@ -399,6 +407,7 @@ create a *named autowiring alias* from a special string containing the
399407
interface followed by a variable name matching the one you use when doing
400408
the injection::
401409

410+
// src/Service/MastodonClient.php
402411
namespace App\Service;
403412

404413
use App\Util\TransformerInterface;
@@ -537,6 +546,7 @@ When autowiring is enabled for a service, you can *also* configure the container
537546
to call methods on your class when it's instantiated. For example, suppose you want
538547
to inject the ``logger`` service, and decide to use setter-injection::
539548

549+
// src/Util/Rot13Transformer.php
540550
namespace App\Util;
541551

542552
class Rot13Transformer

service_container/calls.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Usually, you'll want to inject your dependencies via the constructor. But someti
1313
especially if a dependency is optional, you may want to use "setter injection". For
1414
example::
1515

16+
// src/Service/MessageGenerator.php
1617
namespace App\Service;
1718

1819
use Psr\Log\LoggerInterface;
@@ -80,6 +81,7 @@ To provide immutable services, some classes implement immutable setters.
8081
Such setters return a new instance of the configured class
8182
instead of mutating the object they were called on::
8283

84+
// src/Service/MessageGenerator.php
8385
namespace App\Service;
8486

8587
use Psr\Log\LoggerInterface;

service_container/factories.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ Static Factories
1919
Suppose you have a factory that configures and returns a new ``NewsletterManager``
2020
object by calling the static ``createNewsletterManager()`` method::
2121

22+
// src/Email\NewsletterManagerStaticFactory.php
23+
namespace App\Email;
24+
25+
// ...
26+
2227
class NewsletterManagerStaticFactory
2328
{
2429
public static function createNewsletterManager()
@@ -170,6 +175,10 @@ Invokable Factories
170175
Suppose you now change your factory method to ``__invoke()`` so that your
171176
factory service can be used as a callback::
172177

178+
// src/Email/InvokableNewsletterManagerFactory.php
179+
namespace App\Email;
180+
181+
// ...
173182
class InvokableNewsletterManagerFactory
174183
{
175184
public function __invoke()

service_container/injection_types.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ The most common way to inject dependencies is via a class's constructor.
1919
To do this you need to add an argument to the constructor signature to accept
2020
the dependency::
2121

22+
// src/Mail/NewsletterManager.php
2223
namespace App\Mail;
2324

2425
// ...
@@ -111,6 +112,9 @@ Immutable-setter Injection
111112
Another possible injection is to use a method which returns a separate instance
112113
by cloning the original service, this approach allows you to make a service immutable::
113114

115+
// src/Mail/NewsletterManager.php
116+
namespace App\Mail;
117+
114118
// ...
115119
use Symfony\Component\Mailer\MailerInterface;
116120

@@ -214,6 +218,9 @@ Setter Injection
214218
Another possible injection point into a class is by adding a setter method
215219
that accepts the dependency::
216220

221+
// src/Mail/NewsletterManager.php
222+
namespace App\Mail;
223+
217224
// ...
218225
class NewsletterManager
219226
{

service_container/request.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ add it as an argument to the methods that need the request or inject the
1111
:method:`Symfony\\Component\\HttpFoundation\\RequestStack::getCurrentRequest`
1212
method::
1313

14+
// src/Newsletter/NewsletterManager.php
1415
namespace App\Newsletter;
1516

1617
use Symfony\Component\HttpFoundation\RequestStack;

service_container/service_decoration.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ Three different behaviors are available:
381381
When using ``null``, you may have to update the decorator constructor in
382382
order to make decorated dependency nullable::
383383

384+
// src/Service/DecoratorService.php
384385
namespace App\Service;
385386

386387
use Acme\OptionalBundle\Service\OptionalService;

testing/profiling.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ The data collected by the Symfony Profiler can be used to check the number of
6969
database calls, the time spent in the framework, etc. All this information is
7070
provided by the collectors obtained through the ``$client->getProfile()`` call::
7171

72+
// tests/Controller/LuckyControllerTest.php
73+
namespace App\Tests\Controller;
74+
7275
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
7376

7477
class LuckyControllerTest extends WebTestCase

0 commit comments

Comments
 (0)