These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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; |
||
10 | |||
11 | /** |
||
12 | * Extension for article bundle. |
||
13 | */ |
||
14 | class ArticleTwigExtension extends Twig_Extension |
||
0 ignored issues
–
show
|
|||
15 | { |
||
16 | /** |
||
17 | * @var EntityManagerInterface |
||
18 | */ |
||
19 | private $em; |
||
20 | |||
21 | /** |
||
22 | * @var RouterInterface |
||
23 | */ |
||
24 | private $router; |
||
25 | |||
26 | /** |
||
27 | * ArticleTwigExtension constructor. |
||
28 | * |
||
29 | * @param EntityManagerInterface $em |
||
30 | * @param RouterInterface $router |
||
31 | */ |
||
32 | public function __construct(EntityManagerInterface $em, RouterInterface $router) |
||
33 | { |
||
34 | $this->em = $em; |
||
35 | $this->router = $router; |
||
36 | |||
37 | if (\func_num_args() > 2) { |
||
38 | @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); |
||
39 | } |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Returns a list of functions to add to the existing list. |
||
44 | * |
||
45 | * @return array An array of functions |
||
46 | */ |
||
47 | public function getFunctions() |
||
48 | { |
||
49 | return array( |
||
50 | new \Twig_SimpleFunction( |
||
0 ignored issues
–
show
The class
Twig_SimpleFunction has been deprecated with message: since Twig 2.7, use "Twig\TwigFunction" instead
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.
Loading history...
|
|||
51 | 'get_article_tag_path', array($this, 'getArticleTagRouterPath') |
||
52 | ), |
||
53 | new \Twig_SimpleFunction( |
||
0 ignored issues
–
show
The class
Twig_SimpleFunction has been deprecated with message: since Twig 2.7, use "Twig\TwigFunction" instead
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.
Loading history...
|
|||
54 | 'get_article_category_path', array($this, 'getArticleCategoryRouterPath') |
||
55 | ), |
||
56 | new \Twig_SimpleFunction( |
||
0 ignored issues
–
show
The class
Twig_SimpleFunction has been deprecated with message: since Twig 2.7, use "Twig\TwigFunction" instead
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.
Loading history...
|
|||
57 | 'get_article_categories', array($this, 'getCategories') |
||
58 | ), |
||
59 | new \Twig_SimpleFunction( |
||
0 ignored issues
–
show
The class
Twig_SimpleFunction has been deprecated with message: since Twig 2.7, use "Twig\TwigFunction" instead
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message. The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.
Loading history...
|
|||
60 | 'get_article_tags', array($this, 'getTags') |
||
61 | ), |
||
62 | ); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Get tags array for view. |
||
67 | * |
||
68 | * @param Request $request |
||
69 | * @param string $className |
||
70 | * |
||
71 | * @return array |
||
72 | */ |
||
73 | View Code Duplication | public function getTags(Request $request, $className) |
|
74 | { |
||
75 | $context = array(); |
||
76 | |||
77 | $tagRepository = $this->em->getRepository($className); |
||
78 | $context['tags'] = $tagRepository->findBy(array(), array('name' => 'ASC')); |
||
79 | |||
80 | $searchTag = $request->get('tag') ? explode(',', $request->get('tag')) : null; |
||
81 | if ($searchTag) { |
||
82 | $context['activeTag'] = true; |
||
83 | $context['activeTags'] = $searchTag; |
||
84 | } |
||
85 | |||
86 | return $context; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Get categories array for view. |
||
91 | * |
||
92 | * @param Request $request |
||
93 | * @param string $className |
||
94 | * |
||
95 | * @return array |
||
96 | */ |
||
97 | View Code Duplication | public function getCategories(Request $request, $className) |
|
98 | { |
||
99 | $context = array(); |
||
100 | |||
101 | $categoryRepository = $this->em->getRepository($className); |
||
102 | $context['categories'] = $categoryRepository->findBy(array(), array('name' => 'ASC')); |
||
103 | |||
104 | $searchCategory = $request->get('category') ? explode(',', $request->get('category')) : null; |
||
105 | if ($searchCategory) { |
||
106 | $context['activeCategory'] = true; |
||
107 | $context['activeCategories'] = $searchCategory; |
||
108 | } |
||
109 | |||
110 | return $context; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * @param string $slug |
||
115 | * @param string $tag |
||
116 | * @param string $locale |
||
117 | * @param array $parameters |
||
118 | * @param int $referenceType |
||
119 | * |
||
120 | * @return string |
||
121 | */ |
||
122 | public function getArticleTagRouterPath($slug, $tag, $locale, $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH) |
||
123 | { |
||
124 | $routeName = sprintf('_slug_tag_%s', $locale); |
||
125 | |||
126 | return $this->getArticleRouterPath($routeName, 'tag', $slug, $tag, $locale, $parameters, $referenceType); |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * @param string $slug |
||
131 | * @param string $category |
||
132 | * @param string $locale |
||
133 | * @param array $parameters |
||
134 | * @param int $referenceType |
||
135 | * |
||
136 | * @return string |
||
137 | */ |
||
138 | public function getArticleCategoryRouterPath($slug, $category, $locale, $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH) |
||
139 | { |
||
140 | $routeName = sprintf('_slug_category_%s', $locale); |
||
141 | |||
142 | return $this->getArticleRouterPath($routeName, 'category', $slug, $category, $locale, $parameters, $referenceType); |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * @return string |
||
147 | */ |
||
148 | public function getName() |
||
149 | { |
||
150 | return 'article_twig_extension'; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * @param string $routeName |
||
155 | * @param string $type |
||
156 | * @param string $slug |
||
157 | * @param string $tagOrCategory |
||
158 | * @param string $locale |
||
159 | * @param array $parameters |
||
160 | * @param int $referenceType |
||
161 | * |
||
162 | * @return string |
||
163 | */ |
||
164 | protected function getArticleRouterPath($routeName, $type, $slug, $tagOrCategory, $locale, $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH) |
||
165 | { |
||
166 | if (!$this->articleRouteExists($type, $locale)) { |
||
167 | $routeName = '_slug'; |
||
168 | } |
||
169 | if (!isset($parameters[$type])) { |
||
170 | $parameters[$type] = $tagOrCategory; |
||
171 | } |
||
172 | if (!isset($parameters['url'])) { |
||
173 | $parameters['url'] = $slug; |
||
174 | } |
||
175 | if (!isset($parameters['_locale'])) { |
||
176 | $parameters['_locale'] = $locale; |
||
177 | } |
||
178 | |||
179 | return $this->router->generate($routeName, $parameters, $referenceType); |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * @param string $type |
||
184 | * @param string $locale |
||
185 | * |
||
186 | * @return bool |
||
187 | */ |
||
188 | protected function articleRouteExists($type, $locale) |
||
189 | { |
||
190 | $routeName = sprintf('_slug_%s_%s', $type, $locale); |
||
191 | |||
192 | try { |
||
193 | return !\is_null($this->router->getRouteCollection()->get($routeName)); |
||
194 | } catch (\Exception $e) { |
||
195 | return false; |
||
196 | } |
||
197 | } |
||
198 | } |
||
199 |
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.