Skip to content
This repository has been archived by the owner on Aug 1, 2024. It is now read-only.

Commit

Permalink
Update event & dispatching for symfony/contracts style events
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Nebes authored and Steven Nebes committed Sep 1, 2019
1 parent 109df85 commit 4965130
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
1 change: 0 additions & 1 deletion Event/ApplyFilterConditionEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Lexik\Bundle\FormFilterBundle\Event;

use Lexik\Bundle\FormFilterBundle\Filter\Condition\ConditionBuilderInterface;
use Symfony\Component\EventDispatcher\Event;

/**
* Event class to compute the WHERE clause from the conditions.
Expand Down
16 changes: 16 additions & 0 deletions Event/Event.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Lexik\Bundle\FormFilterBundle\Event;

use Symfony\Component\EventDispatcher\Event as LegacyEvent;
use Symfony\Contracts\EventDispatcher\Event as ContractsEvent;

if (\class_exists(ContractsEvent::class)) {
abstract class Event extends ContractsEvent
{
}
} else {
abstract class Event extends LegacyEvent
{
}
}
1 change: 0 additions & 1 deletion Event/GetFilterConditionEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Lexik\Bundle\FormFilterBundle\Event;

use Symfony\Component\EventDispatcher\Event;
use Lexik\Bundle\FormFilterBundle\Filter\Condition\Condition;
use Lexik\Bundle\FormFilterBundle\Filter\Condition\ConditionInterface;
use Lexik\Bundle\FormFilterBundle\Filter\Query\QueryInterface;
Expand Down
1 change: 0 additions & 1 deletion Event/PrepareEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Lexik\Bundle\FormFilterBundle\Event;

use Symfony\Component\EventDispatcher\Event;
use Lexik\Bundle\FormFilterBundle\Filter\Query\QueryInterface;

/**
Expand Down
22 changes: 19 additions & 3 deletions Filter/FilterBuilderUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\EventDispatcher\Event as ContractsEvent;
use Lexik\Bundle\FormFilterBundle\Filter\Condition\ConditionBuilder;
use Lexik\Bundle\FormFilterBundle\Filter\Condition\ConditionBuilderInterface;
use Lexik\Bundle\FormFilterBundle\Filter\Condition\ConditionInterface;
Expand Down Expand Up @@ -85,7 +86,7 @@ public function addFilterConditions(FormInterface $form, $queryBuilder, $alias =
{
// create the right QueryInterface object
$event = new PrepareEvent($queryBuilder);
$this->dispatcher->dispatch(FilterEvents::PREPARE, $event);
$this->dispatch(FilterEvents::PREPARE, $event);

if (!$event->getFilterQuery() instanceof QueryInterface) {
throw new \RuntimeException("Couldn't find any filter query object.");
Expand All @@ -103,7 +104,7 @@ public function addFilterConditions(FormInterface $form, $queryBuilder, $alias =

// walk condition nodes to add condition on the query builder instance
$name = sprintf('lexik_filter.apply_filters.%s', $event->getFilterQuery()->getEventPartName());
$this->dispatcher->dispatch($name, new ApplyFilterConditionEvent($queryBuilder, $this->conditionBuilder));
$this->dispatch($name, new ApplyFilterConditionEvent($queryBuilder, $this->conditionBuilder));

$this->conditionBuilder = null;

Expand Down Expand Up @@ -207,7 +208,7 @@ protected function getFilterCondition(FormInterface $form, AbstractType $formTyp
}

$event = new GetFilterConditionEvent($filterQuery, $field, $values);
$this->dispatcher->dispatch($eventName, $event);
$this->dispatch($eventName, $event);

$condition = $event->getCondition();
}
Expand Down Expand Up @@ -288,4 +289,19 @@ protected function buildDefaultConditionNode(Form $form, ConditionNodeInterface
}
}
}

/**
* @param string|object $eventName
* @param string|object $event
*
* @return mixed
*/
protected function dispatch($eventName, $event)
{
if ($event instanceof ContractsEvent) {
return $this->dispatcher->dispatch($event, $eventName);
} else {
return $this->dispatcher->dispatch($eventName, $event);
}
}
}

0 comments on commit 4965130

Please sign in to comment.