Beispiel #1
0
 protected function bakeSinglePage(PageRenderer $pageRenderer, array $extraData = null)
 {
     $page = $pageRenderer->getPage();
     // Set the extra template data before the page's data is computed.
     if ($extraData != null) {
         $page->setExtraPageData($extraData);
     }
     // This is usually done in the PieCrustBaker, but we'll do it here too
     // because the `PageBaker` could be used on its own.
     if ($this->parameters['copy_assets']) {
         $page->setAssetUrlBaseRemap("%site_root%%uri%");
     }
     // Figure out the output HTML path.
     $bakePath = $this->getOutputPath($page);
     $this->logger->debug("  p{$page->getPageNumber()} -> {$bakePath}");
     // Figure out if we should re-bake this page.
     $doBake = true;
     if ($this->parameters['smart']) {
         // Don't rebake if the output seems up-to-date, and
         // the page isn't known to be using posts.
         $bakePathTime = @filemtime($bakePath);
         if ($bakePathTime !== false && filemtime($page->getPath()) < $bakePathTime) {
             // TODO: rebake if the page is using pagination and pages/posts were baked this time.
             $doBake = false;
         }
     }
     if (!$doBake) {
         $this->logger->debug("Not baking '{$page->getUri()}/{$page->getPageNumber()}' because '{$bakePath}' is up-to-date.");
         return false;
     }
     // If we're using portable URLs, change the site root to a relative
     // path from the page's directory.
     $savedSiteRoot = $this->setPortableSiteRoot($page->getApp(), $bakePath);
     // Render the page.
     $bakedContents = $pageRenderer->get();
     // Get some objects we need.
     $data = $page->getPageData();
     $assetor = $data['assets'];
     $paginator = $data['pagination'];
     // Copy the page.
     PathHelper::ensureDirectory(dirname($bakePath));
     file_put_contents($bakePath, $bakedContents);
     $this->bakedFiles[] = $bakePath;
     // Copy any used assets for the first sub-page.
     if ($page->getPageNumber() == 1 and $this->parameters['copy_assets']) {
         $prettyUrls = PageHelper::getConfigValue($page, 'pretty_urls', 'site');
         if ($prettyUrls) {
             $bakeAssetDir = dirname($bakePath) . '/';
         } else {
             $bakePathInfo = pathinfo($bakePath);
             $bakeAssetDir = $bakePathInfo['dirname'] . '/' . ($page->getUri() == '' ? '' : $bakePathInfo['filename']) . '/';
         }
         $assetPaths = $assetor->getAssetPathnames();
         if ($assetPaths != null) {
             PathHelper::ensureDirectory($bakeAssetDir);
             foreach ($assetPaths as $assetPath) {
                 $destinationAssetPath = $bakeAssetDir . basename($assetPath);
                 if (@copy($assetPath, $destinationAssetPath) == false) {
                     throw new PieCrustException("Can't copy '{$assetPath}' to '{$destinationAssetPath}'.");
                 }
             }
         }
     }
     // Remember a few things.
     $this->paginationDataAccessed = ($this->paginationDataAccessed or $paginator->wasPaginationDataAccessed());
     // Cleanup.
     if ($savedSiteRoot) {
         $page->getApp()->getConfig()->setValue('site/root', $savedSiteRoot);
     }
     return true;
 }
Beispiel #2
0
 protected function bakeSinglePage(PageRenderer $pageRenderer, array $extraData = null)
 {
     $page = $pageRenderer->getPage();
     // Set the extra template data before the page's data is computed.
     if ($extraData != null) {
         $page->setExtraPageData($extraData);
     }
     // This is usually done in the PieCrustBaker, but we'll do it here too
     // because the `PageBaker` could be used on its own.
     if ($this->parameters['copy_assets']) {
         $page->setAssetUrlBaseRemap("%site_root%%uri%");
     }
     // Figure out the output HTML path.
     $bakePath = $this->getOutputPath($page);
     // Figure out if we should re-bake this page.
     $doBake = true;
     if (is_file($bakePath) && $this->parameters['smart']) {
         // Don't rebake if the output seems up-to-date, and
         // the page isn't known to be using posts.
         if (filemtime($page->getPath()) < filemtime($bakePath)) {
             $bakeRecord = $this->parameters['bake_record'];
             if ($bakeRecord) {
                 $relativePath = PageHelper::getRelativePath($page);
                 if (!$bakeRecord->wasAnyPostBaked() || !$bakeRecord->isPageUsingPosts($relativePath)) {
                     $doBake = false;
                 }
             }
         }
     }
     if (!$doBake) {
         $this->logger->debug("Not baking '{$page->getUri()}/{$page->getPageNumber()}' because '{$bakePath}' is up-to-date.");
         return false;
     }
     // Backward compatibility warning and file-copy.
     // [TODO] Remove in a couple of versions.
     $copyToOldPath = false;
     $contentType = $page->getConfig()->getValue('content_type');
     $nativeExtension = pathinfo($page->getPath(), PATHINFO_EXTENSION);
     if ($contentType != 'html' && $nativeExtension == 'html') {
         $copyToOldPath = $this->bakeDir . $page->getUri();
         if ($page->getPageNumber() > 1) {
             $copyToOldPath .= $page->getPageNumber() . '/';
         }
         $copyToOldPath .= '.' . $contentType;
     }
     // If we're using portable URLs, change the site root to a relative
     // path from the page's directory.
     $savedSiteRoot = $this->setPortableSiteRoot($page->getApp(), $bakePath);
     // Render the page.
     $bakedContents = $pageRenderer->get();
     // Get some objects we need.
     $data = $page->getPageData();
     $assetor = $data['asset'];
     $paginator = $data['pagination'];
     // Copy the page.
     PathHelper::ensureDirectory(dirname($bakePath));
     file_put_contents($bakePath, $bakedContents);
     $this->bakedFiles[] = $bakePath;
     // [TODO] See previous TODO.
     if ($copyToOldPath) {
         $this->logger->warning("Page '{$page->getUri()}' has 'content_type' specified but is an HTML file.");
         $this->logger->warning("Changing a baked file's extension using 'content_type' is deprecated and will be removed in a future version.");
         $this->logger->warning("For backwards compatibility, the page will also be baked to: " . substr($copyToOldPath, strlen($this->bakeDir)));
         $this->logger->warning("To fix the problem, change the source file's extension to the desired output extension.");
         $this->logger->warning("Otherwise, just ignore these messages.");
         file_put_contents($copyToOldPath, $bakedContents);
     }
     // Copy any used assets for the first sub-page.
     if ($page->getPageNumber() == 1 and $this->parameters['copy_assets']) {
         $prettyUrls = PageHelper::getConfigValue($page, 'pretty_urls', 'site');
         if ($prettyUrls) {
             $bakeAssetDir = dirname($bakePath) . '/';
         } else {
             $bakePathInfo = pathinfo($bakePath);
             $bakeAssetDir = $bakePathInfo['dirname'] . '/' . ($page->getUri() == '' ? '' : $bakePathInfo['filename']) . '/';
         }
         $assetPaths = $assetor->getAssetPathnames();
         if ($assetPaths != null) {
             PathHelper::ensureDirectory($bakeAssetDir);
             foreach ($assetPaths as $assetPath) {
                 $destinationAssetPath = $bakeAssetDir . basename($assetPath);
                 if (@copy($assetPath, $destinationAssetPath) == false) {
                     throw new PieCrustException("Can't copy '" . $assetPath . "' to '" . $destinationAssetPath . "'.");
                 }
             }
         }
     }
     // Remember a few things.
     $this->paginationDataAccessed = ($this->paginationDataAccessed or $paginator->wasPaginationDataAccessed());
     // Cleanup.
     if ($savedSiteRoot) {
         $page->getApp()->getConfig()->setValue('site/root', $savedSiteRoot);
     }
     return true;
 }