Пример #1
0
 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 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->setParentDocument($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->setParentDocument($document);
     $headRoute->setAutoRouteTag($autoRouteTag);
     $headRoute->setType(AutoRouteInterface::TYPE_PRIMARY);
     foreach ($uriContext->getDefaults() as $key => $value) {
         $headRoute->setDefault($key, $value);
     }
     return $headRoute;
 }