Пример #1
0
 /**
  * Ensures the page has been formatted completely.
  */
 protected function ensureContentsFormatted()
 {
     if ($this->formattedContents == null) {
         $this->ensureConfigLoaded();
         $loader = new PageLoader($this);
         $this->formattedContents = $loader->formatContents($this->contents);
         foreach ($this->observers as $observer) {
             $observer->onPageFormatted($this);
         }
     }
 }
Пример #2
0
 /**
  * Ensures the page has been loaded from disk.
  */
 protected function ensureContentsLoaded()
 {
     if (!$this->didFormatContents) {
         $this->ensureConfigLoaded();
         $loader = new PageLoader($this);
         $this->contents = $loader->formatContents($this->contents);
         $this->didFormatContents = true;
     }
 }
Пример #3
0
 protected function loadUnsafe()
 {
     if ($this->wasCached()) {
         // Get the page from the cache.
         $configText = $this->cache->read($this->page->getUri(), 'json');
         $config = json_decode($configText, true);
         $this->page->getConfig()->set($config, false);
         // false = No need to validate this.
         if (!$this->page->getConfig()->hasValue('segments')) {
             throw new PieCrustException("Can't get segments list from cache.");
         }
         $contents = array();
         foreach ($config['segments'] as $key) {
             $segmentText = $this->cache->read($this->page->getUri() . '.' . $key, 'json');
             $contents[$key] = json_decode($segmentText);
             // The deserialized JSON object is not of type `ContentSegment` but will
             // have the same attributes so it should work all fine.
             // Sanity test: if the first content segment is null, it may mean that the
             // original page file was in a non-supported encoding.
             if (count($contents[$key]) > 0 && $contents[$key]->parts[0]->content === null) {
                 throw new PieCrustException("Corrupted cache: is the page not saved in UTF-8 encoding?");
             }
         }
         return $contents;
     } else {
         // Load the page from disk.
         $rawContents = file_get_contents($this->page->getPath());
         $rawContents = PageLoader::removeUnicodeBOM($rawContents);
         $header = Configuration::parseHeader($rawContents);
         // Set the format from the file extension.
         if (!isset($header->config['format'])) {
             $app = $this->page->getApp();
             $autoFormats = $app->getConfig()->getValueUnchecked('site/auto_formats');
             $extension = pathinfo($this->page->getPath(), PATHINFO_EXTENSION);
             if (isset($autoFormats[$extension])) {
                 $format = $autoFormats[$extension];
                 if ($format) {
                     $header->config['format'] = $autoFormats[$extension];
                 }
             }
         }
         // Set the configuration.
         $config = $this->page->getConfig();
         $config->set($header->config);
         // Set the raw content with the unparsed content segments.
         $contents = $this->parseContentSegments($rawContents, $header->textOffset);
         // Add the list of known segments to the configuration.
         foreach ($contents as $key => $segment) {
             $config->appendValue('segments', $key);
         }
         // Cache that shit out.
         if ($this->cache != null) {
             $cacheUri = $this->page->getUri();
             if ($cacheUri == '') {
                 $cacheUri = '_index';
             }
             $configText = json_encode($config->get());
             $this->cache->write($cacheUri, 'json', $configText);
             $keys = $config['segments'];
             foreach ($keys as $key) {
                 $segmentText = json_encode($contents[$key]);
                 $this->cache->write($cacheUri . '.' . $key, 'json', $segmentText);
             }
         }
         return $contents;
     }
 }