1 | <?php |
||
18 | class LinkExtension extends \Twig_Extension |
||
19 | { |
||
20 | protected $router; |
||
21 | protected $analytics; |
||
22 | protected $businessEntityHelper; // @victoire_business_page.business_entity_helper |
||
23 | protected $BusinessPageHelper; // @victoire_business_page.business_page_helper |
||
24 | protected $pageHelper; |
||
25 | protected $em; // @doctrine.orm.entity_manager |
||
26 | protected $abstractBusinessTemplates; |
||
27 | |||
28 | /** |
||
29 | * LinkExtension constructor. |
||
30 | * |
||
31 | * @param Router $router |
||
32 | * @param RequestStack $requestStack |
||
33 | * @param $analytics |
||
34 | * @param BusinessEntityHelper $businessEntityHelper |
||
35 | * @param BusinessPageHelper $BusinessPageHelper |
||
36 | * @param PageHelper $pageHelper |
||
37 | * @param EntityManager $em |
||
38 | * @param array $abstractBusinessTemplates |
||
39 | */ |
||
40 | public function __construct( |
||
41 | Router $router, |
||
42 | RequestStack $requestStack, |
||
43 | $analytics, |
||
44 | BusinessEntityHelper $businessEntityHelper, |
||
45 | BusinessPageHelper $BusinessPageHelper, |
||
46 | PageHelper $pageHelper, |
||
47 | EntityManager $em, |
||
|
|||
48 | $abstractBusinessTemplates = [] |
||
49 | ) { |
||
50 | $this->router = $router; |
||
51 | $this->request = $requestStack->getCurrentRequest(); |
||
52 | $this->analytics = $analytics; |
||
53 | $this->businessEntityHelper = $businessEntityHelper; |
||
54 | $this->BusinessPageHelper = $BusinessPageHelper; |
||
55 | $this->pageHelper = $pageHelper; |
||
56 | $this->em = $em; |
||
57 | $this->abstractBusinessTemplates = $abstractBusinessTemplates; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Returns a list of functions to add to the existing list. |
||
62 | * |
||
63 | * @return \Twig_SimpleFunction[] An array of functions |
||
64 | */ |
||
65 | public function getFunctions() |
||
66 | { |
||
67 | return [ |
||
68 | new \Twig_SimpleFunction('vic_link_url', [$this, 'victoireLinkUrl']), |
||
69 | new \Twig_SimpleFunction('vic_link', [$this, 'victoireLink'], ['is_safe' => ['html']]), |
||
70 | new \Twig_SimpleFunction('vic_menu_link', [$this, 'victoireMenuLink'], ['is_safe' => ['html']]), |
||
71 | new \Twig_SimpleFunction('vic_business_link', [$this, 'victoireBusinessLink'], ['is_safe' => ['html']]), |
||
72 | ]; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Generate the complete link (with the a tag). |
||
77 | * |
||
78 | * @param array $parameters The link parameters (go to LinkTrait to have the list) |
||
79 | * @param string $avoidRefresh Do we have to refresh or not ? |
||
80 | * @param array $url Fallback url |
||
81 | * |
||
82 | * @return string |
||
83 | */ |
||
84 | public function victoireLinkUrl($parameters, $avoidRefresh = true, $url = '#') |
||
140 | |||
141 | /** |
||
142 | * Generate the complete link (with the a tag). |
||
143 | * |
||
144 | * @param array $parameters The link parameters (go to LinkTrait to have the list) |
||
145 | * @param string $label link label |
||
146 | * @param array $attr custom attributes |
||
147 | * |
||
148 | * @return string |
||
149 | */ |
||
150 | public function victoireLink($parameters, $label, $attr = [], $currentClass = 'active', $url = '#') |
||
187 | |||
188 | /** |
||
189 | * Generate the complete menu link item (with the li tag). |
||
190 | * |
||
191 | * @param array $parameters The link parameters (go to LinkTrait to have the list) |
||
192 | * @param string $label link label |
||
193 | * @param array $attr custom attributes |
||
194 | * |
||
195 | * @return string |
||
196 | */ |
||
197 | public function victoireMenuLink($parameters, $label, $linkAttr = [], $listAttr = []) |
||
205 | |||
206 | public function victoireBusinessLink($businessEntityInstance, $templateId = null, $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH) |
||
233 | |||
234 | /** |
||
235 | * Add a given attribute to given attributes. |
||
236 | * |
||
237 | * @param string $label |
||
238 | * @param string $value |
||
239 | * @param array $attr The current attributes array |
||
240 | * |
||
241 | * @return LinkExtension |
||
242 | **/ |
||
243 | protected function addAttr($label, $value, &$attr) |
||
254 | |||
255 | /** |
||
256 | * Returns the name of the extension. |
||
257 | * |
||
258 | * @return string The extension name |
||
259 | */ |
||
260 | public function getName() |
||
264 | |||
265 | private function formatAttributes($attributes) |
||
276 | } |
||
277 |
The
EntityManager
might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManager
is closed. Any other code which depends on the same instance of theEntityManager
during this request will fail.On the other hand, if you instead inject the
ManagerRegistry
, thegetManager()
method guarantees that you will always get a usable manager instance.