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

Symfony event contracts #317

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Lexik\Bundle\FormFilterBundle\DependencyInjection;

use Lexik\Bundle\FormFilterBundle\Filter\FilterOperands;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

Expand Down
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
24 changes: 21 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\EventDispatcherInterface as ContractsEventDispatcherInterface;
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,21 @@ protected function buildDefaultConditionNode(Form $form, ConditionNodeInterface
}
}
}

/**
* @param string $eventName
* @param object $event
*
* @return mixed
*/
protected function dispatch($eventName, $event)
{
if ($this->dispatcher instanceof ContractsEventDispatcherInterface) {
// Event dispatcher 4.3+
return $this->dispatcher->dispatch($event, $eventName);
} else {
// Event dispatcher < 4.3
return $this->dispatcher->dispatch($eventName, $event);
}
}
}