1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\ArticleBundle\Twig; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
7
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
8
|
|
|
use Symfony\Component\Routing\RouterInterface; |
9
|
|
|
use Twig\Extension\AbstractExtension; |
10
|
|
|
use Twig\TwigFunction; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Extension for article bundle. |
14
|
|
|
* |
15
|
|
|
* @final since 5.4 |
16
|
|
|
*/ |
17
|
|
|
class ArticleTwigExtension extends AbstractExtension |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var EntityManagerInterface |
21
|
|
|
*/ |
22
|
|
|
private $em; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var RouterInterface |
26
|
|
|
*/ |
27
|
|
|
private $router; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* ArticleTwigExtension constructor. |
31
|
|
|
* |
32
|
|
|
* @param EntityManagerInterface $em |
33
|
|
|
* @param RouterInterface $router |
34
|
|
|
*/ |
35
|
|
|
public function __construct(EntityManagerInterface $em, RouterInterface $router) |
36
|
|
|
{ |
37
|
|
|
$this->em = $em; |
38
|
|
|
$this->router = $router; |
39
|
|
|
|
40
|
|
|
if (\func_num_args() > 2) { |
41
|
|
|
@trigger_error(sprintf('Passing the "request_stack" service as the third argument in "%s" is deprecated in KunstmaanArticleBundle 5.1 and will be removed in KunstmaanArticleBundle 6.0. Remove the "request_stack" argument from your service definition.', __METHOD__), E_USER_DEPRECATED); |
|
|
|
|
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Returns a list of functions to add to the existing list. |
47
|
|
|
* |
48
|
|
|
* @return array An array of functions |
|
|
|
|
49
|
|
|
*/ |
50
|
|
|
public function getFunctions() |
51
|
|
|
{ |
52
|
|
|
return array( |
53
|
|
|
new TwigFunction( |
54
|
|
|
'get_article_tag_path', array($this, 'getArticleTagRouterPath') |
55
|
|
|
), |
56
|
|
|
new TwigFunction( |
57
|
|
|
'get_article_category_path', array($this, 'getArticleCategoryRouterPath') |
58
|
|
|
), |
59
|
|
|
new TwigFunction( |
60
|
|
|
'get_article_categories', array($this, 'getCategories') |
61
|
|
|
), |
62
|
|
|
new TwigFunction( |
63
|
|
|
'get_article_tags', array($this, 'getTags') |
64
|
|
|
), |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get tags array for view. |
70
|
|
|
* |
71
|
|
|
* @param Request $request |
72
|
|
|
* @param string $className |
73
|
|
|
* |
74
|
|
|
* @return array |
|
|
|
|
75
|
|
|
*/ |
76
|
|
View Code Duplication |
public function getTags(Request $request, $className) |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
$context = array(); |
79
|
|
|
|
80
|
|
|
$tagRepository = $this->em->getRepository($className); |
81
|
|
|
$context['tags'] = $tagRepository->findBy(array(), array('name' => 'ASC')); |
82
|
|
|
|
83
|
|
|
$searchTag = $request->get('tag') ? explode(',', $request->get('tag')) : null; |
84
|
|
|
if ($searchTag) { |
85
|
|
|
$context['activeTag'] = true; |
86
|
|
|
$context['activeTags'] = $searchTag; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $context; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get categories array for view. |
94
|
|
|
* |
95
|
|
|
* @param Request $request |
96
|
|
|
* @param string $className |
97
|
|
|
* |
98
|
|
|
* @return array |
|
|
|
|
99
|
|
|
*/ |
100
|
|
View Code Duplication |
public function getCategories(Request $request, $className) |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
$context = array(); |
103
|
|
|
|
104
|
|
|
$categoryRepository = $this->em->getRepository($className); |
105
|
|
|
$context['categories'] = $categoryRepository->findBy(array(), array('name' => 'ASC')); |
106
|
|
|
|
107
|
|
|
$searchCategory = $request->get('category') ? explode(',', $request->get('category')) : null; |
108
|
|
|
if ($searchCategory) { |
109
|
|
|
$context['activeCategory'] = true; |
110
|
|
|
$context['activeCategories'] = $searchCategory; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $context; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $slug |
118
|
|
|
* @param string $tag |
119
|
|
|
* @param string $locale |
120
|
|
|
* @param array $parameters |
121
|
|
|
* @param int $referenceType |
122
|
|
|
* |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
public function getArticleTagRouterPath($slug, $tag, $locale, $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH) |
126
|
|
|
{ |
127
|
|
|
$routeName = sprintf('_slug_tag_%s', $locale); |
128
|
|
|
|
129
|
|
|
return $this->getArticleRouterPath($routeName, 'tag', $slug, $tag, $locale, $parameters, $referenceType); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param string $slug |
134
|
|
|
* @param string $category |
135
|
|
|
* @param string $locale |
136
|
|
|
* @param array $parameters |
137
|
|
|
* @param int $referenceType |
138
|
|
|
* |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
public function getArticleCategoryRouterPath($slug, $category, $locale, $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH) |
142
|
|
|
{ |
143
|
|
|
$routeName = sprintf('_slug_category_%s', $locale); |
144
|
|
|
|
145
|
|
|
return $this->getArticleRouterPath($routeName, 'category', $slug, $category, $locale, $parameters, $referenceType); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
|
|
public function getName() |
152
|
|
|
{ |
153
|
|
|
return 'article_twig_extension'; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param string $routeName |
158
|
|
|
* @param string $type |
159
|
|
|
* @param string $slug |
160
|
|
|
* @param string $tagOrCategory |
161
|
|
|
* @param string $locale |
162
|
|
|
* @param array $parameters |
163
|
|
|
* @param int $referenceType |
164
|
|
|
* |
165
|
|
|
* @return string |
166
|
|
|
*/ |
167
|
|
|
protected function getArticleRouterPath($routeName, $type, $slug, $tagOrCategory, $locale, $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH) |
168
|
|
|
{ |
169
|
|
|
if (!$this->articleRouteExists($type, $locale)) { |
170
|
|
|
$routeName = '_slug'; |
171
|
|
|
} |
172
|
|
|
if (!isset($parameters[$type])) { |
173
|
|
|
$parameters[$type] = $tagOrCategory; |
174
|
|
|
} |
175
|
|
|
if (!isset($parameters['url'])) { |
176
|
|
|
$parameters['url'] = $slug; |
177
|
|
|
} |
178
|
|
|
if (!isset($parameters['_locale'])) { |
179
|
|
|
$parameters['_locale'] = $locale; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return $this->router->generate($routeName, $parameters, $referenceType); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param string $type |
187
|
|
|
* @param string $locale |
188
|
|
|
* |
189
|
|
|
* @return bool |
190
|
|
|
*/ |
191
|
|
|
protected function articleRouteExists($type, $locale) |
192
|
|
|
{ |
193
|
|
|
$routeName = sprintf('_slug_%s_%s', $type, $locale); |
194
|
|
|
|
195
|
|
|
try { |
196
|
|
|
return !\is_null($this->router->getRouteCollection()->get($routeName)); |
197
|
|
|
} catch (\Exception $e) { |
198
|
|
|
return false; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: