/** * Find a menu item * * @param string $id menu item identifier * @param array $with relation * @return MenuItem */ public function findItem($id, $with = []) { $key = $this->getItemCacheKey($id); return $this->cache->has($key) ? $this->cache->get($key) : call_user_func(function () use($key, $id, $with) { if ($menu = $this->repo->findItem($id, $with)) { $this->cache->put($key, $menu); } return $menu; }); }
/** * Returns descendant of item * * @param Permission $item permission instance * @return array */ public function fetchDescendant(Permission $item) { $key = $this->getCacheKey($item->siteKey, $item->name, 'descendant'); if (!($descendants = $this->cache->get($key))) { $descendants = $this->repo->fetchDescendant($item); if (!empty($descendants)) { $this->cache->put($key, $descendants); } } return $descendants; }
/** * Retrieve routes by module name * * @param string $module module name * @return InstanceRoute[] */ public function fetchByModule($module) { $key = $this->getCacheKey($module); $routes = $this->cache->has($key) ? $this->cache->get($key) : call_user_func(function () use($module, $key) { $routes = $this->repo->fetchByModule($module); if (count($routes) > 0) { $this->cache->put($key, $routes); } return $routes; }); return $routes; }
/** * Get all menu * * @param string $siteKey site key * @param array $with relation * @return Menu[] */ public function all($siteKey, $with = []) { $key = $this->getCacheKey($siteKey . '_all'); $subKey = $this->getWithKey($with); $data = $this->cache->get($key); if (!$data || !isset($data[$subKey])) { $menus = $this->repo->all($siteKey, $with); if (!empty($menus)) { $data = $data ?: []; $data[$subKey] = $menus; $this->cache->put($key, $data); } } else { $menus = $data[$subKey]; } return $menus; }
/** * get cached data * * @param string $siteKey site key * @param string $head root name * @return array */ protected function getData($siteKey, $head) { $key = $this->makeKey($siteKey, $head); if (!isset($this->bag[$key])) { $cacheKey = $this->getCacheKey($key); if (!($data = $this->cache->get($cacheKey))) { if (!($config = $this->repo->find($siteKey, $head))) { return []; } $descendant = $this->repo->fetchDescendant($siteKey, $head); $data = array_merge([$config], $descendant); $this->cache->put($cacheKey, $data); } $this->bag[$key] = $data; } return $this->bag[$key]; }
/** * All cache data by head keyword * * @param string $head head keyword * @return mixed */ protected function getAll($head) { return $this->cache->get($this->getCacheKey($head)); }