/**
  * @inheritDoc
  */
 public function getRouteCollectionForRequest(Request $request)
 {
     if (!$this->manager) {
         throw new \Exception('Manager must be set to execute query to the elasticsearch');
     }
     $routeCollection = new RouteCollection();
     $requestPath = $request->getPathInfo();
     $search = new Search();
     $search->addQuery(new MatchQuery('url', $requestPath));
     $results = $this->manager->execute(array_keys($this->routeMap), $search, Result::RESULTS_OBJECT);
     try {
         foreach ($results as $document) {
             $type = $this->collector->getDocumentType(get_class($document));
             if (array_key_exists($type, $this->routeMap)) {
                 $route = new Route($document->url, ['_controller' => $this->routeMap[$type], 'document' => $document, 'type' => $type]);
                 $routeCollection->add('ongr_route_' . $route->getDefault('type'), $route);
             } else {
                 throw new RouteNotFoundException(sprintf('Route for type %s% cannot be generated.', $type));
             }
         }
     } catch (\Exception $e) {
         throw new RouteNotFoundException('Document is not correct or route cannot be generated.');
     }
     return $routeCollection;
 }
 /**
  * {@inheritdoc}
  */
 public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)
 {
     try {
         if ($name instanceof SeoAwareInterface) {
             $documentUrl = $name->getUrl();
         } else {
             throw new RouteNotFoundException();
         }
         $type = $this->collector->getDocumentType(get_class($name));
         $route = new Route($documentUrl, ['_controller' => $this->routeMap[$type], 'document' => $name, 'type' => $type]);
         // the Route has a cache of its own and is not recompiled as long as it does not get modified
         $compiledRoute = $route->compile();
         $hostTokens = $compiledRoute->getHostTokens();
         return $this->doGenerate($compiledRoute->getVariables(), $route->getDefaults(), $route->getRequirements(), $compiledRoute->getTokens(), $parameters, 'ongr_route', $referenceType, $hostTokens);
     } catch (\Exception $e) {
         throw new RouteNotFoundException('Document is not correct or route cannot be generated.');
     }
 }
Esempio n. 3
0
 /**
  * {@inheritdoc}
  */
 public function generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)
 {
     try {
         $document = $parameters['document'];
         if (is_object($document)) {
             $documentUrl = $document->url;
         } else {
             $documentUrl = $document['url'];
         }
         $type = $this->collector->getDocumentType(get_class($document));
         $route = new Route($documentUrl, ['_controller' => $this->routeMap[$type], 'document' => $document, 'type' => $type]);
         // the Route has a cache of its own and is not recompiled as long as it does not get modified
         $compiledRoute = $route->compile();
         $hostTokens = $compiledRoute->getHostTokens();
         $debug_message = $this->getRouteDebugMessage($name);
         return $this->doGenerate($compiledRoute->getVariables(), $route->getDefaults(), $route->getRequirements(), $compiledRoute->getTokens(), $parameters, $debug_message, $referenceType, $hostTokens);
     } catch (\Exception $e) {
         throw new RouteNotFoundException('Document is not correct or route cannot be generated.');
     }
 }
 /**
  * Test for getDocumentType() in case invalid class given.
  *
  * @expectedException \Exception
  * @expectedExceptionMessage Mapping for class "\StdClass" was not found
  */
 public function testGetDocumentTypeException()
 {
     $this->metadataCollector->getDocumentType('\\StdClass');
 }