vendor/sulu/sulu/src/Sulu/Component/DocumentManager/NodeManager.php line 54

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Sulu.
  4.  *
  5.  * (c) Sulu GmbH
  6.  *
  7.  * This source file is subject to the MIT license that is bundled
  8.  * with this source code in the file LICENSE.
  9.  */
  10. namespace Sulu\Component\DocumentManager;
  11. use PHPCR\NodeInterface;
  12. use PHPCR\RepositoryException;
  13. use PHPCR\SessionInterface;
  14. use PHPCR\Util\NodeHelper;
  15. use PHPCR\Util\UUIDHelper;
  16. use Sulu\Component\DocumentManager\Exception\DocumentNotFoundException;
  17. /**
  18.  * The node manager is responsible for talking to the PHPCR implementation.
  19.  */
  20. class NodeManager
  21. {
  22.     /**
  23.      * @var SessionInterface
  24.      */
  25.     private $session;
  26.     public function __construct(SessionInterface $session)
  27.     {
  28.         $this->session $session;
  29.     }
  30.     /**
  31.      * Find a document with the given path or UUID.
  32.      *
  33.      * @param string $identifier UUID or path
  34.      *
  35.      * @return NodeInterface
  36.      *
  37.      * @throws DocumentNotFoundException
  38.      */
  39.     public function find($identifier)
  40.     {
  41.         try {
  42.             if (UUIDHelper::isUUID($identifier)) {
  43.                 return $this->session->getNodeByIdentifier($identifier);
  44.             }
  45.             return $this->session->getNode($identifier);
  46.         } catch (RepositoryException $e) {
  47.             throw new DocumentNotFoundException(\sprintf(
  48.                 'Could not find document with ID or path "%s"'$identifier
  49.             ), null$e);
  50.         }
  51.     }
  52.     /**
  53.      * Determine if a node exists at the specified path or if a UUID is given,
  54.      * then if a node with the UUID exists.
  55.      *
  56.      * @param string $identifier
  57.      *
  58.      * @return bool
  59.      */
  60.     public function has($identifier)
  61.     {
  62.         $this->normalizeToPath($identifier);
  63.         try {
  64.             $this->find($identifier);
  65.             return true;
  66.         } catch (DocumentNotFoundException $e) {
  67.             return false;
  68.         }
  69.     }
  70.     /**
  71.      * Remove the document with the given path or UUID.
  72.      *
  73.      * @param string $identifier ID or path
  74.      */
  75.     public function remove($identifier)
  76.     {
  77.         $identifier $this->normalizeToPath($identifier);
  78.         $this->session->removeItem($identifier);
  79.     }
  80.     /**
  81.      * Move the document with the given path or ID to the path
  82.      * of the destination document (as a child).
  83.      *
  84.      * @param string $srcId
  85.      * @param string $destId
  86.      * @param string $name
  87.      *
  88.      * @deprecated Use NodeHelper::move instead
  89.      */
  90.     public function move($srcId$destId$name)
  91.     {
  92.         $srcPath $this->normalizeToPath($srcId);
  93.         $parentDestPath $this->normalizeToPath($destId);
  94.         $destPath $parentDestPath '/' $name;
  95.         $this->session->move($srcPath$destPath);
  96.     }
  97.     /**
  98.      * Copy the document with the given path or ID to the path
  99.      * of the destination document (as a child).
  100.      *
  101.      * @param string $srcId
  102.      * @param string $destId
  103.      * @param string $name
  104.      *
  105.      * @return string
  106.      *
  107.      * @deprecated Use NodeHelper::copy instead
  108.      */
  109.     public function copy($srcId$destId$name)
  110.     {
  111.         $workspace $this->session->getWorkspace();
  112.         $srcPath $this->normalizeToPath($srcId);
  113.         $parentDestPath $this->normalizeToPath($destId);
  114.         $destPath $parentDestPath '/' $name;
  115.         $workspace->copy($srcPath$destPath);
  116.         return $destPath;
  117.     }
  118.     /**
  119.      * Save all pending changes currently recorded in this Session.
  120.      */
  121.     public function save()
  122.     {
  123.         $this->session->save();
  124.     }
  125.     /**
  126.      * Clear the current session.
  127.      */
  128.     public function clear()
  129.     {
  130.         $this->session->refresh(false);
  131.     }
  132.     /**
  133.      * Create a path.
  134.      *
  135.      * @param string $path
  136.      *
  137.      * @return NodeInterface
  138.      */
  139.     public function createPath($path)
  140.     {
  141.         $current $this->session->getRootNode();
  142.         $segments \preg_split('#/#'$pathnull\PREG_SPLIT_NO_EMPTY);
  143.         foreach ($segments as $segment) {
  144.             if ($current->hasNode($segment)) {
  145.                 $current $current->getNode($segment);
  146.             } else {
  147.                 $current $current->addNode($segment);
  148.                 $current->addMixin('mix:referenceable');
  149.                 $current->setProperty('jcr:uuid'UUIDHelper::generateUUID());
  150.             }
  151.         }
  152.         return $current;
  153.     }
  154.     /**
  155.      * Purge the workspace.
  156.      */
  157.     public function purgeWorkspace()
  158.     {
  159.         NodeHelper::purgeWorkspace($this->session);
  160.     }
  161.     /**
  162.      * Normalize the given path or ID to a path.
  163.      *
  164.      * @param string $identifier
  165.      *
  166.      * @return string
  167.      */
  168.     private function normalizeToPath($identifier)
  169.     {
  170.         if (UUIDHelper::isUUID($identifier)) {
  171.             $identifier $this->session->getNodeByIdentifier($identifier)->getPath();
  172.         }
  173.         return $identifier;
  174.     }
  175. }