Skip to content

Commit a1ccc00

Browse files
authored
Pass $options in knp_pager.before event (#342)
1 parent 1dbd333 commit a1ccc00

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

Diff for: src/Knp/Component/Pager/Event/BeforeEvent.php

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
*/
1313
final class BeforeEvent extends Event
1414
{
15+
/**
16+
* @var array<string, mixed>
17+
*/
18+
public array $options = [];
19+
1520
public function __construct(
1621
private readonly EventDispatcherInterface $eventDispatcher,
1722
private readonly ArgumentAccessInterface $argumentAccess,

Diff for: src/Knp/Component/Pager/Paginator.php

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ public function paginate($target, int $page = 1, ?int $limit = null, array $opti
8888

8989
// before pagination start
9090
$beforeEvent = new Event\BeforeEvent($this->eventDispatcher, $this->argumentAccess, $this->connection);
91+
$beforeEvent->options = &$options;
9192
$this->eventDispatcher->dispatch($beforeEvent, 'knp_pager.before');
9293
// items
9394
$itemsEvent = new Event\ItemsEvent($offset, $limit);

Diff for: tests/Test/Pager/PaginatorTest.php

+45
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
namespace Test\Pager;
44

5+
use Knp\Component\Pager\Event\BeforeEvent;
6+
use Knp\Component\Pager\Event\ItemsEvent;
57
use Knp\Component\Pager\Event\Subscriber\Paginate\PaginationSubscriber;
8+
use Knp\Component\Pager\Pagination\SlidingPagination;
69
use PHPUnit\Framework\Attributes\Test;
710
use Symfony\Component\EventDispatcher\EventDispatcher;
11+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
812
use Test\Tool\BaseTestCase;
913

1014
final class PaginatorTest extends BaseTestCase
@@ -29,4 +33,45 @@ public function shouldFailToPaginateUnsupportedValue(): void
2933
$paginator = $this->getPaginatorInstance(null, $dispatcher);
3034
$paginator->paginate(null, 1, 10);
3135
}
36+
37+
#[Test]
38+
public function shouldPassOptionsToBeforeEventSubscriber(): void
39+
{
40+
$dispatcher = new EventDispatcher();
41+
$dispatcher->addSubscriber(new class implements EventSubscriberInterface {
42+
public static function getSubscribedEvents(): array
43+
{
44+
return [
45+
'knp_pager.before' => ['before', 1],
46+
];
47+
}
48+
public function before(BeforeEvent $event): void
49+
{
50+
BaseTestCase::assertArrayHasKey('some_option', $event->options);
51+
BaseTestCase::assertEquals('value', $event->options['some_option']);
52+
53+
$event->options['some_option'] = 'changed';
54+
$event->options['extra_option'] = 'added';
55+
}
56+
});
57+
$dispatcher->addSubscriber(new class implements EventSubscriberInterface {
58+
public static function getSubscribedEvents(): array
59+
{
60+
return [
61+
'knp_pager.items' => ['items', 1],
62+
];
63+
}
64+
public function items(ItemsEvent $event): void
65+
{
66+
BaseTestCase::assertArrayHasKey('some_option', $event->options);
67+
BaseTestCase::assertEquals('changed', $event->options['some_option']);
68+
BaseTestCase::assertArrayHasKey('extra_option', $event->options);
69+
BaseTestCase::assertEquals('added', $event->options['extra_option']);
70+
}
71+
});
72+
$dispatcher->addSubscriber(new PaginationSubscriber());
73+
$paginator = $this->getPaginatorInstance(null, $dispatcher);
74+
75+
$paginator->paginate([], 1, 10, ['some_option' => 'value']);
76+
}
3277
}

0 commit comments

Comments
 (0)