|
3 | 3 | namespace Skuola\SeoBundle\Extension\Twig;
|
4 | 4 |
|
5 | 5 | use Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination;
|
6 |
| -use Symfony\Component\HttpFoundation\RequestStack; |
| 6 | +use Symfony\Component\Routing\Generator\UrlGenerator; |
7 | 7 | use Symfony\Component\Routing\RouterInterface;
|
8 | 8 | use Symfony\Component\PropertyAccess\PropertyAccess;
|
9 |
| -use Symfony\Component\HttpFoundation\Request; |
10 | 9 |
|
11 | 10 | class PaginationMetaExtension extends \Twig_Extension
|
12 | 11 | {
|
| 12 | + const NEXT = 'next'; |
| 13 | + const PREV = 'prev'; |
| 14 | + |
13 | 15 | /**
|
14 | 16 | * @var RouterInterface
|
15 | 17 | */
|
16 | 18 | protected $router;
|
17 | 19 |
|
18 | 20 | /**
|
19 |
| - * @var null|Request |
20 |
| - */ |
21 |
| - protected $request; |
22 |
| - |
23 |
| - /** |
24 |
| - * @var string |
| 21 | + * @var PropertyAccess |
25 | 22 | */
|
26 |
| - protected $domain; |
| 23 | + protected $accessor; |
27 | 24 |
|
28 |
| - public function __construct(RequestStack $requestStack, RouterInterface $router, $domain) |
| 25 | + public function __construct(RouterInterface $router) |
29 | 26 | {
|
30 | 27 | $this->router = $router;
|
31 |
| - $this->domain = $domain; |
32 |
| - $this->request = $requestStack->getMasterRequest(); |
| 28 | + $this->accessor = PropertyAccess::createPropertyAccessor(); |
33 | 29 | }
|
34 | 30 |
|
35 | 31 | public function getFunctions()
|
36 | 32 | {
|
37 | 33 | return [
|
38 | 34 | new \Twig_SimpleFunction('render_pagination_meta', [$this, 'renderPaginationMeta'], ['is_safe' => ['html']]),
|
39 |
| - new \Twig_SimpleFunction('render_pagination_canonical', [$this, 'renderPaginationCanonical'], ['is_safe' => ['html']]), |
40 |
| - new \Twig_SimpleFunction('generate_pagination_robots_meta_tag', [$this, 'renderPaginationRobots'], ['is_safe' => ['html']]), |
| 35 | + new \Twig_SimpleFunction('render_pagination_robots', [$this, 'renderPaginationRobots'], ['is_safe' => ['html']]), |
41 | 36 | ];
|
42 | 37 | }
|
43 | 38 |
|
44 |
| - public function renderPaginationRobots($current_page_value) |
| 39 | + public function renderPaginationRobots(SlidingPagination $pagination) |
45 | 40 | {
|
46 |
| - $index_value = 'index'; $follow_value = 'follow'; |
| 41 | + $index_value = 'index'; |
| 42 | + $follow_value = 'follow'; |
47 | 43 |
|
48 |
| - if($current_page_value > 1){ |
| 44 | + if($pagination->getCurrentPageNumber() > 1){ |
49 | 45 | $index_value = 'noindex';
|
50 | 46 | }
|
51 | 47 |
|
52 | 48 | return sprintf('<meta name="robots" content="%s,%s" />', $index_value, $follow_value);
|
53 | 49 | }
|
54 | 50 |
|
55 |
| - public function renderPaginationCanonical($request, $query_string = true) |
56 |
| - { |
57 |
| - $canonical_url = $query_string ? $this->generateCanonicalUrl($request) : $request->getPathInfo(); |
58 |
| - |
59 |
| - return sprintf('<link href="%s" rel="canonical">', $canonical_url); |
60 |
| - } |
61 |
| - |
62 |
| - public function renderPaginationMeta($items) |
| 51 | + public function renderPaginationMeta(SlidingPagination $pagination) |
63 | 52 | {
|
64 |
| - if (!$items instanceof SlidingPagination) { |
65 |
| - return; |
66 |
| - } |
67 |
| - |
68 |
| - $paginationData = $items->getPaginationData(); |
69 |
| - |
| 53 | + $paginationData = $pagination->getPaginationData(); |
70 | 54 | $paginationMetas = '';
|
71 |
| - $accessor = PropertyAccess::createPropertyAccessor(); |
72 | 55 |
|
73 |
| - $prev = (int) $accessor->getValue($paginationData, '[previous]'); |
74 |
| - $next = (int) $accessor->getValue($paginationData, '[next]'); |
| 56 | + $prev = (int) $this->accessor->getValue($paginationData, '[previous]'); |
| 57 | + $next = (int) $this->accessor->getValue($paginationData, '[next]'); |
75 | 58 |
|
76 | 59 | if ($prev !== 0) {
|
77 |
| - $paginationMetas = $this->generateMeta('prev', $items, $prev); |
| 60 | + $paginationMetas = $this->generateMeta($pagination, self::PREV, $prev); |
78 | 61 | }
|
79 | 62 |
|
80 | 63 | if ($next !== 0) {
|
81 |
| - $paginationMetas .= $this->generateMeta('next', $items, $next); |
| 64 | + $paginationMetas .= $this->generateMeta($pagination, self::NEXT, $next); |
82 | 65 | }
|
83 | 66 |
|
84 | 67 | return $paginationMetas;
|
85 | 68 | }
|
86 | 69 |
|
87 |
| - protected function generateMeta($direction, $items, $pageNumber) |
| 70 | + protected function generateMeta(SlidingPagination $pagination, $direction, $page) |
88 | 71 | {
|
89 |
| - $pageUrl = sprintf('http://%s%s', $this->domain, $this->request->getPathInfo()); |
| 72 | + $routeInfo['name'] = $pagination->getRoute(); |
90 | 73 |
|
91 |
| - $queryString = []; |
92 |
| - parse_str($this->request->getQueryString(), $queryString); |
93 |
| - |
94 |
| - if ($pageNumber <= 1) { |
95 |
| - if (array_key_exists('page', $queryString)) { |
96 |
| - unset($queryString['page']); |
97 |
| - } |
98 |
| - |
99 |
| - return sprintf('<link rel="%s" href="%s">', |
100 |
| - $direction, |
101 |
| - $this->buildUrl($queryString, $pageUrl, $items->getPaginatorOptions()) |
102 |
| - ); |
103 |
| - } |
104 |
| - |
105 |
| - $queryString['page'] = $pageNumber; |
| 74 | + $routeInfo['params'] = $pagination->getQuery(compact('page')); |
106 | 75 |
|
107 | 76 | return sprintf('<link rel="%s" href="%s">',
|
108 | 77 | $direction,
|
109 |
| - $this->buildUrl($queryString, $pageUrl, $items->getPaginatorOptions()) |
| 78 | + $this->router->generate( |
| 79 | + $routeInfo['name'], |
| 80 | + $routeInfo['params'], |
| 81 | + UrlGenerator::ABSOLUTE_URL |
| 82 | + ) |
110 | 83 | );
|
111 | 84 | }
|
112 | 85 |
|
113 |
| - protected function generateCanonicalUrl(Request $request) |
114 |
| - { |
115 |
| - $route = $request->get('_route'); |
116 |
| - $parameters = $request->get('_route_params'); |
117 |
| - |
118 |
| - $current_page = $request->get('page'); |
119 |
| - |
120 |
| - if ($current_page != 1) { |
121 |
| - $parameters['page'] = $current_page; |
122 |
| - } |
123 |
| - |
124 |
| - return $this->router->generate($route, $parameters, true); |
125 |
| - } |
126 |
| - |
127 |
| - private function buildUrl(array $queryString, $pageUrl, $paginatorOptions) |
128 |
| - { |
129 |
| - foreach ($queryString as $parameter => $value) { |
130 |
| - if ( ! array_search($parameter, $paginatorOptions)) { |
131 |
| - unset($queryString[$parameter]); |
132 |
| - } |
133 |
| - } |
134 |
| - |
135 |
| - return (count($queryString) >= 1) ? sprintf('%s?%s', $pageUrl, http_build_query($queryString)) : $pageUrl; |
136 |
| - } |
137 |
| - |
138 | 86 | public function getName()
|
139 | 87 | {
|
140 | 88 | return 'twig_pagination_meta_extension';
|
|
0 commit comments