/** * Returns a path relative to a site's root directory. */ public static function getRelativePath(IPieCrust $pieCrust, $path, $stripExtension = false) { $relativePath = PathHelper::getRelativePath($pieCrust->getRootDir(), $path); if ($stripExtension) { $relativePath = preg_replace('/\\.[a-zA-Z0-9]+$/', '', $relativePath); } return $relativePath; }
/** * Gets the info about all the page files in the website. */ public function getPageFiles() { if (!$this->pagesDir) { return array(); } $pages = array(); $iterator = new \RecursiveIteratorIterator(new PagesRecursiveFilterIterator(new \RecursiveDirectoryIterator($this->pagesDir))); foreach ($iterator as $path) { $pagePath = $path->getPathname(); // Skip files in page asset folders. if (preg_match('#\\-assets[/\\\\]#', $pagePath)) { continue; } $relativePath = PathHelper::getRelativePath($this->pagesDir, $pagePath); $pages[] = array('path' => $pagePath, 'relative_path' => $relativePath); } return $pages; }
/** * Returns a path relative to a site's root directory. */ public static function getRelativePath(IPieCrust $pieCrust, $path, $stripExtension = false) { $basePath = null; $themeDir = $pieCrust->getThemeDir(); if ($themeDir and strncmp($path, $themeDir, strlen($themeDir)) == 0) { // Theme path. $basePath = $themeDir; } else { // Normal website path. $basePath = $pieCrust->getRootDir(); } if (!$basePath) { throw new PieCrustException("Can't get a relative path for '{$path}': it doesn't seem to be either a website, theme or resource path."); } $relativePath = PathHelper::getRelativePath($basePath, $path); if ($stripExtension) { $relativePath = preg_replace('/\\.[a-zA-Z0-9]+$/', '', $relativePath); } return $relativePath; }
protected function printProcessingTreeNode($node, $message = null, $recursive = false) { $indent = str_repeat(' ', $node->getLevel() + 1); $processor = $node->getProcessor() ? $node->getProcessor()->getName() : 'n/a'; $path = PathHelper::getRelativePath($this->rootDir, $this->getNodeRootDir($node) . $node->getPath()); if (!$message) { $message = ''; } $this->logger->debug("{$indent}{$path} [{$processor}] {$message}"); if ($recursive) { foreach ($node->getOutputs() as $out) { $this->printProcessingTreeNode($out, true); } } }
protected function load() { try { $pieCrust = $this->page->getApp(); $pageRepository = $pieCrust->getEnvironment()->getPageRepository(); $items = array(); $skipNames = array('Thumbs.db'); $it = new FilesystemIterator($this->baseDir); foreach ($it as $item) { $filename = $item->getFilename(); // Skip dot files, Thumbs.db, etc. if (!$filename or $filename[0] == '.') { continue; } if (in_array($filename, $skipNames)) { continue; } if ($item->isDir()) { // Skip "asset" directories. if (preg_match('/\\-assets$/', $filename)) { continue; } $linker = new Linker($this->page, $item->getPathname()); $items[$filename . '_'] = $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 file 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 { // 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 (str_replace('\\', '/', $path) == str_replace('\\', '/', $this->page->getPath())) { $page = $this->page; } else { $relativePath = PathHelper::getRelativePath($pieCrust->getPagesDir(), $path); $uri = UriBuilder::buildUri($pieCrust, $relativePath); $page = $pageRepository->getOrCreatePage($uri, $path); } $key = preg_replace('/\\.[a-zA-Z0-9]+$/', '', $filename); $key = str_replace('.', '_', $key); $items[$key] = new LinkData($page, array('name' => $key, 'is_dir' => false, 'is_self' => $page == $this->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($items, array($this, 'sortByCustom'))) { throw new PieCrustException("Error while sorting pages with the specified setting: {$this->sortByName}"); } } return $items; } catch (Exception $e) { throw new PieCrustException("Error while building the links from page '{$this->page->getUri()}': " . $e->getMessage(), 0, $e); } }
/** * Ensures the configuration has been loaded. */ protected function ensureConfig() { if ($this->config == null) { $configCache = $this->cachingEnabled ? $this->getCacheDir() : false; $configPaths = array(); if ($this->isThemeSite()) { $themeDir = false; $configPaths[] = $this->rootDir . PieCrustDefaults::THEME_CONFIG_PATH; } else { $themeDir = $this->getThemeDir(); if ($themeDir !== false) { $configPaths[] = $themeDir . PieCrustDefaults::THEME_CONFIG_PATH; } $configPaths[] = $this->rootDir . PieCrustDefaults::CONFIG_PATH; } $this->config = new PieCrustConfiguration($configPaths, $configCache); if ($themeDir !== false) { // We'll need to patch the templates directories to be relative // to the site's root, as opposed to the theme root. $relativeThemeDir = PathHelper::getRelativePath($this->rootDir, $themeDir); $this->config->addFixup(function ($i, &$c) use($relativeThemeDir) { if ($i == 0) { if (!isset($c['site'])) { return; } if (!isset($c['site']['templates_dirs'])) { return; } if (!is_array($c['site']['templates_dirs'])) { $c['site']['templates_dirs'] = array($c['site']['templates_dirs']); } foreach ($c['site']['templates_dirs'] as &$dir) { $dir = $relativeThemeDir . $dir; } } }); } } }
public function __construct($rootDir, $path) { $this->path = $path; $this->relativePath = PathHelper::getRelativePath($rootDir, $path); }