Skip to content

Commit 5c48d08

Browse files
committed
minor #13265 Update multiple services example (ker0x)
This PR was submitted for the 5.0 branch but it was merged into the 4.4 branch instead (closes #13265). Discussion ---------- Update multiple services example This PR propose to replace SwiftMailer with the Mailer component and to add the Logger service for a better understanding how to inject multiple services <!-- If your pull request fixes a BUG, use the oldest maintained branch that contains the bug (see https://symfony.com/roadmap 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 ------- e55dcad Update multiple services example
2 parents 8c8b4c1 + e55dcad commit 5c48d08

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

service_container.rst

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -295,13 +295,15 @@ made. To do that, you create a new class::
295295
namespace App\Updates;
296296

297297
use App\Service\MessageGenerator;
298+
use Symfony\Component\Mailer\MailerInterface;
299+
use Symfony\Component\Mime\Email;
298300

299301
class SiteUpdateManager
300302
{
301303
private $messageGenerator;
302304
private $mailer;
303305

304-
public function __construct(MessageGenerator $messageGenerator, \Swift_Mailer $mailer)
306+
public function __construct(MessageGenerator $messageGenerator, MailerInterface $mailer)
305307
{
306308
$this->messageGenerator = $messageGenerator;
307309
$this->mailer = $mailer;
@@ -311,19 +313,21 @@ made. To do that, you create a new class::
311313
{
312314
$happyMessage = $this->messageGenerator->getHappyMessage();
313315

314-
$message = (new \Swift_Message('Site update just happened!'))
315-
->setFrom('admin@example.com')
316-
->setTo('manager@example.com')
317-
->addPart(
318-
'Someone just updated the site. We told them: '.$happyMessage
319-
);
316+
$email = (new Email())
317+
->from('admin@example.com')
318+
->to('manager@example.com')
319+
->subject('Site update just happened!')
320+
->text('Someone just updated the site. We told them: '.$happyMessage);
320321

321-
return $this->mailer->send($message) > 0;
322+
$this->mailer->send($email);
323+
324+
// ...
322325
}
323326
}
324327

325-
This needs the ``MessageGenerator`` *and* the ``Swift_Mailer`` service. That's no
326-
problem! In fact, this new service is ready to be used. In a controller, for example,
328+
This needs the ``MessageGenerator`` *and* the ``Mailer`` service. That's no
329+
problem, we ask them by type hinting their class and interface names!
330+
Now, this new service is ready to be used. In a controller, for example,
327331
you can type-hint the new ``SiteUpdateManager`` class and use it::
328332

329333
// src/Controller/SiteController.php

0 commit comments

Comments
 (0)