diff --git a/DependencyInjection/SineflowElasticsearchExtension.php b/DependencyInjection/SineflowElasticsearchExtension.php index eaa9294..f98a011 100644 --- a/DependencyInjection/SineflowElasticsearchExtension.php +++ b/DependencyInjection/SineflowElasticsearchExtension.php @@ -35,50 +35,12 @@ public function load(array $configs, ContainerBuilder $container) $configuration = $this->getConfiguration($configs, $container); $config = $this->processConfiguration($configuration, $configs); - $container->setParameter('sfes.document_dir', $config['document_dir']); - $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); $loader->load('services.yml'); + $container->setParameter('sfes.document_dir', $config['document_dir']); $container->setParameter('sfes.connections', $config['connections']); $container->setParameter('sfes.indices', $config['indices']); - - $this->addDocumentsResource($config, $container); - } - - /** - * Adds document directory resource. - * This is done, so if any entity definition is changed, the cache can be rebuilt - * - * @param array $config - * @param ContainerBuilder $container - */ - private function addDocumentsResource(array $config, ContainerBuilder $container) - { - $watchedBundles = []; - foreach ($config['indices'] as $index) { - foreach ($index['types'] as $typeEntity) { - // Get the bundle name from the entity class short syntax (e.g. AppBundle:Product) - $bundleName = substr($typeEntity, 0, strpos($typeEntity, ':')); - $watchedBundles[$bundleName] = true; - } - } - // Get the bundles' classes from the container registered bundles - $watchedBundles = array_intersect_key( - $container->getParameter('kernel.bundles'), - $watchedBundles - ); - - // TODO: once the metadata is no longer gathered during container compilation, - // figure out another way of invalidating the cache when a document in the watched bundles' document dirs is changed - // because it won't be the container cache that needs invalidation, but the metadata cache, which I guess would be separate - - - foreach ($watchedBundles as $name => $class) { - $bundle = new \ReflectionClass($class); - $dir = dirname($bundle->getFileName()) . DIRECTORY_SEPARATOR . $config['document_dir']; - $container->addResource(new DirectoryResource($dir)); - } } } diff --git a/Manager/IndexManager.php b/Manager/IndexManager.php index d1d31c6..177d5b7 100644 --- a/Manager/IndexManager.php +++ b/Manager/IndexManager.php @@ -518,15 +518,33 @@ public function rebuildIndex($deleteOld = false) */ public function reindex($documentClass, $id) { + $documentMetadata = $this->metadataCollector->getDocumentMetadata($documentClass); + $dataProvider = $this->getDataProvider($documentClass); $document = $dataProvider->getDocument($id); - // TODO: verify that the id of the returned document matches $id - if (is_array($document)) { - $documentMetadata = $this->metadataCollector->getDocumentMetadata($documentClass); - $this->persistRaw($documentMetadata->getType(), $document); - } else { - $this->persist($document); + + switch (true) { + case $document instanceof DocumentInterface: + if (get_class($document) !== $documentMetadata->getClassName()) { + throw new \RuntimeException(sprintf('Document must be "%s", but "%s" was returned from data provider', $documentMetadata->getClassName(), get_class($document))); + } + $this->persist($document); + break; + + case is_array($document): + if (!isset($document['_id'])) { + throw new \RuntimeException(sprintf('The returned document array must include an "_id" field: (%s)', serialize($document))); + } + if ($document['_id'] !== $id) { + throw new \RuntimeException(sprintf('The document id must be "%s", but "%s" was returned from data provider', $id, $document['_id'])); + } + $this->persistRaw($documentMetadata->getType(), $document); + break; + + default: + throw new \RuntimeException('Document must be either a DocumentInterface instance or an array with raw data'); } + $this->commit(); } diff --git a/Mapping/DocumentParser.php b/Mapping/DocumentParser.php index cdd4cef..f66b683 100644 --- a/Mapping/DocumentParser.php +++ b/Mapping/DocumentParser.php @@ -327,7 +327,7 @@ private function getProperties(\ReflectionClass $documentReflection, array $inde foreach ($this->languageProvider->getLanguages() as $language) { $mapping[$propertyAnnotation->name . $this->languageSeparator . $language] = $this->getPropertyMapping($propertyAnnotation, $language, $indexAnalyzers); } - // TODO: This is a temporary hardcode. The application should decide whether it wants to use a default field at all and set its mapping on a global base (or per property?) + // TODO: The application should decide whether it wants to use a default field at all and set its mapping on a global base (or per property?) // The custom mapping from the application should be set here, using perhaps some kind of decorator $mapping[$propertyAnnotation->name . $this->languageSeparator . MLProperty::DEFAULT_LANG_SUFFIX] = [ 'type' => 'string',