/** * recurse method * * @param array $items * @param string $parentId */ protected function recurse($items = [], $parentId = 'root') { foreach ($items as $item) { $link = '#'; $type = null; if (array_key_exists('document', $item)) { $type = 'document'; // remove .md extension if present $path = ends_with($item['document'], ['.md']) ? Str::remove($item['document'], '.md') : $item['document']; $link = $this->codex->url($this->ref->getProject(), $this->ref->getName(), $path); } elseif (array_key_exists('href', $item)) { $type = 'href'; $link = $item['href']; } $id = md5($item['name'] . $link); $node = $this->menu->add($id, $item['name'], $parentId); $node->setAttribute('href', $link); $node->setAttribute('id', $id); if (isset($path)) { $node->setMeta('path', $path); } if (isset($item['icon'])) { $node->setMeta('icon', $item['icon']); } if (isset($item['children'])) { $type = 'parent'; } $node->setMeta('type', $type); if (isset($item['children'])) { $this->recurse($item['children'], $id); } } }
/** * handle method * * @param \Codex\Contracts\Menus\Menu|\Codex\Menus\Menu $menu */ public function handle(Menu $menu, Project $project = null) { $menu->setAttribute('title', $project === null ? 'Pick...' : $project->getDisplayName()); $menu->setAttribute('subtitle', 'project'); foreach ($this->codex->projects->all() as $project) { # Add to menu $name = (string) $project->config('display_name'); $names = []; if (strpos($name, ' :: ') !== false) { $names = explode(' :: ', $name); $name = array_shift($names); } $href = $project->url(); $metas = compact('project'); $id = Str::slugify($name, '_'); if (!$menu->has($id)) { $node = $menu->add($id, $name, 'root', count($names) === 0 ? $metas : [], count($names) === 0 ? compact('href') : []); } $parentId = $id; while (count($names) > 0) { $name = array_shift($names); $id .= '::' . $name; $id = Str::slugify($id, '_'); if (!$menu->has($id)) { $node = $menu->add($id, $name, $parentId, $metas, count($names) === 0 ? compact('href') : []); } $parentId = $id; } } }