1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /* |
||
6 | * (c) Christian Gripp <[email protected]> |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | namespace Core23\Twig\Extension; |
||
13 | |||
14 | use Sonata\DatagridBundle\Pager\BasePager; |
||
15 | use Symfony\Component\Routing\RouterInterface; |
||
16 | use Twig\Environment; |
||
17 | use Twig\Error\LoaderError; |
||
18 | use Twig\Error\RuntimeError; |
||
19 | use Twig\Error\SyntaxError; |
||
20 | use Twig\Extension\AbstractExtension; |
||
21 | use Twig\Extension\InitRuntimeInterface; |
||
22 | use Twig\TwigFilter; |
||
23 | use Twig\TwigFunction; |
||
24 | |||
25 | final class RouterTwigExtension extends AbstractExtension implements InitRuntimeInterface |
||
0 ignored issues
–
show
|
|||
26 | { |
||
27 | /** |
||
28 | * @var RouterInterface |
||
29 | */ |
||
30 | private $router; |
||
31 | |||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | private $options; |
||
36 | |||
37 | /** |
||
38 | * @var Environment |
||
39 | */ |
||
40 | private $environment; |
||
41 | |||
42 | /** |
||
43 | * @throws LoaderError |
||
44 | */ |
||
45 | public function __construct(RouterInterface $router, array $options = []) |
||
46 | { |
||
47 | $this->router = $router; |
||
48 | $this->options = $options; |
||
49 | |||
50 | if (!isset($this->options['template'])) { |
||
51 | throw new LoaderError('Pager template is not set.'); |
||
52 | } |
||
53 | if (!isset($this->options['extremeLimit'])) { |
||
54 | throw new LoaderError('Pager extreme limit is not set.'); |
||
55 | } |
||
56 | if (!isset($this->options['nearbyLimit'])) { |
||
57 | throw new LoaderError('Pager nearby limit is not set.'); |
||
58 | } |
||
59 | } |
||
60 | |||
61 | public function initRuntime(Environment $environment): void |
||
62 | { |
||
63 | $this->environment = $environment; |
||
64 | } |
||
65 | |||
66 | public function getFunctions() |
||
67 | { |
||
68 | return [ |
||
69 | new TwigFunction('routeExists', [$this, 'routeExists']), |
||
70 | new TwigFunction('page_pager', [$this, 'generatePager'], [ |
||
71 | 'is_safe' => ['html'], |
||
72 | ]), |
||
73 | ]; |
||
74 | } |
||
75 | |||
76 | public function getFilters() |
||
77 | { |
||
78 | return [ |
||
79 | new TwigFilter('splitTag', [$this, 'splitTag']), |
||
80 | ]; |
||
81 | } |
||
82 | |||
83 | public function routeExists(string $name): bool |
||
84 | { |
||
85 | return null !== $this->router->getRouteCollection()->get($name); |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @return string[] |
||
90 | */ |
||
91 | public function splitTag(string $text, string $tag): array |
||
92 | { |
||
93 | if ('' === trim($tag)) { |
||
94 | return [$text]; |
||
95 | } |
||
96 | |||
97 | return preg_split('/(?=<'.$tag.'([^>])*>)/', $text, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE) ?: [$text]; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @throws LoaderError |
||
102 | * @throws RuntimeError |
||
103 | * @throws SyntaxError |
||
104 | */ |
||
105 | public function generatePager(BasePager $pager, array $options = []): string |
||
106 | { |
||
107 | $data = array_merge(array_merge($this->options, $options), [ |
||
108 | 'itemsCount' => $pager->count(), |
||
109 | 'limit' => max(1, $pager->getMaxPerPage()), |
||
110 | 'currentPage' => $pager->getPage(), |
||
111 | ]); |
||
112 | |||
113 | $data['lastPage'] = self::getNumPages((int) $data['limit'], (int) $data['itemsCount']); |
||
114 | |||
115 | return $this->environment->render($data['template'], $data); |
||
116 | } |
||
117 | |||
118 | private static function getNumPages(int $limit, int $count): int |
||
119 | { |
||
120 | return (int) ceil($count / $limit); |
||
121 | } |
||
122 | } |
||
123 |
This interface has been deprecated. The supplier of the interface has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the interface will be removed and what other interface to use instead.