Skip to content

Commit 7b1e127

Browse files
authored
Merge pull request #694 from XWB/s43-events
Fix Symfony 4.3 deprecation issues
2 parents 082f8f2 + 1ef6ce5 commit 7b1e127

File tree

7 files changed

+92
-15
lines changed

7 files changed

+92
-15
lines changed

Event/CommentEvent.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace FOS\CommentBundle\Event;
1313

1414
use FOS\CommentBundle\Model\CommentInterface;
15-
use Symfony\Component\EventDispatcher\Event;
1615

1716
/**
1817
* An event that occurs related to a comment.

Event/Event.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSCommentBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace FOS\CommentBundle\Event;
13+
14+
use Symfony\Component\EventDispatcher\Event as BaseEventDeprecated;
15+
use Symfony\Contracts\EventDispatcher\Event as BaseEvent;
16+
17+
// Symfony 4.3 BC layer
18+
if (class_exists(BaseEvent::class)) {
19+
class Event extends BaseEvent
20+
{
21+
}
22+
} else {
23+
class Event extends BaseEventDeprecated
24+
{
25+
}
26+
}

Event/ThreadEvent.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace FOS\CommentBundle\Event;
1313

1414
use FOS\CommentBundle\Model\ThreadInterface;
15-
use Symfony\Component\EventDispatcher\Event;
1615

1716
/**
1817
* An event that occurs related to a thread.

Event/VoteEvent.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace FOS\CommentBundle\Event;
1313

1414
use FOS\CommentBundle\Model\VoteInterface;
15-
use Symfony\Component\EventDispatcher\Event;
1615

1716
/**
1817
* An event that occurs related to a vote.

Model/CommentManager.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111

1212
namespace FOS\CommentBundle\Model;
1313

14+
use FOS\CommentBundle\Event\Event;
1415
use FOS\CommentBundle\Event\CommentEvent;
1516
use FOS\CommentBundle\Event\CommentPersistEvent;
1617
use FOS\CommentBundle\Events;
1718
use FOS\CommentBundle\Sorting\SortingFactory;
1819
use FOS\CommentBundle\Sorting\SortingInterface;
1920
use InvalidArgumentException;
2021
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
22+
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
2123

2224
/**
2325
* Abstract Comment Manager implementation which can be used as base class for your
@@ -45,7 +47,7 @@ abstract class CommentManager implements CommentManagerInterface
4547
*/
4648
public function __construct(EventDispatcherInterface $dispatcher, SortingFactory $factory)
4749
{
48-
$this->dispatcher = $dispatcher;
50+
$this->dispatcher = class_exists(LegacyEventDispatcherProxy::class) ? LegacyEventDispatcherProxy::decorate($dispatcher) : $dispatcher;
4951
$this->sortingFactory = $factory;
5052
}
5153

@@ -64,7 +66,7 @@ public function createComment(ThreadInterface $thread, CommentInterface $parent
6466
}
6567

6668
$event = new CommentEvent($comment);
67-
$this->dispatcher->dispatch(Events::COMMENT_CREATE, $event);
69+
$this->dispatch($event, Events::COMMENT_CREATE);
6870

6971
return $comment;
7072
}
@@ -90,7 +92,7 @@ public function saveComment(CommentInterface $comment)
9092
}
9193

9294
$event = new CommentPersistEvent($comment);
93-
$this->dispatcher->dispatch(Events::COMMENT_PRE_PERSIST, $event);
95+
$this->dispatch($event, Events::COMMENT_PRE_PERSIST);
9496

9597
if ($event->isPersistenceAborted()) {
9698
return false;
@@ -99,7 +101,7 @@ public function saveComment(CommentInterface $comment)
99101
$this->doSaveComment($comment);
100102

101103
$event = new CommentEvent($comment);
102-
$this->dispatcher->dispatch(Events::COMMENT_POST_PERSIST, $event);
104+
$this->dispatch($event, Events::COMMENT_POST_PERSIST);
103105

104106
return true;
105107
}
@@ -141,6 +143,22 @@ protected function organiseComments($comments, SortingInterface $sorter, $ignore
141143
return $tree;
142144
}
143145

146+
/**
147+
* @param Event $event
148+
* @param string $eventName
149+
*/
150+
protected function dispatch(Event $event, $eventName)
151+
{
152+
// LegacyEventDispatcherProxy exists in Symfony >= 4.3
153+
if (class_exists(LegacyEventDispatcherProxy::class)) {
154+
// New Symfony 4.3 EventDispatcher signature
155+
$this->dispatcher->dispatch($event, $eventName);
156+
} else {
157+
// Old EventDispatcher signature
158+
$this->dispatcher->dispatch($eventName, $event);
159+
}
160+
}
161+
144162
/**
145163
* Performs the persistence of a comment.
146164
*

Model/ThreadManager.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111

1212
namespace FOS\CommentBundle\Model;
1313

14+
use FOS\CommentBundle\Event\Event;
1415
use FOS\CommentBundle\Event\ThreadEvent;
1516
use FOS\CommentBundle\Events;
1617
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
18+
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
1719

1820
/**
1921
* Abstract Thread Manager implementation which can be used as base class for your
@@ -33,7 +35,7 @@ abstract class ThreadManager implements ThreadManagerInterface
3335
*/
3436
public function __construct(EventDispatcherInterface $dispatcher)
3537
{
36-
$this->dispatcher = $dispatcher;
38+
$this->dispatcher = class_exists(LegacyEventDispatcherProxy::class) ? LegacyEventDispatcherProxy::decorate($dispatcher) : $dispatcher;
3739
}
3840

3941
/**
@@ -63,7 +65,7 @@ public function createThread($id = null)
6365
}
6466

6567
$event = new ThreadEvent($thread);
66-
$this->dispatcher->dispatch(Events::THREAD_CREATE, $event);
68+
$this->dispatch($event, Events::THREAD_CREATE);
6769

6870
return $thread;
6971
}
@@ -76,12 +78,28 @@ public function createThread($id = null)
7678
public function saveThread(ThreadInterface $thread)
7779
{
7880
$event = new ThreadEvent($thread);
79-
$this->dispatcher->dispatch(Events::THREAD_PRE_PERSIST, $event);
81+
$this->dispatch($event, Events::THREAD_PRE_PERSIST);
8082

8183
$this->doSaveThread($thread);
8284

8385
$event = new ThreadEvent($thread);
84-
$this->dispatcher->dispatch(Events::THREAD_POST_PERSIST, $event);
86+
$this->dispatch($event, Events::THREAD_POST_PERSIST);
87+
}
88+
89+
/**
90+
* @param Event $event
91+
* @param string $eventName
92+
*/
93+
protected function dispatch(Event $event, $eventName)
94+
{
95+
// LegacyEventDispatcherProxy exists in Symfony >= 4.3
96+
if (class_exists(LegacyEventDispatcherProxy::class)) {
97+
// New Symfony 4.3 EventDispatcher signature
98+
$this->dispatcher->dispatch($event, $eventName);
99+
} else {
100+
// Old EventDispatcher signature
101+
$this->dispatcher->dispatch($eventName, $event);
102+
}
85103
}
86104

87105
/**

Model/VoteManager.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111

1212
namespace FOS\CommentBundle\Model;
1313

14+
use FOS\CommentBundle\Event\Event;
1415
use FOS\CommentBundle\Event\VoteEvent;
1516
use FOS\CommentBundle\Event\VotePersistEvent;
1617
use FOS\CommentBundle\Events;
1718
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
19+
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
1820

1921
/**
2022
* Abstract VotingManager.
@@ -35,7 +37,7 @@ abstract class VoteManager implements VoteManagerInterface
3537
*/
3638
public function __construct(EventDispatcherInterface $dispatcher)
3739
{
38-
$this->dispatcher = $dispatcher;
40+
$this->dispatcher = class_exists(LegacyEventDispatcherProxy::class) ? LegacyEventDispatcherProxy::decorate($dispatcher) : $dispatcher;
3941
}
4042

4143
/**
@@ -64,7 +66,7 @@ public function createVote(VotableCommentInterface $comment)
6466
$vote->setComment($comment);
6567

6668
$event = new VoteEvent($vote);
67-
$this->dispatcher->dispatch(Events::VOTE_CREATE, $event);
69+
$this->dispatch($event, Events::VOTE_CREATE);
6870

6971
return $vote;
7072
}
@@ -79,7 +81,7 @@ public function saveVote(VoteInterface $vote)
7981
}
8082

8183
$event = new VotePersistEvent($vote);
82-
$this->dispatcher->dispatch(Events::VOTE_PRE_PERSIST, $event);
84+
$this->dispatch($event, Events::VOTE_PRE_PERSIST);
8385

8486
if ($event->isPersistenceAborted()) {
8587
return;
@@ -88,7 +90,23 @@ public function saveVote(VoteInterface $vote)
8890
$this->doSaveVote($vote);
8991

9092
$event = new VoteEvent($vote);
91-
$this->dispatcher->dispatch(Events::VOTE_POST_PERSIST, $event);
93+
$this->dispatch($event, Events::VOTE_POST_PERSIST);
94+
}
95+
96+
/**
97+
* @param Event $event
98+
* @param string $eventName
99+
*/
100+
protected function dispatch(Event $event, $eventName)
101+
{
102+
// LegacyEventDispatcherProxy exists in Symfony >= 4.3
103+
if (class_exists(LegacyEventDispatcherProxy::class)) {
104+
// New Symfony 4.3 EventDispatcher signature
105+
$this->dispatcher->dispatch($event, $eventName);
106+
} else {
107+
// Old EventDispatcher signature
108+
$this->dispatcher->dispatch($eventName, $event);
109+
}
92110
}
93111

94112
/**

0 commit comments

Comments
 (0)