protected function ensureLinksCache() { if ($this->linksCache === null) { try { $pieCrust = $this->page->getApp(); $pageRepository = $pieCrust->getEnvironment()->getPageRepository(); $this->linksCache = array(); $skipNames = array('Thumbs.db'); $it = new FilesystemIterator($this->baseDir); foreach ($it as $item) { $basename = $item->getBasename(); // Skip dot files, Thumbs.db, etc. if (!$basename or $basename[0] == '.') { continue; } if (in_array($item->getFilename(), $skipNames)) { continue; } if ($item->isDir()) { $linker = new Linker($this->page, $item->getPathname()); $this->linksCache[$basename . '_'] = $linker; // We add '_' at the end of the directory name to avoid // collisions with a possibly existing page with the same // name (since we strip out the '.html' extension). // This means the user must access directories with // 'link.dirname_' instead of 'link.dirname' but hey, if // you have a better idea, send me an email! } else { $path = $item->getPathname(); try { $relativePath = PageHelper::getRelativePath($this->page); $uri = UriBuilder::buildUri($relativePath); // To get the link's page, we need to be careful with the case // where that page is the currently rendering one. This is // because it could be rendering a sub-page -- but we would be // requesting the default first page, which would effectively // change the page number *while* we're rendering, which leads // to all kinds of bad things! // TODO: obviously, there needs to be some design changes to // prevent this kind of chaotic behaviour. if ($path == $this->page->getPath()) { $page = $this->page; } else { $page = $pageRepository->getOrCreatePage($uri, $path); } $key = str_replace('.', '_', $item->getBasename('.html')); $this->linksCache[$key] = array('uri' => $uri, 'name' => $key, 'is_dir' => false, 'is_self' => $basename == $this->selfName, 'page' => new PaginationData($page)); } catch (Exception $e) { throw new PieCrustException("Error while loading page '{$path}' for linking from '{$this->page->getUri()}': " . $e->getMessage(), 0, $e); } } } if ($this->sortByName) { if (false === usort($this->linksCache, array($this, 'sortByCustom'))) { throw new PieCrustException("Error while sorting pages with the specified setting: {$this->sortByName}"); } } if ($this->selfName != null) { // Add special stuff only for the original Linker // (the one directly created by the current page). if (PageHelper::isRegular($this->page)) { // Add a link to go up to the parent directory, but stay inside // the app's pages directory. $parentBaseDir = dirname($this->baseDir); if (strlen($parentBaseDir) >= strlen($pieCrust->getPagesDir())) { $linker = new Linker($this->page, dirname($this->baseDir)); $this->linksCache['_'] = $linker; } } else { if (PageHelper::isPost($this->page)) { // Add a link to go up to the parent directory, but stay inside // the app's posts directory. $parentBaseDir = dirname($this->baseDir); if (strlen($parentBaseDir) >= strlen($pieCrust->getPostsDir())) { $linker = new Linker($this->page, dirname($this->baseDir)); $this->linksCache['_'] = $linker; } } } if ($pieCrust->getPagesDir()) { // Add a shortcut to the pages directory. $linker = new Linker($this->page, $pieCrust->getPagesDir()); $this->linksCache['_pages_'] = $linker; } if ($pieCrust->getPostsDir()) { // Add a shortcut to the posts directory. $linker = new Linker($this->page, $pieCrust->getPostsDir()); $this->linksCache['_posts_'] = $linker; } } } catch (Exception $e) { throw new PieCrustException("Error while building the links from page '{$this->page->getUri()}': " . $e->getMessage(), 0, $e); } } }