Skip to content

Commit 725ba41

Browse files
committed
minor #14346 Missing Namespace and comment from service container and testing context (TheGarious)
This PR was submitted for the master branch but it was merged into the 4.4 branch instead. Discussion ---------- Missing Namespace and comment from service container and testing context <!-- If your pull request fixes a BUG, use the oldest maintained branch that contains the bug (see https://symfony.com/releases for the list of maintained branches). If your pull request documents a NEW FEATURE, use the same Symfony branch where the feature was introduced (and `master` for features of unreleased versions). --> Commits ------- 533ea57 Missing Namespace and comment from service container and testing context
2 parents fa17c23 + 533ea57 commit 725ba41

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

@@ -277,6 +283,7 @@ of concrete classes as it replaces your dependencies with other objects.
277283

278284
To follow this best practice, suppose you decide to create a ``TransformerInterface``::
279285

286+
// src/Util/TransformerInterface.php
280287
namespace App\Util;
281288

282289
interface TransformerInterface
@@ -376,6 +383,7 @@ Dealing with Multiple Implementations of the Same Type
376383
Suppose you create a second class - ``UppercaseTransformer`` that implements
377384
``TransformerInterface``::
378385

386+
// src/Util/UppercaseTransformer.php
379387
namespace App\Util;
380388

381389
class UppercaseTransformer implements TransformerInterface
@@ -404,6 +412,7 @@ create a *named autowiring alias* from a special string containing the
404412
interface followed by a variable name matching the one you use when doing
405413
the injection::
406414

415+
// src/Service/MastodonClient.php
407416
namespace App\Service;
408417

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

558+
// src/Util/Rot13Transformer.php
549559
namespace App\Util;
550560

551561
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;
@@ -84,6 +85,7 @@ To provide immutable services, some classes implement immutable setters.
8485
Such setters return a new instance of the configured class
8586
instead of mutating the object they were called on::
8687

88+
// src/Service/MessageGenerator.php
8789
namespace App\Service;
8890

8991
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()
@@ -169,6 +174,10 @@ Invokable Factories
169174
Suppose you now change your factory method to ``__invoke()`` so that your
170175
factory service can be used as a callback::
171176

177+
// src/Email/InvokableNewsletterManagerFactory.php
178+
namespace App\Email;
179+
180+
// ...
172181
class InvokableNewsletterManagerFactory
173182
{
174183
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
// ...
@@ -115,6 +116,9 @@ Immutable-setter Injection
115116
Another possible injection is to use a method which returns a separate instance
116117
by cloning the original service, this approach allows you to make a service immutable::
117118

119+
// src/Mail/NewsletterManager.php
120+
namespace App\Mail;
121+
118122
// ...
119123
use Symfony\Component\Mailer\MailerInterface;
120124

@@ -218,6 +222,9 @@ Setter Injection
218222
Another possible injection point into a class is by adding a setter method
219223
that accepts the dependency::
220224

225+
// src/Mail/NewsletterManager.php
226+
namespace App\Mail;
227+
221228
// ...
222229
class NewsletterManager
223230
{

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
@@ -378,6 +378,7 @@ Three different behaviors are available:
378378
When using ``null``, you may have to update the decorator constructor in
379379
order to make decorated dependency nullable::
380380

381+
// src/Service/DecoratorService.php
381382
namespace App\Service;
382383

383384
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)