diff --git a/src/Knp/Component/Pager/Event/BeforeEvent.php b/src/Knp/Component/Pager/Event/BeforeEvent.php index 7365e604..6bbfaa0f 100644 --- a/src/Knp/Component/Pager/Event/BeforeEvent.php +++ b/src/Knp/Component/Pager/Event/BeforeEvent.php @@ -12,6 +12,11 @@ */ final class BeforeEvent extends Event { + /** + * @var array + */ + public array $options = []; + public function __construct( private readonly EventDispatcherInterface $eventDispatcher, private readonly ArgumentAccessInterface $argumentAccess, diff --git a/src/Knp/Component/Pager/Paginator.php b/src/Knp/Component/Pager/Paginator.php index bf1aa3b9..87ad542b 100644 --- a/src/Knp/Component/Pager/Paginator.php +++ b/src/Knp/Component/Pager/Paginator.php @@ -88,6 +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; $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']); + } }