1 | <?php |
||
14 | class MenuBuilder |
||
15 | { |
||
16 | protected $currentUri; |
||
17 | protected $entityManager; |
||
18 | protected $factory; |
||
19 | protected $knpMenu; |
||
20 | protected $defaultLocale; |
||
21 | protected $request; |
||
22 | protected $menuManipulator; |
||
23 | |||
24 | /** |
||
25 | * MenuBuilder constructor. |
||
26 | * |
||
27 | * @param EntityManager $entityManager |
||
28 | * @param FactoryInterface $factory |
||
29 | */ |
||
30 | public function __construct(MenuManipulator $menuManipulator, RequestStack $requestStack, EntityManager $entityManager, FactoryInterface $factory) |
||
40 | |||
41 | /** |
||
42 | * Check if locale is valid. |
||
43 | * |
||
44 | * @param $locale |
||
45 | * |
||
46 | * @return bool |
||
47 | */ |
||
48 | public static function isValidLocale($locale) |
||
56 | |||
57 | /** |
||
58 | * The parameter $locale must be defined in your |
||
59 | * symfony configuration file under parameters. |
||
60 | * |
||
61 | * @param $locale String |
||
62 | * |
||
63 | * @return $this |
||
64 | */ |
||
65 | public function setDefaultLocale($locale) |
||
78 | |||
79 | /** |
||
80 | * Return default locale. |
||
81 | * |
||
82 | * @return string |
||
83 | */ |
||
84 | public function getDefaultLocale() |
||
88 | |||
89 | /** |
||
90 | * Check if the machineName is valid. |
||
91 | * |
||
92 | * @param $machineName |
||
93 | * |
||
94 | * @return bool |
||
95 | */ |
||
96 | public static function isValidMachineName($machineName) |
||
104 | |||
105 | /** |
||
106 | * Retrun null or a KnpMenuItem instance. |
||
107 | * |
||
108 | * @return null|KnpMenuItem |
||
109 | */ |
||
110 | public function getKnpMenu() |
||
114 | |||
115 | /** |
||
116 | * Set the KnpMenuItem instance. |
||
117 | * |
||
118 | * @param KnpMenuItem $knpMenu |
||
119 | * |
||
120 | * @return $this |
||
121 | */ |
||
122 | public function setKnpMenu(KnpMenuItem $knpMenu) |
||
128 | |||
129 | /** |
||
130 | * Create KnpMenuItem. |
||
131 | * |
||
132 | * @param string $machineName The name of menu |
||
133 | * @param string $locale Language code (Recommanded ISO-639) |
||
134 | * |
||
135 | * @return KnpMenuItem Get formatted menu |
||
136 | */ |
||
137 | public function createKnpMenu($machineName, $locale = null) |
||
169 | |||
170 | /** |
||
171 | * Create tree un KnpMenuItem. |
||
172 | * |
||
173 | * @param KnpMenuItem $knpMenu |
||
174 | * @param ItemInterface $item |
||
175 | * @param KnpMenuItem|null $parent |
||
176 | * |
||
177 | * @return KnpMenuItem A formatted KnpMenu |
||
178 | */ |
||
179 | protected function getTree(KnpMenuItem $knpMenu, ItemInterface $item, KnpMenuItem $parent = null) |
||
213 | } |
||
214 |
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.