Skip to content

Commit 565acf5

Browse files
author
Sergii Kovalenko
committed
MAGETWO-62855: Year Copyright
2 parents 43da581 + 2d6bb43 commit 565acf5

File tree

137 files changed

+5889
-1277
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

137 files changed

+5889
-1277
lines changed

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

Lines changed: 87 additions & 22 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,54 @@ 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+
* Menu constructor
43+
*
44+
* @param LoggerInterface $logger
2745
* @param string $pathInMenuStructure
46+
* @param Factory|null $menuItemFactory
47+
* @param SerializerInterface|null $serializer
2848
*/
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+
) {
3155
if ($pathInMenuStructure) {
3256
$this->_path = $pathInMenuStructure . '/';
3357
}
3458
$this->_logger = $logger;
3559
$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);
3663
}
3764

3865
/**
3966
* Add child to menu item
4067
*
41-
* @param \Magento\Backend\Model\Menu\Item $item
68+
* @param Item $item
4269
* @param string $parentId
4370
* @param int $index
4471
* @return void
4572
* @throws \InvalidArgumentException
4673
*/
47-
public function add(\Magento\Backend\Model\Menu\Item $item, $parentId = null, $index = null)
74+
public function add(Item $item, $parentId = null, $index = null)
4875
{
4976
if ($parentId !== null) {
5077
$parentItem = $this->get($parentId);
@@ -69,13 +96,13 @@ public function add(\Magento\Backend\Model\Menu\Item $item, $parentId = null, $i
6996
* Retrieve menu item by id
7097
*
7198
* @param string $itemId
72-
* @return \Magento\Backend\Model\Menu\Item|null
99+
* @return Item|null
73100
*/
74101
public function get($itemId)
75102
{
76103
$result = null;
104+
/** @var Item $item */
77105
foreach ($this as $item) {
78-
/** @var $item \Magento\Backend\Model\Menu\Item */
79106
if ($item->getId() == $itemId) {
80107
$result = $item;
81108
break;
@@ -116,8 +143,8 @@ public function move($itemId, $toItemId, $sortIndex = null)
116143
public function remove($itemId)
117144
{
118145
$result = false;
146+
/** @var Item $item */
119147
foreach ($this as $key => $item) {
120-
/** @var $item \Magento\Backend\Model\Menu\Item */
121148
if ($item->getId() == $itemId) {
122149
unset($this[$key]);
123150
$result = true;
@@ -144,8 +171,8 @@ public function remove($itemId)
144171
public function reorder($itemId, $position)
145172
{
146173
$result = false;
174+
/** @var Item $item */
147175
foreach ($this as $key => $item) {
148-
/** @var $item \Magento\Backend\Model\Menu\Item */
149176
if ($item->getId() == $itemId) {
150177
unset($this[$key]);
151178
$this->add($item, null, $position);
@@ -161,23 +188,23 @@ public function reorder($itemId, $position)
161188
/**
162189
* Check whether provided item is last in list
163190
*
164-
* @param \Magento\Backend\Model\Menu\Item $item
191+
* @param Item $item
165192
* @return bool
166193
*/
167-
public function isLast(\Magento\Backend\Model\Menu\Item $item)
194+
public function isLast(Item $item)
168195
{
169196
return $this->offsetGet(max(array_keys($this->getArrayCopy())))->getId() == $item->getId();
170197
}
171198

172199
/**
173200
* Find first menu item that user is able to access
174201
*
175-
* @return \Magento\Backend\Model\Menu\Item|null
202+
* @return Item|null
176203
*/
177204
public function getFirstAvailable()
178205
{
179206
$result = null;
180-
/** @var $item \Magento\Backend\Model\Menu\Item */
207+
/** @var Item $item */
181208
foreach ($this as $item) {
182209
if ($item->isAllowed() && !$item->isDisabled()) {
183210
if ($item->hasChildren()) {
@@ -198,7 +225,7 @@ public function getFirstAvailable()
198225
* Get parent items by item id
199226
*
200227
* @param string $itemId
201-
* @return \Magento\Backend\Model\Menu\Item[]
228+
* @return Item[]
202229
*/
203230
public function getParentItems($itemId)
204231
{
@@ -217,8 +244,8 @@ public function getParentItems($itemId)
217244
*/
218245
protected function _findParentItems($menu, $itemId, &$parents)
219246
{
247+
/** @var Item $item */
220248
foreach ($menu as $item) {
221-
/** @var $item \Magento\Backend\Model\Menu\Item */
222249
if ($item->getId() == $itemId) {
223250
return true;
224251
}
@@ -233,16 +260,54 @@ protected function _findParentItems($menu, $itemId, &$parents)
233260
}
234261

235262
/**
236-
* Hack to unset logger instance which cannot be serialized
263+
* Serialize menu
237264
*
238265
* @return string
239266
*/
240267
public function serialize()
241268
{
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);
247312
}
248313
}

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

Lines changed: 46 additions & 45 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

@@ -130,6 +131,7 @@ class Item
130131
* Serialized submenu string
131132
*
132133
* @var string
134+
* @deprecated
133135
*/
134136
protected $_serializedSubmenu;
135137

@@ -167,22 +169,13 @@ public function __construct(
167169
) {
168170
$this->_validator = $validator;
169171
$this->_validator->validate($data);
170-
171172
$this->_moduleManager = $moduleManager;
172173
$this->_acl = $authorization;
173174
$this->_scopeConfig = $scopeConfig;
174175
$this->_menuFactory = $menuFactory;
175176
$this->_urlModel = $urlModel;
176-
$this->_moduleName = isset($data['module']) ? $data['module'] : 'Magento_Backend';
177177
$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', '');
178+
$this->populateFromArray($data);
186179
}
187180

188181
/**
@@ -215,13 +208,13 @@ public function getId()
215208
*/
216209
public function hasChildren()
217210
{
218-
return !is_null($this->_submenu) && (bool)$this->_submenu->count();
211+
return (null !== $this->_submenu) && (bool)$this->_submenu->count();
219212
}
220213

221214
/**
222215
* Retrieve submenu
223216
*
224-
* @return \Magento\Backend\Model\Menu
217+
* @return Menu
225218
*/
226219
public function getChildren()
227220
{
@@ -425,7 +418,7 @@ protected function _isModuleDependenciesAvailable()
425418
protected function _isConfigDependenciesAvailable()
426419
{
427420
if ($this->_dependsOnConfig) {
428-
return $this->_scopeConfig->isSetFlag((string)$this->_dependsOnConfig, \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
421+
return $this->_scopeConfig->isSetFlag((string)$this->_dependsOnConfig, ScopeInterface::SCOPE_STORE);
429422
}
430423
return true;
431424
}
@@ -445,45 +438,53 @@ public function isAllowed()
445438
}
446439

447440
/**
448-
* @return string[]
441+
* Get menu item data represented as an array
442+
*
443+
* @return array
449444
*/
450-
public function __sleep()
445+
public function toArray()
451446
{
452-
if ($this->_submenu) {
453-
$this->_serializedSubmenu = $this->_submenu->serialize();
454-
}
455447
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'
448+
'parent_id' => $this->_parentId,
449+
'module_name' => $this->_moduleName,
450+
'sort_index' => $this->_sortIndex,
451+
'depends_on_config' => $this->_dependsOnConfig,
452+
'id' => $this->_id,
453+
'resource' => $this->_resource,
454+
'path' => $this->_path,
455+
'action' => $this->_action,
456+
'depends_on_module' => $this->_dependsOnModule,
457+
'tooltip' => $this->_tooltip,
458+
'title' => $this->_title,
459+
'sub_menu' => isset($this->_submenu) ? $this->_submenu->toArray() : null
468460
];
469461
}
470462

471463
/**
464+
* Populate the menu item with data from array
465+
*
466+
* @param array $data
472467
* @return void
473468
*/
474-
public function __wakeup()
469+
public function populateFromArray(array $data)
475470
{
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);
471+
$this->_parentId = $this->_getArgument($data, 'parent_id');
472+
$this->_moduleName = $this->_getArgument($data, 'module_name', 'Magento_Backend');
473+
$this->_sortIndex = $this->_getArgument($data, 'sort_index');
474+
$this->_dependsOnConfig = $this->_getArgument($data, 'depends_on_config');
475+
$this->_id = $this->_getArgument($data, 'id');
476+
$this->_resource = $this->_getArgument($data, 'resource');
477+
$this->_path = $this->_getArgument($data, 'path', '');
478+
$this->_action = $this->_getArgument($data, 'action');
479+
$this->_dependsOnModule = $this->_getArgument($data, 'depends_on_module');
480+
$this->_tooltip = $this->_getArgument($data, 'tooltip', '');
481+
$this->_title = $this->_getArgument($data, 'title');
482+
if (isset($data['sub_menu'])) {
483+
$menu = $this->_menuFactory->create();
484+
$menu->populateFromArray($data['sub_menu']);
485+
$this->_submenu = $menu;
486+
} else {
487+
$this->_submenu = null;
487488
}
488489
}
489490
}

0 commit comments

Comments
 (0)