Skip to content

Commit

Permalink
Merge pull request #694 from XWB/s43-events
Browse files Browse the repository at this point in the history
Fix Symfony 4.3 deprecation issues
  • Loading branch information
XWB authored Nov 6, 2019
2 parents 082f8f2 + 1ef6ce5 commit 7b1e127
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 15 deletions.
1 change: 0 additions & 1 deletion Event/CommentEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace FOS\CommentBundle\Event;

use FOS\CommentBundle\Model\CommentInterface;
use Symfony\Component\EventDispatcher\Event;

/**
* An event that occurs related to a comment.
Expand Down
26 changes: 26 additions & 0 deletions Event/Event.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of the FOSCommentBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace FOS\CommentBundle\Event;

use Symfony\Component\EventDispatcher\Event as BaseEventDeprecated;
use Symfony\Contracts\EventDispatcher\Event as BaseEvent;

// Symfony 4.3 BC layer
if (class_exists(BaseEvent::class)) {
class Event extends BaseEvent
{
}
} else {
class Event extends BaseEventDeprecated
{
}
}
1 change: 0 additions & 1 deletion Event/ThreadEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace FOS\CommentBundle\Event;

use FOS\CommentBundle\Model\ThreadInterface;
use Symfony\Component\EventDispatcher\Event;

/**
* An event that occurs related to a thread.
Expand Down
1 change: 0 additions & 1 deletion Event/VoteEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace FOS\CommentBundle\Event;

use FOS\CommentBundle\Model\VoteInterface;
use Symfony\Component\EventDispatcher\Event;

/**
* An event that occurs related to a vote.
Expand Down
26 changes: 22 additions & 4 deletions Model/CommentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@

namespace FOS\CommentBundle\Model;

use FOS\CommentBundle\Event\Event;
use FOS\CommentBundle\Event\CommentEvent;
use FOS\CommentBundle\Event\CommentPersistEvent;
use FOS\CommentBundle\Events;
use FOS\CommentBundle\Sorting\SortingFactory;
use FOS\CommentBundle\Sorting\SortingInterface;
use InvalidArgumentException;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;

/**
* Abstract Comment Manager implementation which can be used as base class for your
Expand Down Expand Up @@ -45,7 +47,7 @@ abstract class CommentManager implements CommentManagerInterface
*/
public function __construct(EventDispatcherInterface $dispatcher, SortingFactory $factory)
{
$this->dispatcher = $dispatcher;
$this->dispatcher = class_exists(LegacyEventDispatcherProxy::class) ? LegacyEventDispatcherProxy::decorate($dispatcher) : $dispatcher;
$this->sortingFactory = $factory;
}

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

$event = new CommentEvent($comment);
$this->dispatcher->dispatch(Events::COMMENT_CREATE, $event);
$this->dispatch($event, Events::COMMENT_CREATE);

return $comment;
}
Expand All @@ -90,7 +92,7 @@ public function saveComment(CommentInterface $comment)
}

$event = new CommentPersistEvent($comment);
$this->dispatcher->dispatch(Events::COMMENT_PRE_PERSIST, $event);
$this->dispatch($event, Events::COMMENT_PRE_PERSIST);

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

$event = new CommentEvent($comment);
$this->dispatcher->dispatch(Events::COMMENT_POST_PERSIST, $event);
$this->dispatch($event, Events::COMMENT_POST_PERSIST);

return true;
}
Expand Down Expand Up @@ -141,6 +143,22 @@ protected function organiseComments($comments, SortingInterface $sorter, $ignore
return $tree;
}

/**
* @param Event $event
* @param string $eventName
*/
protected function dispatch(Event $event, $eventName)
{
// LegacyEventDispatcherProxy exists in Symfony >= 4.3
if (class_exists(LegacyEventDispatcherProxy::class)) {
// New Symfony 4.3 EventDispatcher signature
$this->dispatcher->dispatch($event, $eventName);
} else {
// Old EventDispatcher signature
$this->dispatcher->dispatch($eventName, $event);
}
}

/**
* Performs the persistence of a comment.
*
Expand Down
26 changes: 22 additions & 4 deletions Model/ThreadManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@

namespace FOS\CommentBundle\Model;

use FOS\CommentBundle\Event\Event;
use FOS\CommentBundle\Event\ThreadEvent;
use FOS\CommentBundle\Events;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;

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

/**
Expand Down Expand Up @@ -63,7 +65,7 @@ public function createThread($id = null)
}

$event = new ThreadEvent($thread);
$this->dispatcher->dispatch(Events::THREAD_CREATE, $event);
$this->dispatch($event, Events::THREAD_CREATE);

return $thread;
}
Expand All @@ -76,12 +78,28 @@ public function createThread($id = null)
public function saveThread(ThreadInterface $thread)
{
$event = new ThreadEvent($thread);
$this->dispatcher->dispatch(Events::THREAD_PRE_PERSIST, $event);
$this->dispatch($event, Events::THREAD_PRE_PERSIST);

$this->doSaveThread($thread);

$event = new ThreadEvent($thread);
$this->dispatcher->dispatch(Events::THREAD_POST_PERSIST, $event);
$this->dispatch($event, Events::THREAD_POST_PERSIST);
}

/**
* @param Event $event
* @param string $eventName
*/
protected function dispatch(Event $event, $eventName)
{
// LegacyEventDispatcherProxy exists in Symfony >= 4.3
if (class_exists(LegacyEventDispatcherProxy::class)) {
// New Symfony 4.3 EventDispatcher signature
$this->dispatcher->dispatch($event, $eventName);
} else {
// Old EventDispatcher signature
$this->dispatcher->dispatch($eventName, $event);
}
}

/**
Expand Down
26 changes: 22 additions & 4 deletions Model/VoteManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@

namespace FOS\CommentBundle\Model;

use FOS\CommentBundle\Event\Event;
use FOS\CommentBundle\Event\VoteEvent;
use FOS\CommentBundle\Event\VotePersistEvent;
use FOS\CommentBundle\Events;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;

/**
* Abstract VotingManager.
Expand All @@ -35,7 +37,7 @@ abstract class VoteManager implements VoteManagerInterface
*/
public function __construct(EventDispatcherInterface $dispatcher)
{
$this->dispatcher = $dispatcher;
$this->dispatcher = class_exists(LegacyEventDispatcherProxy::class) ? LegacyEventDispatcherProxy::decorate($dispatcher) : $dispatcher;
}

/**
Expand Down Expand Up @@ -64,7 +66,7 @@ public function createVote(VotableCommentInterface $comment)
$vote->setComment($comment);

$event = new VoteEvent($vote);
$this->dispatcher->dispatch(Events::VOTE_CREATE, $event);
$this->dispatch($event, Events::VOTE_CREATE);

return $vote;
}
Expand All @@ -79,7 +81,7 @@ public function saveVote(VoteInterface $vote)
}

$event = new VotePersistEvent($vote);
$this->dispatcher->dispatch(Events::VOTE_PRE_PERSIST, $event);
$this->dispatch($event, Events::VOTE_PRE_PERSIST);

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

$event = new VoteEvent($vote);
$this->dispatcher->dispatch(Events::VOTE_POST_PERSIST, $event);
$this->dispatch($event, Events::VOTE_POST_PERSIST);
}

/**
* @param Event $event
* @param string $eventName
*/
protected function dispatch(Event $event, $eventName)
{
// LegacyEventDispatcherProxy exists in Symfony >= 4.3
if (class_exists(LegacyEventDispatcherProxy::class)) {
// New Symfony 4.3 EventDispatcher signature
$this->dispatcher->dispatch($event, $eventName);
} else {
// Old EventDispatcher signature
$this->dispatcher->dispatch($eventName, $event);
}
}

/**
Expand Down

0 comments on commit 7b1e127

Please sign in to comment.