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

Commit 35837c0

Browse files
committed
Update event & dispatching for symfony/contracts style events
1 parent 109df85 commit 35837c0

File tree

5 files changed

+35
-6
lines changed

5 files changed

+35
-6
lines changed

Event/ApplyFilterConditionEvent.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Lexik\Bundle\FormFilterBundle\Event;
44

55
use Lexik\Bundle\FormFilterBundle\Filter\Condition\ConditionBuilderInterface;
6-
use Symfony\Component\EventDispatcher\Event;
76

87
/**
98
* Event class to compute the WHERE clause from the conditions.

Event/Event.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Lexik\Bundle\FormFilterBundle\Event;
4+
5+
use Symfony\Component\EventDispatcher\Event as LegacyEvent;
6+
use Symfony\Contracts\EventDispatcher\Event as ContractsEvent;
7+
8+
if (\class_exists(ContractsEvent::class)) {
9+
abstract class Event extends ContractsEvent
10+
{
11+
}
12+
} else {
13+
abstract class Event extends LegacyEvent
14+
{
15+
}
16+
}

Event/GetFilterConditionEvent.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Lexik\Bundle\FormFilterBundle\Event;
44

5-
use Symfony\Component\EventDispatcher\Event;
65
use Lexik\Bundle\FormFilterBundle\Filter\Condition\Condition;
76
use Lexik\Bundle\FormFilterBundle\Filter\Condition\ConditionInterface;
87
use Lexik\Bundle\FormFilterBundle\Filter\Query\QueryInterface;

Event/PrepareEvent.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Lexik\Bundle\FormFilterBundle\Event;
44

5-
use Symfony\Component\EventDispatcher\Event;
65
use Lexik\Bundle\FormFilterBundle\Filter\Query\QueryInterface;
76

87
/**

Filter/FilterBuilderUpdater.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Symfony\Component\Form\Form;
77
use Symfony\Component\Form\FormInterface;
88
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
9+
use Symfony\Contracts\EventDispatcher\Event as ContractsEvent;
910
use Lexik\Bundle\FormFilterBundle\Filter\Condition\ConditionBuilder;
1011
use Lexik\Bundle\FormFilterBundle\Filter\Condition\ConditionBuilderInterface;
1112
use Lexik\Bundle\FormFilterBundle\Filter\Condition\ConditionInterface;
@@ -85,7 +86,7 @@ public function addFilterConditions(FormInterface $form, $queryBuilder, $alias =
8586
{
8687
// create the right QueryInterface object
8788
$event = new PrepareEvent($queryBuilder);
88-
$this->dispatcher->dispatch(FilterEvents::PREPARE, $event);
89+
$this->dispatch(FilterEvents::PREPARE, $event);
8990

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

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

108109
$this->conditionBuilder = null;
109110

@@ -207,7 +208,7 @@ protected function getFilterCondition(FormInterface $form, AbstractType $formTyp
207208
}
208209

209210
$event = new GetFilterConditionEvent($filterQuery, $field, $values);
210-
$this->dispatcher->dispatch($eventName, $event);
211+
$this->dispatch($eventName, $event);
211212

212213
$condition = $event->getCondition();
213214
}
@@ -288,4 +289,19 @@ protected function buildDefaultConditionNode(Form $form, ConditionNodeInterface
288289
}
289290
}
290291
}
292+
293+
/**
294+
* @param string|object $eventName
295+
* @param string|object $event
296+
*
297+
* @return mixed
298+
*/
299+
protected function dispatch($eventName, $event)
300+
{
301+
if ($event instanceof ContractsEvent) {
302+
return $this->dispatcher->dispatch($event, $eventName);
303+
} else {
304+
return $this->dispatcher->dispatch($eventName, $event);
305+
}
306+
}
291307
}

0 commit comments

Comments
 (0)