/** * {@inheritdoc} */ public function provideValue(UriContext $uriContext, $options) { $object = $uriContext->getSubjectObject(); $method = $options['method']; $this->checkMethodExists($object, $method); return $this->normalizeValue($object->{$method}(), $uriContext, $options); }
/** * {@inheritdoc} */ protected function normalizeValue($date, UriContext $uriContext, $options) { if (!$date instanceof \DateTime) { throw new \RuntimeException(sprintf('Method %s:%s must return an instance of DateTime.', get_class($uriContext->getSubjectObject()), $options['method'])); } return $date->format($options['date_format']); }
/** * {@inheritdoc} */ public function resolveConflict(UriContext $uriContext) { $conflictResolverConfig = $uriContext->getConflictResolverConfig(); $conflictResolver = $this->serviceRegistry->getConflictResolver($conflictResolverConfig['name'], $conflictResolverConfig['options']); $uri = $conflictResolver->resolveConflict($uriContext); return $uri; }
/** * {@inheritDoc} */ public function resolveConflict(UriContext $uriContext) { $realClassName = $this->driver->getRealClassName(get_class($uriContext->getSubjectObject())); $metadata = $this->metadataFactory->getMetadataForClass($realClassName); $conflictResolverConfig = $metadata->getConflictResolver(); $conflictResolver = $this->serviceRegistry->getConflictResolver($conflictResolverConfig['name'], $conflictResolverConfig['options']); $uri = $conflictResolver->resolveConflict($uriContext); return $uri; }
/** * {@inheritdoc} */ public function resolveConflict(UriContext $uriContext) { $this->inc = 0; $uri = $uriContext->getUri(); $candidateUri = $this->incrementUri($uri); while ($route = $this->adapter->findRouteForUri($candidateUri, $uriContext)) { $candidateUri = $this->incrementUri($uri); } return $candidateUri; }
/** * {@inheritDoc} */ public function provideValue(UriContext $uriContext, $options) { $object = $uriContext->getSubjectObject(); $method = $options['method']; $this->checkMethodExists($object, $method); $value = $object->{$method}(); if ($options['slugify']) { $value = $this->slugifier->slugify($value); } return $value; }
/** * {@inheritDoc} */ public function provideValue(UriContext $uriContext, $options) { $object = $uriContext->getSubjectObject(); $method = $options['method']; $this->checkMethodExists($object, $method); $date = $object->{$method}(); if (!$date instanceof \DateTime) { throw new \RuntimeException(sprintf('Method %s:%s must return an instance of DateTime.', get_class($object), $method)); } $string = $date->format($options['date_format']); return $string; }
/** * 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); }
public function testGetSet() { $uriContext = new UriContext($this->subjectObject, 'fr'); // locales $this->assertEquals('fr', $uriContext->getLocale()); /// uri $this->assertEquals(null, $uriContext->getUri()); $uriContext->setUri('/foo/bar'); $this->assertEquals('/foo/bar', $uriContext->getUri()); // subject object $this->assertEquals($this->subjectObject, $uriContext->getSubjectObject()); // auto route $uriContext->setAutoRoute($this->autoRoute); $this->assertEquals($this->autoRoute, $uriContext->getAutoRoute()); }
/** * {@inheritDoc} */ public function provideValue(UriContext $uriContext, $options) { return $uriContext->getLocale(); }
/** * {@inheritDoc} */ public function resolveConflict(UriContext $uriContext) { $uri = $uriContext->getUri(); throw new Exception\ExistingUriException(sprintf('There already exists an auto route for URL "%s" and the system is configured ' . 'to throw this exception in this case. Alternatively you can choose to use a ' . 'different strategy, for example, auto incrementation. Please refer to the ' . 'documentation for more information.', $uri)); }
/** * Handle the case where the generated path already exists. * Either if it does not reference the same content then we * have a conflict which needs to be resolved. * * @param Route $route * @param UriContext $uriContext */ private function handleExistingRoute($existingRoute, $uriContext) { $isSameContent = $this->adapter->compareAutoRouteContent($existingRoute, $uriContext->getSubjectObject()); if ($isSameContent) { $autoRoute = $existingRoute; $autoRoute->setType(AutoRouteInterface::TYPE_PRIMARY); return $autoRoute; } $uri = $this->uriGenerator->resolveConflict($uriContext); $uriContext->setUri($uri); return; }
public function testGetSet() { $uriContext = new UriContext($this->subjectObject, '/uri/', array('default1' => 'value1'), array('token'), array('conflict'), 'fr'); // locales $this->assertEquals('fr', $uriContext->getLocale()); /// uri $this->assertEquals(null, $uriContext->getUri()); $uriContext->setUri('/foo/bar'); $this->assertEquals('/foo/bar', $uriContext->getUri()); // subject object $this->assertEquals($this->subjectObject, $uriContext->getSubjectObject()); // auto route $uriContext->setAutoRoute($this->autoRoute); $this->assertEquals($this->autoRoute, $uriContext->getAutoRoute()); // the translated subject should be initially set as the original subject $this->assertSame($this->subjectObject, $uriContext->getTranslatedSubjectObject()); $transSubject = new \stdClass(); $uriContext->setTranslatedSubjectObject($transSubject); $this->assertSame($transSubject, $uriContext->getTranslatedSubjectObject()); // uri schema $this->assertEquals('/uri/', $uriContext->getUriSchema()); // token provider configs $this->assertEquals(array('token'), $uriContext->getTokenProviderConfigs()); // conflict resolver configs $this->assertEquals(array('conflict'), $uriContext->getConflictResolverConfig()); // defaults $this->assertEquals(array('default1' => 'value1'), $uriContext->getDefaults()); }
/** * {@inheritdoc} */ public function generateAutoRouteTag(UriContext $uriContext) { return $uriContext->getLocale() ?: self::TAG_NO_MULTILANG; }
/** * {@inheritdoc} */ public function createAutoRoute(UriContext $uriContext, $contentDocument, $autoRouteTag) { $basePath = $this->baseRoutePath; $document = $parentDocument = $this->dm->find(null, $basePath); if (null === $parentDocument) { throw new \RuntimeException(sprintf('The "route_basepath" configuration points to a non-existant path "%s".', $basePath)); } $segments = preg_split('#/#', $uriContext->getUri(), null, PREG_SPLIT_NO_EMPTY); $headName = array_pop($segments); foreach ($segments as $segment) { $basePath .= '/' . $segment; $document = $this->dm->find(null, $basePath); if (null === $document) { $document = new Generic(); $document->setParent($parentDocument); $document->setNodeName($segment); $this->dm->persist($document); } $parentDocument = $document; } $path = $basePath . '/' . $headName; $existingDocument = $this->dm->find(null, $path); if ($existingDocument) { if ($existingDocument instanceof Generic) { return $this->migrateGenericToAutoRoute($existingDocument, $contentDocument, $autoRouteTag, AutoRouteInterface::TYPE_PRIMARY); } throw new \RuntimeException(sprintf('Encountered existing PHPCR-ODM document at path "%s" of class "%s", the route tree should ' . 'contain only instances of AutoRouteInterface.', $path, get_class($existingDocument))); } $headRoute = new $this->autoRouteFqcn(); $headRoute->setContent($contentDocument); $headRoute->setName($headName); $headRoute->setParent($document); $headRoute->setAutoRouteTag($autoRouteTag); $headRoute->setType(AutoRouteInterface::TYPE_PRIMARY); return $headRoute; }