Beispiel #1
0
 /**
  * @dataProvider buildUriDataProvider
  */
 public function testBuildUri($relativePath, $expectedUri, $autoFormats = null)
 {
     $pc = new MockPieCrust();
     if ($autoFormats) {
         $autoFormats['html'] = '';
         $pc->getConfig()->setValue('site/auto_formats', $autoFormats);
     }
     $uri = UriBuilder::buildUri($pc, $relativePath);
     $this->assertEquals($expectedUri, $uri);
 }
Beispiel #2
0
 /**
  * Creates a new Page instance given a path.
  */
 public static function createFromPath(IPieCrust $pieCrust, $path, $pageType = IPage::TYPE_REGULAR, $pageNumber = 1, $blogKey = null, $pageKey = null, $date = null)
 {
     if ($path == null) {
         throw new InvalidArgumentException("The given path is null.");
     }
     if (!is_file($path)) {
         throw new InvalidArgumentException("The given path does not exist: " . $path);
     }
     $relativePath = PieCrustHelper::getRelativePath($pieCrust, $path, true);
     $uri = UriBuilder::buildUri($relativePath);
     return new Page($pieCrust, $uri, $path, $pageType, $blogKey, $pageKey, $pageNumber, $date);
 }
Beispiel #3
0
 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);
         }
     }
 }
 protected function ensurePagesCached()
 {
     if ($this->pages == null) {
         $pageRepository = $this->getPageRepository();
         $pageInfos = $this->getPageInfos();
         $this->pages = array();
         foreach ($pageInfos as $pageInfo) {
             $page = $pageRepository->getOrCreatePage(UriBuilder::buildUri($pageInfo['relative_path']), $pageInfo['path']);
             $this->pages[] = $page;
         }
     }
 }
 protected function ensurePagesCached()
 {
     if ($this->pages == null) {
         $this->getLog()->debug("Indexing pages...");
         // Start with the theme pages, if any.
         $pageInfos = array();
         $themeDir = $this->pieCrust->getThemeDir();
         if ($themeDir) {
             $fs = new FlatFileSystem();
             $fs->initializeForTheme($this->pieCrust);
             $themePageInfos = $fs->getPageFiles();
             foreach ($themePageInfos as $pageInfo) {
                 $pageInfos[$pageInfo->relativePath] = $pageInfo;
             }
         }
         // Override with the user pages.
         $fs = $this->getFileSystem();
         $userPageInfos = $fs->getPageFiles();
         foreach ($userPageInfos as $userPageInfo) {
             $pageInfos[$userPageInfo->relativePath] = $userPageInfo;
         }
         $this->getLog()->debug("Creating pages...");
         $pageRepository = $this->getPageRepository();
         $this->pages = array();
         foreach ($pageInfos as $pageInfo) {
             $page = $pageRepository->getOrCreatePage(UriBuilder::buildUri($this->pieCrust, $pageInfo->relativePath), $pageInfo->path);
             $this->pages[] = $page;
         }
     }
 }
Beispiel #6
0
 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);
     }
 }
 /**
  * @dataProvider buildUriDataProvider
  */
 public function testBuildUri($relativePath, $expectedUri, $stripExtension = '.html', $stripIndex = true)
 {
     $uri = UriBuilder::buildUri($relativePath, $stripExtension, $stripIndex);
     $this->assertEquals($expectedUri, $uri);
 }