From 226ea4bfea523076b26e9135dbe174cb692be856 Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Fri, 20 Sep 2024 10:37:34 +0200 Subject: [PATCH 1/3] Pass `$options` in `knp_pager.before` event --- src/Knp/Component/Pager/Event/BeforeEvent.php | 8 +++++++- src/Knp/Component/Pager/Paginator.php | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Knp/Component/Pager/Event/BeforeEvent.php b/src/Knp/Component/Pager/Event/BeforeEvent.php index 7365e604..bddd7b03 100644 --- a/src/Knp/Component/Pager/Event/BeforeEvent.php +++ b/src/Knp/Component/Pager/Event/BeforeEvent.php @@ -15,7 +15,8 @@ final class BeforeEvent extends Event public function __construct( private readonly EventDispatcherInterface $eventDispatcher, private readonly ArgumentAccessInterface $argumentAccess, - private readonly ?Connection $connection = null + private readonly ?Connection $connection = null, + private array &$options = [], ) { } @@ -29,6 +30,11 @@ public function getArgumentAccess(): ArgumentAccessInterface return $this->argumentAccess; } + public function &getOptions(): array + { + return $this->options; + } + public function getConnection(): ?Connection { return $this->connection; diff --git a/src/Knp/Component/Pager/Paginator.php b/src/Knp/Component/Pager/Paginator.php index bf1aa3b9..cc57a1eb 100644 --- a/src/Knp/Component/Pager/Paginator.php +++ b/src/Knp/Component/Pager/Paginator.php @@ -87,7 +87,7 @@ public function paginate($target, int $page = 1, ?int $limit = null, array $opti } // before pagination start - $beforeEvent = new Event\BeforeEvent($this->eventDispatcher, $this->argumentAccess, $this->connection); + $beforeEvent = new Event\BeforeEvent($this->eventDispatcher, $this->argumentAccess, $this->connection, $options); $this->eventDispatcher->dispatch($beforeEvent, 'knp_pager.before'); // items $itemsEvent = new Event\ItemsEvent($offset, $limit); From 7163ed1fdb749ea8bd0e44952f68de3078efa8b5 Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Fri, 20 Sep 2024 11:40:21 +0200 Subject: [PATCH 2/3] Make options a public property assigned by ref, to avoid more involved reference handling --- src/Knp/Component/Pager/Event/BeforeEvent.php | 13 ++++++------- src/Knp/Component/Pager/Paginator.php | 3 ++- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Knp/Component/Pager/Event/BeforeEvent.php b/src/Knp/Component/Pager/Event/BeforeEvent.php index bddd7b03..6bbfaa0f 100644 --- a/src/Knp/Component/Pager/Event/BeforeEvent.php +++ b/src/Knp/Component/Pager/Event/BeforeEvent.php @@ -12,11 +12,15 @@ */ final class BeforeEvent extends Event { + /** + * @var array + */ + public array $options = []; + public function __construct( private readonly EventDispatcherInterface $eventDispatcher, private readonly ArgumentAccessInterface $argumentAccess, - private readonly ?Connection $connection = null, - private array &$options = [], + private readonly ?Connection $connection = null ) { } @@ -30,11 +34,6 @@ public function getArgumentAccess(): ArgumentAccessInterface return $this->argumentAccess; } - public function &getOptions(): array - { - return $this->options; - } - public function getConnection(): ?Connection { return $this->connection; diff --git a/src/Knp/Component/Pager/Paginator.php b/src/Knp/Component/Pager/Paginator.php index cc57a1eb..c7dd3918 100644 --- a/src/Knp/Component/Pager/Paginator.php +++ b/src/Knp/Component/Pager/Paginator.php @@ -87,7 +87,8 @@ public function paginate($target, int $page = 1, ?int $limit = null, array $opti } // before pagination start - $beforeEvent = new Event\BeforeEvent($this->eventDispatcher, $this->argumentAccess, $this->connection, $options); + $beforeEvent = new Event\BeforeEvent($this->eventDispatcher, $this->argumentAccess, $this->connection); + $beforeEvent->options =& $options; $this->eventDispatcher->dispatch($beforeEvent, 'knp_pager.before'); // items $itemsEvent = new Event\ItemsEvent($offset, $limit); From ebbf4015bbbc0547d51ac622d69a6fed0f11708d Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Fri, 20 Sep 2024 11:59:53 +0200 Subject: [PATCH 3/3] Add test --- src/Knp/Component/Pager/Paginator.php | 2 +- tests/Test/Pager/PaginatorTest.php | 45 +++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/Knp/Component/Pager/Paginator.php b/src/Knp/Component/Pager/Paginator.php index c7dd3918..87ad542b 100644 --- a/src/Knp/Component/Pager/Paginator.php +++ b/src/Knp/Component/Pager/Paginator.php @@ -88,7 +88,7 @@ public function paginate($target, int $page = 1, ?int $limit = null, array $opti // before pagination start $beforeEvent = new Event\BeforeEvent($this->eventDispatcher, $this->argumentAccess, $this->connection); - $beforeEvent->options =& $options; + $beforeEvent->options = &$options; $this->eventDispatcher->dispatch($beforeEvent, 'knp_pager.before'); // items $itemsEvent = new Event\ItemsEvent($offset, $limit); diff --git a/tests/Test/Pager/PaginatorTest.php b/tests/Test/Pager/PaginatorTest.php index e41fcde7..850c8529 100644 --- a/tests/Test/Pager/PaginatorTest.php +++ b/tests/Test/Pager/PaginatorTest.php @@ -2,9 +2,13 @@ namespace Test\Pager; +use Knp\Component\Pager\Event\BeforeEvent; +use Knp\Component\Pager\Event\ItemsEvent; use Knp\Component\Pager\Event\Subscriber\Paginate\PaginationSubscriber; +use Knp\Component\Pager\Pagination\SlidingPagination; use PHPUnit\Framework\Attributes\Test; use Symfony\Component\EventDispatcher\EventDispatcher; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Test\Tool\BaseTestCase; final class PaginatorTest extends BaseTestCase @@ -29,4 +33,45 @@ public function shouldFailToPaginateUnsupportedValue(): void $paginator = $this->getPaginatorInstance(null, $dispatcher); $paginator->paginate(null, 1, 10); } + + #[Test] + public function shouldPassOptionsToBeforeEventSubscriber(): void + { + $dispatcher = new EventDispatcher(); + $dispatcher->addSubscriber(new class implements EventSubscriberInterface { + public static function getSubscribedEvents(): array + { + return [ + 'knp_pager.before' => ['before', 1], + ]; + } + public function before(BeforeEvent $event): void + { + BaseTestCase::assertArrayHasKey('some_option', $event->options); + BaseTestCase::assertEquals('value', $event->options['some_option']); + + $event->options['some_option'] = 'changed'; + $event->options['extra_option'] = 'added'; + } + }); + $dispatcher->addSubscriber(new class implements EventSubscriberInterface { + public static function getSubscribedEvents(): array + { + return [ + 'knp_pager.items' => ['items', 1], + ]; + } + public function items(ItemsEvent $event): void + { + BaseTestCase::assertArrayHasKey('some_option', $event->options); + BaseTestCase::assertEquals('changed', $event->options['some_option']); + BaseTestCase::assertArrayHasKey('extra_option', $event->options); + BaseTestCase::assertEquals('added', $event->options['extra_option']); + } + }); + $dispatcher->addSubscriber(new PaginationSubscriber()); + $paginator = $this->getPaginatorInstance(null, $dispatcher); + + $paginator->paginate([], 1, 10, ['some_option' => 'value']); + } }