Skip to content
Permalink
Browse files

avoid deprecation notices from the event dispatcher in Symfony 4.3

  • Loading branch information
craue committed May 21, 2019
1 parent bb8f034 commit 333914ce643e639345b438e9386f51e489316848
Showing with 48 additions and 23 deletions.
  1. +26 −8 Event/FormFlowEvent.php
  2. +22 −15 Form/FormFlow.php
@@ -3,14 +3,32 @@
namespace Craue\FormFlowBundle\Event;

use Craue\FormFlowBundle\Form\FormFlowInterface;
use Symfony\Component\EventDispatcher\Event;

/**
* @author Christian Raue <christian.raue@gmail.com>
* @copyright 2011-2019 Christian Raue
* @license http://opensource.org/licenses/mit-license.php MIT License
*/
abstract class FormFlowEvent extends Event {
use Symfony\Component\EventDispatcher\Event as LegacyEvent;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Contracts\EventDispatcher\Event;

if (Kernel::VERSION_ID < 40300) {
// TODO remove as soon as Symfony >= 4.3 is required
/**
* @author Christian Raue <christian.raue@gmail.com>
* @copyright 2011-2019 Christian Raue
* @license http://opensource.org/licenses/mit-license.php MIT License
*/
abstract class FormFlowEvent extends LegacyEvent {
use _FormFlowEventTrait;
}
} else {
/**
* @author Christian Raue <christian.raue@gmail.com>
* @copyright 2011-2019 Christian Raue
* @license http://opensource.org/licenses/mit-license.php MIT License
*/
abstract class FormFlowEvent extends Event {
use _FormFlowEventTrait;
}
}

trait _FormFlowEventTrait {

/**
* @var FormFlowInterface
@@ -3,6 +3,7 @@
namespace Craue\FormFlowBundle\Form;

use Craue\FormFlowBundle\Event\FlowExpiredEvent;
use Craue\FormFlowBundle\Event\FormFlowEvent;
use Craue\FormFlowBundle\Event\GetStepsEvent;
use Craue\FormFlowBundle\Event\PostBindFlowEvent;
use Craue\FormFlowBundle\Event\PostBindRequestEvent;
@@ -19,6 +20,7 @@
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Validator\Constraints\GroupSequence;

/**
@@ -601,17 +603,15 @@ public function bind($formData) {
$this->setInstanceId($this->determineInstanceId());

if ($this->hasListeners(FormFlowEvents::PRE_BIND)) {
$event = new PreBindEvent($this);
$this->eventDispatcher->dispatch(FormFlowEvents::PRE_BIND, $event);
$this->dispatchEvent(new PreBindEvent($this), FormFlowEvents::PRE_BIND);
}

$this->formData = $formData;

$this->bindFlow();

if ($this->hasListeners(FormFlowEvents::POST_BIND_FLOW)) {
$event = new PostBindFlowEvent($this, $this->formData);
$this->eventDispatcher->dispatch(FormFlowEvents::POST_BIND_FLOW, $event);
$this->dispatchEvent(new PostBindFlowEvent($this, $this->formData), FormFlowEvents::POST_BIND_FLOW);
}

if ($this->newInstance) {
@@ -756,8 +756,7 @@ protected function applyDataFromSavedSteps() {
}

if ($this->hasListeners(FormFlowEvents::POST_BIND_SAVED_DATA)) {
$event = new PostBindSavedDataEvent($this, $this->formData, $stepNumber);
$this->eventDispatcher->dispatch(FormFlowEvents::POST_BIND_SAVED_DATA, $event);
$this->dispatchEvent(new PostBindSavedDataEvent($this, $this->formData, $stepNumber), FormFlowEvents::POST_BIND_SAVED_DATA);
}
}
}
@@ -770,8 +769,7 @@ public function createForm() {
$form = $this->createFormForStep($this->currentStepNumber);

if ($this->expired && $this->hasListeners(FormFlowEvents::FLOW_EXPIRED)) {
$event = new FlowExpiredEvent($this, $form);
$this->eventDispatcher->dispatch(FormFlowEvents::FLOW_EXPIRED, $event);
$this->dispatchEvent(new FlowExpiredEvent($this, $form), FormFlowEvents::FLOW_EXPIRED);
}

return $form;
@@ -837,7 +835,7 @@ public function getSteps() {

if ($this->hasListeners(FormFlowEvents::GET_STEPS)) {
$event = new GetStepsEvent($this);
$this->eventDispatcher->dispatch(FormFlowEvents::GET_STEPS, $event);
$this->dispatchEvent($event, FormFlowEvents::GET_STEPS);

// A listener has provided the steps for this flow.
if ($event->isPropagationStopped()) {
@@ -894,8 +892,7 @@ public function isValid(FormInterface $form) {
}

if ($this->hasListeners(FormFlowEvents::POST_BIND_REQUEST)) {
$event = new PostBindRequestEvent($this, $form->getData(), $this->currentStepNumber);
$this->eventDispatcher->dispatch(FormFlowEvents::POST_BIND_REQUEST, $event);
$this->dispatchEvent(new PostBindRequestEvent($this, $form->getData(), $this->currentStepNumber), FormFlowEvents::POST_BIND_REQUEST);
}

if ($this->revalidatePreviousSteps) {
@@ -913,8 +910,7 @@ public function isValid(FormInterface $form) {

if (!$stepForm->isValid()) {
if ($this->hasListeners(FormFlowEvents::PREVIOUS_STEP_INVALID)) {
$event = new PreviousStepInvalidEvent($this, $form, $stepNumber);
$this->eventDispatcher->dispatch(FormFlowEvents::PREVIOUS_STEP_INVALID, $event);
$this->dispatchEvent(new PreviousStepInvalidEvent($this, $form, $stepNumber), FormFlowEvents::PREVIOUS_STEP_INVALID);
}

return false;
@@ -924,8 +920,7 @@ public function isValid(FormInterface $form) {

if ($form->isValid()) {
if ($this->hasListeners(FormFlowEvents::POST_VALIDATE)) {
$event = new PostValidateEvent($this, $form->getData());
$this->eventDispatcher->dispatch(FormFlowEvents::POST_VALIDATE, $event);
$this->dispatchEvent(new PostValidateEvent($this, $form->getData()), FormFlowEvents::POST_VALIDATE);
}

return true;
@@ -1013,6 +1008,18 @@ protected function hasListeners($eventName) {
return $this->eventDispatcher !== null && $this->eventDispatcher->hasListeners($eventName);
}

/**
* @param FormFlowEvent $event
* @param string $eventName
*/
private function dispatchEvent($event, $eventName) {
if (Kernel::VERSION_ID < 40300) {
$this->eventDispatcher->dispatch($eventName, $event);
} else {
$this->eventDispatcher->dispatch($event, $eventName);
}
}

/**
* {@inheritDoc}
*/

0 comments on commit 333914c

Please sign in to comment.
You can’t perform that action at this time.