/** * Test load from cache cache */ public function testFullCache() { $node = new Node(); $node->setSubnodes(new ArrayCollection())->setId(1); $menu = new Menu(); $menu->setSubnodes(new ArrayCollection())->addSubnode($node); $this->menuRepository->expects($this->any())->method('findOneBy'); $this->cacheProvider->expects($this->once())->method('fetch')->will($this->returnValue('{"1":{"id":1,"name":null,"url":null,"subnodes":[]}}')); $this->cacheProvider->expects($this->any())->method('save'); /** * Data is required twice to test how many times data is fetched from * cache provider, and to test than both times, returned data is the * same */ $this->assertEquals($this->menuManager->loadMenuByCode('admin'), [1 => ['id' => 1, 'name' => null, 'url' => null, 'subnodes' => []]]); $this->assertEquals($this->menuManager->loadMenuByCode('admin'), [1 => ['id' => 1, 'name' => null, 'url' => null, 'subnodes' => []]]); }
/** * Load menu hidration given the code. * * If the menu is already loaded in local variable, return it. * Otherwise, if is saved into cache, load it and save it locally * Otherwise, generate the data, save it into cache and save it locally * * @param string $menuCode Menu code * * @return array|null Result */ public function loadMenuByCode($menuCode) { $key = $this->buildKey($this->key, $menuCode); if (isset($this->menus[$key])) { return $this->menus[$key]; } $menuHidrated = $this->encoder->decode($this->cache->fetch($key)); if (empty($menuHidrated)) { $menu = $this->menuRepository->findOneBy(['code' => $menuCode, 'enabled' => true]); if (!$menu instanceof MenuInterface) { return null; } $menuHidrated = $this->loadSubnodes($menu); $this->cache->save($key, $this->encoder->encode($menuHidrated)); } $this->menus[$key] = $menuHidrated; return $menuHidrated; }
/** * Build menu. * * @param string $menuCode Menu code * * @return MenuInterface Menu * * @throws Exception */ private function buildMenuFromRepository($menuCode) { $menu = $this->menuRepository->findOneBy(['code' => $menuCode, 'enabled' => true]); if (!$menu instanceof MenuInterface) { throw new Exception(sprintf('Menu "%s" not found', $menuCode)); } $this->menuObjectManager->detach($menu); return $menu; }