Example #1
0
 /**
  * Update page object
  *
  * @param string $pageUID Page unique ID
  * @param Page $page Page object
  * @throws PageException If the page is not valid
  * @throws PageException If the page already exists
  * @throws PageException If the page does not exists
  * @return bool Return true if page updated
  */
 public function update($pageUID, $page)
 {
     $fs = new Files($this->folder);
     $pageFile = $pageUID . $this->fileExtension;
     if (!$fs->exists($pageFile)) {
         throw new PageException('Page `' . $pageUID . '` does not exists');
     }
     if (!isset($page->title, $page->slug)) {
         throw new PageException('Page not valid. Must have at least a `title` and a `slug`');
     }
     // New slug, need to rename
     if ($pageUID !== $page->slug) {
         $pageFileNew = $page->slug . $this->fileExtension;
         if ($fs->exists($pageFileNew)) {
             throw new PageException('Cannot rename, page `' . $page->slug . '` already exists');
         }
         $fs->rename($pageFile, $pageFileNew);
         $pageFile = $pageFileNew;
     }
     $data = $this->renderer->render($page);
     $fs->write($pageFile, $data);
     $this->data[$page->slug] = $page;
     return true;
 }