Example #1
0
 /**
  * @param OutputInterface $output
  *
  * @return array
  */
 public function publish(OutputInterface $output)
 {
     $callbacks = $this->getCallbacks();
     $publishDirectory = $this->getPublishDirectory();
     $fileSystem = $this->getFileSystem();
     if (empty($callbacks)) {
         throw new \Exception('No pages queued for publishing');
     }
     if ($fileSystem->directoryExists($publishDirectory)) {
         $fileSystem->recursiveRemoveDirectory($publishDirectory);
     }
     foreach ($callbacks as $path => $callback) {
         $pageSourcePath = $this->getPagePath($path);
         // Create a layout
         $layout = new Layout($this->getLayoutRenderer());
         $layout->setSource($this->getDefaultLayout());
         $layout->addAttributes($this->global);
         // Create a page
         $page = new Page($this->getPageRenderer());
         $page->setRawContent($fileSystem->getContents($pageSourcePath));
         $page->addAttributes($this->global);
         $page->setSource($path);
         // User callback
         call_user_func($callback, $page, $layout);
         // Render the page
         $page->setAttribute('url', $this->getPageUrl($page));
         $publishedContent = $page->render();
         $layoutSourcePath = $this->getLayoutPath($layout->getSource());
         if ($layout->isEnabled() && $fileSystem->fileExists($layoutSourcePath)) {
             $layout->setRawContent($fileSystem->getContents($layoutSourcePath));
             $publishedContent = $layout->render($page);
         }
         $pagePublishPath = $this->getPagePublishPath($page);
         $pagePublishDirectory = $this->getPagePublishDirectory($page);
         if (!$fileSystem->directoryExists($pagePublishDirectory)) {
             $fileSystem->createDirectory($pagePublishDirectory, 0755, true);
         }
         $fileSystem->putContents($pagePublishPath, $publishedContent);
         $output->writeln($this->publishMessage($pageSourcePath, $pagePublishPath));
         $this->pages[] = $page->getAttributes();
     }
     $fileSystem->copyDirectory($this->getAssetsDirectory(), $publishDirectory);
     $output->writeln($this->publishMessage($this->getAssetsDirectory(), $publishDirectory));
     return $this->pages;
 }