5
5
*/
6
6
namespace Magento \Backend \Model ;
7
7
8
+ use Magento \Backend \Model \Menu \Item ;
9
+ use Magento \Backend \Model \Menu \Item \Factory ;
10
+ use Magento \Framework \App \ObjectManager ;
11
+ use Magento \Framework \Serialize \SerializerInterface ;
12
+ use Psr \Log \LoggerInterface ;
13
+
8
14
/**
9
15
* Backend menu model
10
16
*/
@@ -18,33 +24,54 @@ class Menu extends \ArrayObject
18
24
protected $ _path = '' ;
19
25
20
26
/**
21
- * @var \Psr\Log\ LoggerInterface
27
+ * @var LoggerInterface
22
28
*/
23
29
protected $ _logger ;
24
30
25
31
/**
26
- * @param \Psr\Log\LoggerInterface $logger
32
+ * @var Factory
33
+ */
34
+ private $ menuItemFactory ;
35
+
36
+ /**
37
+ * @var SerializerInterface
38
+ */
39
+ private $ serializer ;
40
+
41
+ /**
42
+ * Menu constructor
43
+ *
44
+ * @param LoggerInterface $logger
27
45
* @param string $pathInMenuStructure
46
+ * @param Factory|null $menuItemFactory
47
+ * @param SerializerInterface|null $serializer
28
48
*/
29
- public function __construct (\Psr \Log \LoggerInterface $ logger , $ pathInMenuStructure = '' )
30
- {
49
+ public function __construct (
50
+ LoggerInterface $ logger ,
51
+ $ pathInMenuStructure = '' ,
52
+ Factory $ menuItemFactory = null ,
53
+ SerializerInterface $ serializer = null
54
+ ) {
31
55
if ($ pathInMenuStructure ) {
32
56
$ this ->_path = $ pathInMenuStructure . '/ ' ;
33
57
}
34
58
$ this ->_logger = $ logger ;
35
59
$ this ->setIteratorClass (\Magento \Backend \Model \Menu \Iterator::class);
60
+ $ this ->menuItemFactory = $ menuItemFactory ?: ObjectManager::getInstance ()
61
+ ->create (Factory::class);
62
+ $ this ->serializer = $ serializer ?: ObjectManager::getInstance ()->create (SerializerInterface::class);
36
63
}
37
64
38
65
/**
39
66
* Add child to menu item
40
67
*
41
- * @param \Magento\Backend\Model\Menu\ Item $item
68
+ * @param Item $item
42
69
* @param string $parentId
43
70
* @param int $index
44
71
* @return void
45
72
* @throws \InvalidArgumentException
46
73
*/
47
- public function add (\ Magento \ Backend \ Model \ Menu \ Item $ item , $ parentId = null , $ index = null )
74
+ public function add (Item $ item , $ parentId = null , $ index = null )
48
75
{
49
76
if ($ parentId !== null ) {
50
77
$ parentItem = $ this ->get ($ parentId );
@@ -69,13 +96,13 @@ public function add(\Magento\Backend\Model\Menu\Item $item, $parentId = null, $i
69
96
* Retrieve menu item by id
70
97
*
71
98
* @param string $itemId
72
- * @return \Magento\Backend\Model\Menu\ Item|null
99
+ * @return Item|null
73
100
*/
74
101
public function get ($ itemId )
75
102
{
76
103
$ result = null ;
104
+ /** @var Item $item */
77
105
foreach ($ this as $ item ) {
78
- /** @var $item \Magento\Backend\Model\Menu\Item */
79
106
if ($ item ->getId () == $ itemId ) {
80
107
$ result = $ item ;
81
108
break ;
@@ -116,8 +143,8 @@ public function move($itemId, $toItemId, $sortIndex = null)
116
143
public function remove ($ itemId )
117
144
{
118
145
$ result = false ;
146
+ /** @var Item $item */
119
147
foreach ($ this as $ key => $ item ) {
120
- /** @var $item \Magento\Backend\Model\Menu\Item */
121
148
if ($ item ->getId () == $ itemId ) {
122
149
unset($ this [$ key ]);
123
150
$ result = true ;
@@ -144,8 +171,8 @@ public function remove($itemId)
144
171
public function reorder ($ itemId , $ position )
145
172
{
146
173
$ result = false ;
174
+ /** @var Item $item */
147
175
foreach ($ this as $ key => $ item ) {
148
- /** @var $item \Magento\Backend\Model\Menu\Item */
149
176
if ($ item ->getId () == $ itemId ) {
150
177
unset($ this [$ key ]);
151
178
$ this ->add ($ item , null , $ position );
@@ -161,23 +188,23 @@ public function reorder($itemId, $position)
161
188
/**
162
189
* Check whether provided item is last in list
163
190
*
164
- * @param \Magento\Backend\Model\Menu\ Item $item
191
+ * @param Item $item
165
192
* @return bool
166
193
*/
167
- public function isLast (\ Magento \ Backend \ Model \ Menu \ Item $ item )
194
+ public function isLast (Item $ item )
168
195
{
169
196
return $ this ->offsetGet (max (array_keys ($ this ->getArrayCopy ())))->getId () == $ item ->getId ();
170
197
}
171
198
172
199
/**
173
200
* Find first menu item that user is able to access
174
201
*
175
- * @return \Magento\Backend\Model\Menu\ Item|null
202
+ * @return Item|null
176
203
*/
177
204
public function getFirstAvailable ()
178
205
{
179
206
$ result = null ;
180
- /** @var $item \Magento\Backend\Model\Menu\Item */
207
+ /** @var Item $item */
181
208
foreach ($ this as $ item ) {
182
209
if ($ item ->isAllowed () && !$ item ->isDisabled ()) {
183
210
if ($ item ->hasChildren ()) {
@@ -198,7 +225,7 @@ public function getFirstAvailable()
198
225
* Get parent items by item id
199
226
*
200
227
* @param string $itemId
201
- * @return \Magento\Backend\Model\Menu\ Item[]
228
+ * @return Item[]
202
229
*/
203
230
public function getParentItems ($ itemId )
204
231
{
@@ -217,8 +244,8 @@ public function getParentItems($itemId)
217
244
*/
218
245
protected function _findParentItems ($ menu , $ itemId , &$ parents )
219
246
{
247
+ /** @var Item $item */
220
248
foreach ($ menu as $ item ) {
221
- /** @var $item \Magento\Backend\Model\Menu\Item */
222
249
if ($ item ->getId () == $ itemId ) {
223
250
return true ;
224
251
}
@@ -233,16 +260,54 @@ protected function _findParentItems($menu, $itemId, &$parents)
233
260
}
234
261
235
262
/**
236
- * Hack to unset logger instance which cannot be serialized
263
+ * Serialize menu
237
264
*
238
265
* @return string
239
266
*/
240
267
public function serialize ()
241
268
{
242
- $ logger = $ this ->_logger ;
243
- unset($ this ->_logger );
244
- $ result = parent ::serialize ();
245
- $ this ->_logger = $ logger ;
246
- return $ result ;
269
+ return $ this ->serializer ->serialize ($ this ->toArray ());
270
+ }
271
+
272
+ /**
273
+ * Get menu data represented as an array
274
+ *
275
+ * @return array
276
+ */
277
+ public function toArray ()
278
+ {
279
+ $ data = [];
280
+ foreach ($ this as $ item ) {
281
+ $ data [] = $ item ->toArray ();
282
+ }
283
+ return $ data ;
284
+ }
285
+
286
+ /**
287
+ * Unserialize menu
288
+ *
289
+ * @param string $serialized
290
+ * @return void
291
+ */
292
+ public function unserialize ($ serialized )
293
+ {
294
+ $ data = $ this ->serializer ->unserialize ($ serialized );
295
+ $ this ->populateFromArray ($ data );
296
+ }
297
+
298
+ /**
299
+ * Populate the menu with data from array
300
+ *
301
+ * @param array $data
302
+ * @return void
303
+ */
304
+ public function populateFromArray (array $ data )
305
+ {
306
+ $ items = [];
307
+ foreach ($ data as $ itemData ) {
308
+ $ item = $ this ->menuItemFactory ->create ($ itemData );
309
+ $ items [] = $ item ;
310
+ }
311
+ $ this ->exchangeArray ($ items );
247
312
}
248
313
}
0 commit comments