Skip to content

Commit 24b608a

Browse files
author
Joan He
committed
MAGETWO-62253: Refactor code to remove unserialize
1 parent 813fad8 commit 24b608a

File tree

4 files changed

+284
-92
lines changed

4 files changed

+284
-92
lines changed

app/code/Magento/Backend/Model/Menu.php

Lines changed: 85 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
*/
66
namespace Magento\Backend\Model;
77

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+
814
/**
915
* Backend menu model
1016
*/
@@ -18,33 +24,52 @@ class Menu extends \ArrayObject
1824
protected $_path = '';
1925

2026
/**
21-
* @var \Psr\Log\LoggerInterface
27+
* @var LoggerInterface
2228
*/
2329
protected $_logger;
2430

2531
/**
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+
* @param LoggerInterface $logger
2743
* @param string $pathInMenuStructure
44+
* @param Factory $menuItemFactory
45+
* @param SerializerInterface $serializer
2846
*/
29-
public function __construct(\Psr\Log\LoggerInterface $logger, $pathInMenuStructure = '')
30-
{
47+
public function __construct(
48+
LoggerInterface $logger,
49+
$pathInMenuStructure = '',
50+
Factory $menuItemFactory = null,
51+
SerializerInterface $serializer = null
52+
) {
3153
if ($pathInMenuStructure) {
3254
$this->_path = $pathInMenuStructure . '/';
3355
}
3456
$this->_logger = $logger;
3557
$this->setIteratorClass(\Magento\Backend\Model\Menu\Iterator::class);
58+
$this->menuItemFactory = $menuItemFactory ?: ObjectManager::getInstance()
59+
->create(Factory::class);
60+
$this->serializer = $serializer ?: ObjectManager::getInstance()->create(SerializerInterface::class);
3661
}
3762

3863
/**
3964
* Add child to menu item
4065
*
41-
* @param \Magento\Backend\Model\Menu\Item $item
66+
* @param Item $item
4267
* @param string $parentId
4368
* @param int $index
4469
* @return void
4570
* @throws \InvalidArgumentException
4671
*/
47-
public function add(\Magento\Backend\Model\Menu\Item $item, $parentId = null, $index = null)
72+
public function add(Item $item, $parentId = null, $index = null)
4873
{
4974
if ($parentId !== null) {
5075
$parentItem = $this->get($parentId);
@@ -69,13 +94,13 @@ public function add(\Magento\Backend\Model\Menu\Item $item, $parentId = null, $i
6994
* Retrieve menu item by id
7095
*
7196
* @param string $itemId
72-
* @return \Magento\Backend\Model\Menu\Item|null
97+
* @return Item|null
7398
*/
7499
public function get($itemId)
75100
{
76101
$result = null;
102+
/** @var Item $item */
77103
foreach ($this as $item) {
78-
/** @var $item \Magento\Backend\Model\Menu\Item */
79104
if ($item->getId() == $itemId) {
80105
$result = $item;
81106
break;
@@ -116,8 +141,8 @@ public function move($itemId, $toItemId, $sortIndex = null)
116141
public function remove($itemId)
117142
{
118143
$result = false;
144+
/** @var Item $item */
119145
foreach ($this as $key => $item) {
120-
/** @var $item \Magento\Backend\Model\Menu\Item */
121146
if ($item->getId() == $itemId) {
122147
unset($this[$key]);
123148
$result = true;
@@ -144,8 +169,8 @@ public function remove($itemId)
144169
public function reorder($itemId, $position)
145170
{
146171
$result = false;
172+
/** @var Item $item */
147173
foreach ($this as $key => $item) {
148-
/** @var $item \Magento\Backend\Model\Menu\Item */
149174
if ($item->getId() == $itemId) {
150175
unset($this[$key]);
151176
$this->add($item, null, $position);
@@ -161,23 +186,23 @@ public function reorder($itemId, $position)
161186
/**
162187
* Check whether provided item is last in list
163188
*
164-
* @param \Magento\Backend\Model\Menu\Item $item
189+
* @param Item $item
165190
* @return bool
166191
*/
167-
public function isLast(\Magento\Backend\Model\Menu\Item $item)
192+
public function isLast(Item $item)
168193
{
169194
return $this->offsetGet(max(array_keys($this->getArrayCopy())))->getId() == $item->getId();
170195
}
171196

172197
/**
173198
* Find first menu item that user is able to access
174199
*
175-
* @return \Magento\Backend\Model\Menu\Item|null
200+
* @return Item|null
176201
*/
177202
public function getFirstAvailable()
178203
{
179204
$result = null;
180-
/** @var $item \Magento\Backend\Model\Menu\Item */
205+
/** @var Item $item */
181206
foreach ($this as $item) {
182207
if ($item->isAllowed() && !$item->isDisabled()) {
183208
if ($item->hasChildren()) {
@@ -198,7 +223,7 @@ public function getFirstAvailable()
198223
* Get parent items by item id
199224
*
200225
* @param string $itemId
201-
* @return \Magento\Backend\Model\Menu\Item[]
226+
* @return Item[]
202227
*/
203228
public function getParentItems($itemId)
204229
{
@@ -217,8 +242,8 @@ public function getParentItems($itemId)
217242
*/
218243
protected function _findParentItems($menu, $itemId, &$parents)
219244
{
245+
/** @var Item $item */
220246
foreach ($menu as $item) {
221-
/** @var $item \Magento\Backend\Model\Menu\Item */
222247
if ($item->getId() == $itemId) {
223248
return true;
224249
}
@@ -241,8 +266,51 @@ public function serialize()
241266
{
242267
$logger = $this->_logger;
243268
unset($this->_logger);
244-
$result = parent::serialize();
269+
$result = $this->serializer->serialize($this->toArray());
245270
$this->_logger = $logger;
246271
return $result;
247272
}
273+
274+
/**
275+
* Get menu data represented as an array
276+
*
277+
* @return array
278+
*/
279+
public function toArray()
280+
{
281+
$data = [];
282+
foreach ($this as $item) {
283+
$data[] = $item->toArray();
284+
}
285+
return $data;
286+
}
287+
288+
/**
289+
* Unserialize menu
290+
*
291+
* @param string $serialized
292+
* @return void
293+
*/
294+
public function unserialize($serialized)
295+
{
296+
$data = $this->serializer->unserialize($serialized);
297+
$this->populateFromArray($data);
298+
}
299+
300+
/**
301+
* Populate the menu with data from array
302+
*
303+
* @param array $data
304+
* @return void
305+
*/
306+
public function populateFromArray(array $data)
307+
{
308+
$items = [];
309+
foreach ($data as $itemData) {
310+
$item = $this->menuItemFactory->create();
311+
$item->populateFromArray($itemData);
312+
$items[] = $item;
313+
}
314+
$this->exchangeArray($items);
315+
}
248316
}

app/code/Magento/Backend/Model/Menu/Item.php

Lines changed: 51 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
* See COPYING.txt for license details.
55
*/
66

7-
// @codingStandardsIgnoreFile
8-
97
namespace Magento\Backend\Model\Menu;
108

9+
use Magento\Backend\Model\Menu;
10+
use Magento\Store\Model\ScopeInterface;
11+
1112
/**
1213
* Menu item. Should be used to create nested menu structures with \Magento\Backend\Model\Menu
1314
*
@@ -102,7 +103,7 @@ class Item
102103
/**
103104
* Submenu item list
104105
*
105-
* @var \Magento\Backend\Model\Menu
106+
* @var Menu
106107
*/
107108
protected $_submenu;
108109

@@ -166,23 +167,16 @@ public function __construct(
166167
array $data = []
167168
) {
168169
$this->_validator = $validator;
169-
$this->_validator->validate($data);
170-
170+
if (!empty($data)) {
171+
$this->_validator->validate($data);
172+
$this->populateFromArray($data);
173+
}
171174
$this->_moduleManager = $moduleManager;
172175
$this->_acl = $authorization;
173176
$this->_scopeConfig = $scopeConfig;
174177
$this->_menuFactory = $menuFactory;
175178
$this->_urlModel = $urlModel;
176-
$this->_moduleName = isset($data['module']) ? $data['module'] : 'Magento_Backend';
177179
$this->_moduleList = $moduleList;
178-
179-
$this->_id = $data['id'];
180-
$this->_title = $data['title'];
181-
$this->_action = $this->_getArgument($data, 'action');
182-
$this->_resource = $this->_getArgument($data, 'resource');
183-
$this->_dependsOnModule = $this->_getArgument($data, 'dependsOnModule');
184-
$this->_dependsOnConfig = $this->_getArgument($data, 'dependsOnConfig');
185-
$this->_tooltip = $this->_getArgument($data, 'toolTip', '');
186180
}
187181

188182
/**
@@ -215,13 +209,13 @@ public function getId()
215209
*/
216210
public function hasChildren()
217211
{
218-
return !is_null($this->_submenu) && (bool)$this->_submenu->count();
212+
return (null !== $this->_submenu) && (bool)$this->_submenu->count();
219213
}
220214

221215
/**
222216
* Retrieve submenu
223217
*
224-
* @return \Magento\Backend\Model\Menu
218+
* @return Menu
225219
*/
226220
public function getChildren()
227221
{
@@ -425,7 +419,7 @@ protected function _isModuleDependenciesAvailable()
425419
protected function _isConfigDependenciesAvailable()
426420
{
427421
if ($this->_dependsOnConfig) {
428-
return $this->_scopeConfig->isSetFlag((string)$this->_dependsOnConfig, \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
422+
return $this->_scopeConfig->isSetFlag((string)$this->_dependsOnConfig, ScopeInterface::SCOPE_STORE);
429423
}
430424
return true;
431425
}
@@ -445,45 +439,56 @@ public function isAllowed()
445439
}
446440

447441
/**
448-
* @return string[]
442+
* Get menu item data represented as an array
443+
*
444+
* @return array
449445
*/
450-
public function __sleep()
446+
public function toArray()
451447
{
452-
if ($this->_submenu) {
453-
$this->_serializedSubmenu = $this->_submenu->serialize();
454-
}
455448
return [
456-
'_parentId',
457-
'_moduleName',
458-
'_sortIndex',
459-
'_dependsOnConfig',
460-
'_id',
461-
'_resource',
462-
'_path',
463-
'_action',
464-
'_dependsOnModule',
465-
'_tooltip',
466-
'_title',
467-
'_serializedSubmenu'
449+
'parent_id' => $this->_parentId,
450+
'module' => $this->_moduleName,
451+
'sort_index' => $this->_sortIndex,
452+
'depends_on_config' => $this->_dependsOnConfig,
453+
'id' => $this->_id,
454+
'resource' => $this->_resource,
455+
'path' => $this->_path,
456+
'action' => $this->_action,
457+
'depends_on_module' => $this->_dependsOnModule,
458+
'tooltip' => $this->_tooltip,
459+
'title' => $this->_title,
460+
'sub_menu' => isset($this->_submenu) ? $this->_submenu->toArray() : null
468461
];
469462
}
470463

471464
/**
465+
* Populate the menu item with data from array
466+
*
467+
* @param array $data
472468
* @return void
473469
*/
474-
public function __wakeup()
470+
public function populateFromArray(array $data)
475471
{
476-
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
477-
$this->_moduleManager = $objectManager->get(\Magento\Framework\Module\Manager::class);
478-
$this->_validator = $objectManager->get(\Magento\Backend\Model\Menu\Item\Validator::class);
479-
$this->_acl = $objectManager->get(\Magento\Framework\AuthorizationInterface::class);
480-
$this->_scopeConfig = $objectManager->get(\Magento\Framework\App\Config\ScopeConfigInterface::class);
481-
$this->_menuFactory = $objectManager->get(\Magento\Backend\Model\MenuFactory::class);
482-
$this->_urlModel = $objectManager->get(\Magento\Backend\Model\UrlInterface::class);
483-
$this->_moduleList = $objectManager->get(\Magento\Framework\Module\ModuleListInterface::class);
484-
if ($this->_serializedSubmenu) {
485-
$this->_submenu = $this->_menuFactory->create();
486-
$this->_submenu->unserialize($this->_serializedSubmenu);
472+
$this->_moduleName = isset($data['module']) ? $data['module'] : 'Magento_Backend';
473+
474+
$this->_id = $data['id'];
475+
$this->_title = $data['title'];
476+
$this->_action = $this->_getArgument($data, 'action');
477+
$this->_resource = $this->_getArgument($data, 'resource');
478+
$this->_dependsOnModule = $this->_getArgument($data, 'dependsOnModule');
479+
$this->_dependsOnConfig = $this->_getArgument($data, 'dependsOnConfig');
480+
$this->_tooltip = $this->_getArgument($data, 'toolTip', '');
481+
$this->_parentId = $this->_getArgument($data, 'parent_id');
482+
$this->_sortIndex = $this->_getArgument($data, 'sort_index');
483+
$this->_dependsOnConfig = $this->_getArgument($data, 'depends_on_config');
484+
$this->_path = $this->_getArgument($data, 'path');
485+
$this->_dependsOnModule = $this->_getArgument($data, 'depends_on_module');
486+
if (isset($data['sub_menu'])) {
487+
$menu = $this->_menuFactory->create();
488+
$menu->populateFromArray($data['sub_menu']);
489+
$this->_submenu = $menu;
490+
} else {
491+
$this->_submenu = null;
487492
}
488493
}
489494
}

0 commit comments

Comments
 (0)