/**
  * @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;
 }
 /**
  * Test if function throws exception if ES type names are not unique.
  */
 public function testGetBundleMappingWithDocumentSubdirectory()
 {
     $mapping = $this->metadataCollector->getMappings(['AcmeBazBundle']);
     $this->assertArrayHasKey('product', $mapping);
     $this->assertNotEmpty($mapping['product']['objects']);
     $this->assertEquals('ONGR\\ElasticsearchBundle\\Tests\\app\\fixture\\Acme\\BazBundle\\Document\\Object\\CategoryObject', $mapping['product']['objects'][0]);
 }
Esempio n. 3
0
 /**
  * Returns aliases for certain document.
  *
  * @param DocumentInterface $document
  *
  * @return array
  *
  * @throws \DomainException
  */
 private function getAlias($document)
 {
     $class = get_class($document);
     $documentMapping = $this->metadataCollector->getDocumentMapping($document);
     if (is_array($documentMapping) && isset($documentMapping['aliases'])) {
         return $documentMapping['aliases'];
     }
     throw new \DomainException("Aliases could not be found for {$class} document.");
 }
 /**
  * {@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. 5
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.');
     }
 }
 /**
  * Factory function to create a manager instance.
  *
  * @param string $managerName   Manager name.
  * @param array  $connection    Connection configuration.
  * @param array  $analysis      Analyzers, filters and tokenizers config.
  * @param array  $managerConfig Manager configuration.
  *
  * @return Manager
  */
 public function createManager($managerName, $connection, $analysis, $managerConfig)
 {
     foreach (array_keys($analysis) as $analyzerType) {
         foreach ($connection['analysis'][$analyzerType] as $name) {
             $connection['settings']['analysis'][$analyzerType][$name] = $analysis[$analyzerType][$name];
         }
     }
     unset($connection['analysis']);
     $mappings = $this->metadataCollector->getClientMapping($managerConfig);
     $client = ClientBuilder::create();
     $client->setHosts($connection['hosts']);
     if ($this->tracer && $managerConfig['profiler']) {
         $client->setTracer($this->tracer);
     }
     if ($this->logger && $managerConfig['logger']['enabled']) {
         $client->setLogger($this->logger);
     }
     $indexSettings = ['index' => $connection['index_name'], 'body' => ['settings' => $connection['settings'], 'mappings' => $mappings]];
     $manager = new Manager($managerName, $managerConfig, $client->build(), $indexSettings, $this->metadataCollector, $this->converter);
     return $manager;
 }
 /**
  * Test mapping getter when there are no bundles loaded from parser.
  *
  * @expectedException \LogicException
  * @expectedExceptionMessage Bundle 'acme' does not exist.
  */
 public function testGetBundleMappingWithNoBundlesLoaded()
 {
     $this->metadataCollector->getBundleMapping('acme');
 }
 /**
  * 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');
 }
 /**
  * Test bundle mapping parser when requesting non string bundle name.
  *
  * @expectedException \LogicException
  * @expectedExceptionMessage getBundleMapping() in the Metadata collector expects a string argument only!
  */
 public function testGetBundleMappingWithNotStringName()
 {
     $this->metadataCollector->getBundleMapping(1000);
 }
 /**
  * Test for getClientMapping() in case no mapping exists.
  */
 public function testGetClientMappingNull()
 {
     $this->assertNull($this->metadataCollector->getClientMapping([]));
 }
Esempio n. 11
0
 /**
  * Returns aliases for certain document.
  *
  * @param object $document
  *
  * @return array
  */
 private function getAlias($document)
 {
     $class = get_class($document);
     $documentMapping = $this->metadataCollector->getMapping($class);
     return $documentMapping['aliases'];
 }