Temporary fix #6 as serialization breaks because of the Menu class

This commit is contained in:
Matias Griese
2015-03-31 14:05:41 +03:00
parent 4caef624db
commit a61c93db08
2 changed files with 57 additions and 11 deletions
+44 -6
View File
@@ -32,17 +32,19 @@ use RocketTheme\Toolbox\ArrayTraits\Export;
* @property int $group
* @property int $level
*/
class Item implements \ArrayAccess, \Iterator
class Item implements \ArrayAccess, \Iterator, \Serializable
{
use ArrayAccessWithGetters, Export;
const VERSION = 1;
protected $items;
protected $menu;
protected $groups = [];
protected $children = [];
protected $url;
public function __construct($menu, $name, array $item = [])
public function __construct(AbstractMenu $menu, $name, array $item = [])
{
$this->menu = $menu;
@@ -67,6 +69,33 @@ class Item implements \ArrayAccess, \Iterator
];
}
public function serialize()
{
// FIXME: need to create collection class to gather the sibling data.
return serialize([
'version' => static::VERSION,
'items' => $this->items,
'groups' => $this->groups,
'children' => $this->children,
'url' => $this->url
]);
}
public function unserialize($serialized)
{
// FIXME: need to create collection class to gather the sibling data.
$data = unserialize($serialized);
if (!isset($data['version']) && $data['version'] === static::VERSION) {
throw new \UnexpectedValueException('Serialized data is not valid');
}
$this->items = $data['items'];
$this->groups = $data['groups'];
$this->children = $data['children'];
$this->url = $data['url'];
}
/**
* @param string|null|bool $url
* @return string
@@ -79,12 +108,21 @@ class Item implements \ArrayAccess, \Iterator
return $this->url;
}
/**
* @return AbstractMenu
* @deprecated Need to break relationship to the menu and use a collection instead.
*/
protected function menu()
{
return $this->menu;
}
/**
* @return Item
*/
public function parent()
{
return $this->menu[$this->items['parent_id']];
return $this->menu()[$this->items['parent_id']];
}
public function columnWidth($column)
@@ -102,7 +140,7 @@ class Item implements \ArrayAccess, \Iterator
$list = [];
foreach ($this->groups as $i => $group) {
foreach ($group as $path) {
$list[$i][] = $this->menu[$path];
$list[$i][] = $this->menu()[$path];
}
}
return $list;
@@ -196,7 +234,7 @@ class Item implements \ArrayAccess, \Iterator
$children =& $this->children;
if ($children) {
$menu = $this->menu;
$menu = $this->menu();
$ordered = [];
$this->groups[0] = [];
foreach ($groups as $i => $ordering) {
@@ -251,7 +289,7 @@ class Item implements \ArrayAccess, \Iterator
*/
public function current()
{
return $this->menu[current($this->children)];
return $this->menu()[current($this->children)];
}
/**
+13 -5
View File
@@ -154,11 +154,18 @@ class Menu extends AbstractMenu
$levels = \JFactory::getUser()->getAuthorisedViewLevels();
asort($levels);
$key = 'gantry_menu_items.' . json_encode($params) . '.' . json_encode($levels) . '.' . $this->base->id;
$cache = \JFactory::getCache('mod_menu', '');
$this->items = $cache->get($key);
// FIXME: need to create collection class to gather the sibling data, otherwise caching cannot work.
//$key = 'gantry_menu_items.' . json_encode($params) . '.' . json_encode($levels) . '.' . $this->base->id;
//$cache = \JFactory::getCache('mod_menu', '');
//try {
// $this->items = $cache->get($key);
//} catch (\Exception $e) {
// $this->items = false;
//}
if (!$this->items) {
$this->items = false;
if ($this->items === false) {
$config = $this->config();
$items = isset($config['items']) ? $config['items'] : [];
@@ -262,7 +269,8 @@ class Menu extends AbstractMenu
$this->sortAll();
$cache->store($this->items, $key);
// FIXME: need to create collection class to gather the sibling data, otherwise caching cannot work.
// $cache->store($this->items, $key);
}
}
}