Skip to content

Commit e26780b

Browse files
committed
feature #977 Feature: update events sf43 (ayshiff)
This PR was squashed before being merged into the master branch (closes #977). Discussion ---------- Feature: update events sf43 This PR updates the way events are used since Symfony 4.3 Beta 2 [#975](#975) - removed Event class - created a `CommentCreatedEvent` event object - update calls to dispatch() - used `Symfony\Contracts\EventDispatcher\Event` instead of the `Event` class I have a problem when I update the calls to dispatch() to remove the event name. The `comment.created` event is inside the `not called listeners` but it works fine when I keep the event name. I don't know if it is an issue or not. Commits ------- 6481025 Feature: update events sf43
2 parents c30d49a + 6481025 commit e26780b

File tree

5 files changed

+46
-42
lines changed

5 files changed

+46
-42
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,5 +88,6 @@
8888
"allow-contrib": true,
8989
"require": "4.3.*"
9090
}
91-
}
91+
},
92+
"minimum-stability": "beta"
9293
}

src/Controller/BlogController.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use App\Entity\Comment;
1515
use App\Entity\Post;
16-
use App\Events;
16+
use App\Events\CommentCreatedEvent;
1717
use App\Form\CommentType;
1818
use App\Repository\PostRepository;
1919
use App\Repository\TagRepository;
@@ -22,7 +22,6 @@
2222
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
2323
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
2424
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
25-
use Symfony\Component\EventDispatcher\GenericEvent;
2625
use Symfony\Component\HttpFoundation\Request;
2726
use Symfony\Component\HttpFoundation\Response;
2827
use Symfony\Component\Routing\Annotation\Route;
@@ -109,14 +108,17 @@ public function commentNew(Request $request, Post $post, EventDispatcherInterfac
109108
// to pass some PHP variables. For more complex applications, define your
110109
// own event object classes.
111110
// See https://symfony.com/doc/current/components/event_dispatcher/generic_event.html
112-
$event = new GenericEvent($comment);
111+
112+
// Since Symfony 4.3, it is better to use event object to make the calls to dispatch lighter
113+
// https://symfony.com/blog/new-in-symfony-4-3-simpler-event-dispatching
114+
$event = new CommentCreatedEvent($comment);
113115

114116
// When an event is dispatched, Symfony notifies it to all the listeners
115117
// and subscribers registered to it. Listeners can modify the information
116118
// passed in the event and they can even modify the execution flow, so
117119
// there's no guarantee that the rest of this controller will be executed.
118120
// See https://symfony.com/doc/current/components/event_dispatcher.html
119-
$eventDispatcher->dispatch($event, Events::COMMENT_CREATED);
121+
$eventDispatcher->dispatch($event);
120122

121123
return $this->redirectToRoute('blog_post', ['slug' => $post->getSlug()]);
122124
}

src/EventSubscriber/CommentNotificationSubscriber.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@
1212
namespace App\EventSubscriber;
1313

1414
use App\Entity\Comment;
15-
use App\Events;
15+
use App\Events\CommentCreatedEvent;
1616
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17-
use Symfony\Component\EventDispatcher\GenericEvent;
1817
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1918
use Symfony\Component\Translation\TranslatorInterface;
2019

@@ -41,14 +40,14 @@ public function __construct(\Swift_Mailer $mailer, UrlGeneratorInterface $urlGen
4140
public static function getSubscribedEvents(): array
4241
{
4342
return [
44-
Events::COMMENT_CREATED => 'onCommentCreated',
43+
CommentCreatedEvent::class => 'onCommentCreated',
4544
];
4645
}
4746

48-
public function onCommentCreated(GenericEvent $event): void
47+
public function onCommentCreated(CommentCreatedEvent $event): void
4948
{
5049
/** @var Comment $comment */
51-
$comment = $event->getSubject();
50+
$comment = $event->getComment();
5251
$post = $comment->getPost();
5352

5453
$linkToPost = $this->urlGenerator->generate('blog_post', [

src/Events.php

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/Events/CommentCreatedEvent.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace App\Events;
13+
14+
use App\Entity\Comment;
15+
use Symfony\Contracts\EventDispatcher\Event;
16+
17+
/**
18+
* The comment.created event is dispatched each time a comment is created
19+
* in the system.
20+
*/
21+
class CommentCreatedEvent extends Event
22+
{
23+
protected $comment;
24+
25+
public function __construct(Comment $comment)
26+
{
27+
$this->comment = $comment;
28+
}
29+
30+
public function getComment()
31+
{
32+
return $this->comment;
33+
}
34+
}

0 commit comments

Comments
 (0)