/**
  * Return a token value for the given configuration and
  * document.
  *
  * @param UriContext $uriContext
  * @param array $options
  * @return string
  */
 public function provideValue(UriContext $uriContext, $options)
 {
     $path = array();
     $subject = $uriContext->getSubjectObject();
     $this->traversePath($subject, $path);
     if (empty($path)) {
         return '';
     }
     $path = array_map(function ($item) {
         return $this->slugifier->slugify($item);
     }, $path);
     $path = array_reverse($path);
     return implode('/', $path);
 }
 /**
  * @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);
 }
示例#3
0
 /**
  * {@inheritdoc}
  */
 public function generate($entity, array $options)
 {
     $routeSchema = $options['route_schema'];
     $tokens = [];
     preg_match_all('/{(.*?)}/', $routeSchema, $matches);
     $tokenNames = $matches[1];
     foreach ($tokenNames as $index => $name) {
         $tokenName = '{' . $name . '}';
         $tokenValue = $this->tokenProvider->provide($entity, $name);
         $tokens[$tokenName] = $this->slugifier->slugify($tokenValue);
     }
     $path = strtr($routeSchema, $tokens);
     if (0 !== strpos($path, '/')) {
         throw new \InvalidArgumentException(sprintf('Generated path "%s" for object "%s" has to start with a slash', $path, get_class($entity)));
     }
     return $path;
 }