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\NodeBundle\Twig; |
||
4 | |||
5 | use Doctrine\ORM\EntityManagerInterface; |
||
6 | use Kunstmaan\AdminListBundle\Entity\OverviewNavigationInterface; |
||
7 | use Kunstmaan\NodeBundle\Entity\Node; |
||
8 | use Kunstmaan\NodeBundle\Entity\NodeTranslation; |
||
9 | use Kunstmaan\NodeBundle\Entity\PageInterface; |
||
10 | use Kunstmaan\NodeBundle\Entity\StructureNode; |
||
11 | use Kunstmaan\NodeBundle\Helper\NodeMenu; |
||
12 | use Symfony\Component\HttpFoundation\RequestStack; |
||
13 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
||
14 | use Twig_Extension; |
||
15 | |||
16 | /** |
||
17 | * Extension to fetch node / translation by page in Twig templates |
||
18 | */ |
||
19 | class NodeTwigExtension extends Twig_Extension |
||
20 | { |
||
21 | /** |
||
22 | * @var EntityManagerInterface |
||
23 | */ |
||
24 | private $em; |
||
25 | |||
26 | /** |
||
27 | * @var UrlGeneratorInterface |
||
28 | */ |
||
29 | private $generator; |
||
30 | |||
31 | /** |
||
32 | * @var NodeMenu |
||
33 | */ |
||
34 | private $nodeMenu; |
||
35 | |||
36 | /** |
||
37 | * @var RequestStack |
||
38 | */ |
||
39 | private $requestStack; |
||
40 | |||
41 | /** |
||
42 | * @param \Doctrine\ORM\EntityManagerInterface $em |
||
43 | * @param \Symfony\Component\Routing\Generator\UrlGeneratorInterface $generator |
||
44 | * @param \Kunstmaan\NodeBundle\Helper\NodeMenu $nodeMenu |
||
45 | * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack |
||
46 | */ |
||
47 | public function __construct( |
||
48 | EntityManagerInterface $em, |
||
49 | UrlGeneratorInterface $generator, |
||
50 | NodeMenu $nodeMenu, |
||
51 | RequestStack $requestStack |
||
52 | ) { |
||
53 | $this->em = $em; |
||
54 | $this->generator = $generator; |
||
55 | $this->nodeMenu = $nodeMenu; |
||
56 | $this->requestStack = $requestStack; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Returns a list of functions to add to the existing list. |
||
61 | * |
||
62 | * @return array An array of functions |
||
63 | */ |
||
64 | public function getFunctions() |
||
65 | { |
||
66 | return array( |
||
67 | new \Twig_SimpleFunction( |
||
0 ignored issues
–
show
|
|||
68 | 'get_node_for', array($this, 'getNodeFor') |
||
69 | ), |
||
70 | 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...
|
|||
71 | 'get_node_translation_for', |
||
72 | array($this, 'getNodeTranslationFor') |
||
73 | ), |
||
74 | 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...
|
|||
75 | 'get_node_by_internal_name', |
||
76 | array($this, 'getNodeByInternalName') |
||
77 | ), |
||
78 | 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...
|
|||
79 | 'get_url_by_internal_name', |
||
80 | array($this, 'getUrlByInternalName') |
||
81 | ), |
||
82 | 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...
|
|||
83 | 'get_path_by_internal_name', |
||
84 | array($this, 'getPathByInternalName') |
||
85 | ), |
||
86 | 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...
|
|||
87 | 'get_page_by_node_translation', |
||
88 | array($this, 'getPageByNodeTranslation') |
||
89 | ), |
||
90 | 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...
|
|||
91 | 'get_node_menu', |
||
92 | array($this, 'getNodeMenu') |
||
93 | ), |
||
94 | new \Twig_SimpleFunction( |
||
95 | 'is_structure_node', |
||
96 | array($this, 'isStructureNode') |
||
97 | ), |
||
98 | new \Twig_SimpleFunction( |
||
99 | 'file_exists', |
||
100 | array($this, 'fileExists') |
||
101 | ), |
||
102 | new \Twig_SimpleFunction( |
||
103 | 'get_node_trans_by_node_id', |
||
104 | array($this, 'getNodeTranslationByNodeId') |
||
105 | ), |
||
106 | new \Twig_SimpleFunction( |
||
107 | 'getOverviewRoute', |
||
108 | array($this, 'getOverviewRoute') |
||
109 | ), |
||
110 | ); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Get the node translation object based on node id and language. |
||
115 | * |
||
116 | * @param int $nodeId |
||
117 | * @param string $lang |
||
118 | * |
||
119 | * @return NodeTranslation |
||
120 | */ |
||
121 | public function getNodeTranslationByNodeId($nodeId, $lang) |
||
122 | { |
||
123 | $repo = $this->em->getRepository('KunstmaanNodeBundle:NodeTranslation'); |
||
124 | |||
125 | return $repo->getNodeTranslationByNodeIdQueryBuilder($nodeId, $lang); |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * @param NodeTranslation $nodeTranslation |
||
130 | * |
||
131 | * @return null|object |
||
132 | */ |
||
133 | public function getPageByNodeTranslation(NodeTranslation $nodeTranslation) |
||
134 | { |
||
135 | return $nodeTranslation->getRef($this->em); |
||
0 ignored issues
–
show
$this->em of type object<Doctrine\ORM\EntityManagerInterface> is not a sub-type of object<Doctrine\ORM\EntityManager> . It seems like you assume a concrete implementation of the interface Doctrine\ORM\EntityManagerInterface to be always present.
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass. Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.
Loading history...
|
|||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @param PageInterface $page |
||
140 | * |
||
141 | * @return Node |
||
142 | */ |
||
143 | public function getNodeFor(PageInterface $page) |
||
144 | { |
||
145 | return $this->em->getRepository('KunstmaanNodeBundle:Node')->getNodeFor($page); |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * @param PageInterface $page |
||
150 | * |
||
151 | * @return NodeTranslation |
||
152 | */ |
||
153 | public function getNodeTranslationFor(PageInterface $page) |
||
154 | { |
||
155 | return $this->em->getRepository('KunstmaanNodeBundle:NodeTranslation')->getNodeTranslationFor($page); |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * @param string $internalName |
||
160 | * @param string $locale |
||
161 | * |
||
162 | * @return Node|null |
||
163 | */ |
||
164 | public function getNodeByInternalName($internalName, $locale) |
||
165 | { |
||
166 | $nodes = $this->em->getRepository('KunstmaanNodeBundle:Node') |
||
167 | ->getNodesByInternalName($internalName, $locale); |
||
168 | if (!empty($nodes)) { |
||
169 | return $nodes[0]; |
||
170 | } |
||
171 | |||
172 | return null; |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * @param string $internalName Internal name of the node |
||
177 | * @param string $locale Locale |
||
178 | * @param array $parameters (optional) extra parameters |
||
179 | * @param bool $relative (optional) return relative path? |
||
180 | * |
||
181 | * @return string |
||
182 | */ |
||
183 | View Code Duplication | public function getPathByInternalName($internalName, $locale, $parameters = array(), $relative = false) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
184 | { |
||
185 | $routeParameters = $this->getRouteParametersByInternalName($internalName, $locale, $parameters); |
||
186 | |||
187 | return $this->generator->generate( |
||
188 | '_slug', |
||
189 | $routeParameters, |
||
190 | $relative ? UrlGeneratorInterface::RELATIVE_PATH : UrlGeneratorInterface::ABSOLUTE_PATH |
||
191 | ); |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * @param string $internalName Internal name of the node |
||
196 | * @param string $locale Locale |
||
197 | * @param array $parameters (optional) extra parameters |
||
198 | * @param bool $schemeRelative (optional) return relative scheme? |
||
199 | * |
||
200 | * @return string |
||
201 | */ |
||
202 | View Code Duplication | public function getUrlByInternalName($internalName, $locale, $parameters = array(), $schemeRelative = false) |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository.
Loading history...
|
|||
203 | { |
||
204 | $routeParameters = $this->getRouteParametersByInternalName($internalName, $locale, $parameters); |
||
205 | |||
206 | return $this->generator->generate( |
||
207 | '_slug', |
||
208 | $routeParameters, |
||
209 | $schemeRelative ? UrlGeneratorInterface::NETWORK_PATH : UrlGeneratorInterface::ABSOLUTE_URL |
||
210 | ); |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * @param string $locale |
||
215 | * @param Node $node |
||
216 | * @param bool $includeHiddenFromNav |
||
217 | * |
||
218 | * @return NodeMenu |
||
219 | */ |
||
220 | public function getNodeMenu($locale, Node $node = null, $includeHiddenFromNav = false) |
||
221 | { |
||
222 | $request = $this->requestStack->getMasterRequest(); |
||
223 | $isPreview = $request->attributes->has('preview') && $request->attributes->get('preview') === true; |
||
224 | $this->nodeMenu->setLocale($locale); |
||
225 | $this->nodeMenu->setCurrentNode($node); |
||
226 | $this->nodeMenu->setIncludeOffline($isPreview); |
||
227 | $this->nodeMenu->setIncludeHiddenFromNav($includeHiddenFromNav); |
||
228 | |||
229 | return $this->nodeMenu; |
||
230 | } |
||
231 | |||
232 | public function isStructureNode($page) |
||
233 | { |
||
234 | return $page instanceof StructureNode; |
||
235 | } |
||
236 | |||
237 | public function fileExists($filename) |
||
238 | { |
||
239 | return file_exists($filename); |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * @param string $internalName |
||
244 | * @param string $locale |
||
245 | * @param array $parameters |
||
246 | * |
||
247 | * @return array |
||
248 | */ |
||
249 | private function getRouteParametersByInternalName($internalName, $locale, $parameters = array()) |
||
250 | { |
||
251 | $url = ''; |
||
252 | $translation = $this->em->getRepository('KunstmaanNodeBundle:NodeTranslation') |
||
253 | ->getNodeTranslationByLanguageAndInternalName($locale, $internalName); |
||
254 | |||
255 | if (!\is_null($translation)) { |
||
256 | $url = $translation->getUrl(); |
||
257 | } |
||
258 | |||
259 | return array_merge( |
||
260 | array( |
||
261 | 'url' => $url, |
||
262 | '_locale' => $locale, |
||
263 | ), |
||
264 | $parameters |
||
265 | ); |
||
266 | } |
||
267 | |||
268 | public function getOverviewRoute($node) |
||
269 | { |
||
270 | if ($node instanceof OverviewNavigationInterface) { |
||
271 | return $node->getOverViewRoute(); |
||
272 | } |
||
273 | |||
274 | return null; |
||
275 | } |
||
276 | } |
||
277 |
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.