|
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\TwigFilter; |
|
22
|
|
|
use Twig\TwigFunction; |
|
23
|
|
|
|
|
24
|
|
|
final class RouterTwigExtension extends AbstractExtension |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var RouterInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $router; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
private $options; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var Environment |
|
38
|
|
|
*/ |
|
39
|
|
|
private $environment; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @throws LoaderError |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct(Environment $environment, RouterInterface $router, array $options = []) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->environment = $environment; |
|
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 getFunctions() |
|
62
|
|
|
{ |
|
63
|
|
|
return [ |
|
64
|
|
|
new TwigFunction('routeExists', [$this, 'routeExists']), |
|
65
|
|
|
new TwigFunction('page_pager', [$this, 'generatePager'], [ |
|
66
|
|
|
'is_safe' => ['html'], |
|
67
|
|
|
]), |
|
68
|
|
|
]; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function getFilters() |
|
72
|
|
|
{ |
|
73
|
|
|
return [ |
|
74
|
|
|
new TwigFilter('splitTag', [$this, 'splitTag']), |
|
75
|
|
|
]; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function routeExists(string $name): bool |
|
79
|
|
|
{ |
|
80
|
|
|
return null !== $this->router->getRouteCollection()->get($name); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return string[] |
|
85
|
|
|
*/ |
|
86
|
|
|
public function splitTag(string $text, string $tag): array |
|
87
|
|
|
{ |
|
88
|
|
|
if ('' === trim($tag)) { |
|
89
|
|
|
return [$text]; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return preg_split('/(?=<'.$tag.'([^>])*>)/', $text, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE) ?: [$text]; |
|
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @throws LoaderError |
|
97
|
|
|
* @throws RuntimeError |
|
98
|
|
|
* @throws SyntaxError |
|
99
|
|
|
*/ |
|
100
|
|
|
public function generatePager(BasePager $pager, array $options = []): string |
|
101
|
|
|
{ |
|
102
|
|
|
$data = array_merge(array_merge($this->options, $options), [ |
|
103
|
|
|
'itemsCount' => $pager->count(), |
|
104
|
|
|
'limit' => max(1, $pager->getMaxPerPage()), |
|
105
|
|
|
'currentPage' => $pager->getPage(), |
|
106
|
|
|
]); |
|
107
|
|
|
|
|
108
|
|
|
$data['lastPage'] = self::getNumPages((int) $data['limit'], (int) $data['itemsCount']); |
|
109
|
|
|
|
|
110
|
|
|
return $this->environment->render($data['template'], $data); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
private static function getNumPages(int $limit, int $count): int |
|
114
|
|
|
{ |
|
115
|
|
|
return (int) ceil($count / $limit); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|