Ejemplo n.º 1
0
 /**
  * @return NodeInterface
  *
  * @throws \RuntimeException
  */
 public function getParentNode()
 {
     if (!$this->parentNode) {
         throw new \RuntimeException(sprintf('Trying to retrieve parent node when no parent node has been set. An event ' . 'listener should have set the node when persisting document "%s"', DocumentHelper::getDebugTitle($this->document)));
     }
     return $this->parentNode;
 }
 /**
  * @param PersistEvent $event
  *
  * @throws DocumentManagerException
  */
 public function handlePersist(PersistEvent $event)
 {
     $document = $event->getDocument();
     if (!$document instanceof AutoNameBehavior) {
         return;
     }
     $title = $document->getTitle();
     if (!$title) {
         throw new DocumentManagerException(sprintf('Document has no title (title is required for auto name behavior): %s)', DocumentHelper::getDebugTitle($document)));
     }
     $name = $this->slugifier->slugify($title);
     $parentNode = $event->getParentNode();
     $node = $event->hasNode() ? $event->getNode() : null;
     $name = $this->resolver->resolveName($parentNode, $name, $node);
     if (null === $node) {
         $node = $this->documentStrategy->createNodeForDocument($document, $parentNode, $name);
         $event->setNode($node);
         return;
     }
     if ($name === $node->getName()) {
         return;
     }
     $node = $event->getNode();
     $defaultLocale = $this->registry->getDefaultLocale();
     if ($defaultLocale != $event->getLocale()) {
         return;
     }
     $this->rename($node, $name);
 }
 /**
  * @param PersistEvent $event
  *
  * @throws DocumentManagerException
  */
 public function handlePersist(PersistEvent $event)
 {
     $options = $event->getOptions();
     $this->validateOptions($options);
     $document = $event->getDocument();
     $parentPath = null;
     $nodeName = null;
     if ($options['path']) {
         $parentPath = PathHelper::getParentPath($options['path']);
         $nodeName = PathHelper::getNodeName($options['path']);
     }
     if ($options['parent_path']) {
         $parentPath = $options['parent_path'];
     }
     if ($parentPath) {
         $event->setParentNode($this->resolveParent($parentPath, $options));
     }
     if ($options['node_name']) {
         if (!$event->hasParentNode()) {
             throw new DocumentManagerException(sprintf('The "node_name" option can only be used either with the "parent_path" option ' . 'or when a parent node has been established by a previous subscriber. ' . 'When persisting document: %s', DocumentHelper::getDebugTitle($document)));
         }
         $nodeName = $options['node_name'];
     }
     if (!$nodeName) {
         return;
     }
     if ($event->hasNode()) {
         $this->renameNode($event->getNode(), $nodeName);
         return;
     }
     $node = $this->strategy->createNodeForDocument($document, $event->getParentNode(), $nodeName);
     $event->setNode($node);
 }
 /**
  * It should show the title for a document which implements the TitleBehavior.
  */
 public function testDebugTitleWithTitle()
 {
     $this->titleDocument->getTitle()->willReturn('Hello');
     $title = DocumentHelper::getDebugTitle($this->titleDocument->reveal());
     $this->assertContains('Hello', $title);
 }