From a52becdfc65e85cd6c8067e71837480339867cdf Mon Sep 17 00:00:00 2001 From: pmishev Date: Mon, 23 Nov 2015 11:09:49 +0000 Subject: [PATCH] Removed MLProperty annotation in favour of multilanguage=true setting of Property annotation --- Annotation/AbstractProperty.php | 59 ------------------- Annotation/MLProperty.php | 54 ----------------- Annotation/Property.php | 83 ++++++++++++++++++++++++++- Mapping/DocumentMetadataCollector.php | 1 - Mapping/DocumentParser.php | 23 +++----- 5 files changed, 91 insertions(+), 129 deletions(-) delete mode 100644 Annotation/AbstractProperty.php delete mode 100644 Annotation/MLProperty.php diff --git a/Annotation/AbstractProperty.php b/Annotation/AbstractProperty.php deleted file mode 100644 index 715e8d3..0000000 --- a/Annotation/AbstractProperty.php +++ /dev/null @@ -1,59 +0,0 @@ -options, ['type' => $this->type]); - - return $result; - } -} diff --git a/Annotation/MLProperty.php b/Annotation/MLProperty.php deleted file mode 100644 index a1d5628..0000000 --- a/Annotation/MLProperty.php +++ /dev/null @@ -1,54 +0,0 @@ -options, ['type' => $this->type]); + + if (isset($settings['language'])) { + if (!isset($settings['indexAnalyzers'])) { + throw new \InvalidArgumentException('Available index analyzers missing'); + } + + // Replace {lang} in any analyzers with the respective language + // If no analyzer is defined for a certain language, replace {lang} with 'default' + array_walk($result, function (&$value, $key, $settings) { + if (in_array($key, ['analyzer', 'index_analyzer', 'search_analyzer']) && false !== strpos($value, self::LANGUAGE_PLACEHOLDER)) { + // Get the names of all available analyzers in the index + $indexAnalyzers = array_keys($settings['indexAnalyzers']); + + // Make sure a default analyzer is defined, even if we don't need it right now + // because, if a new language is added and we don't have an analyzer for it, ES mapping would fail + $defaultAnalyzer = str_replace(self::LANGUAGE_PLACEHOLDER, self::DEFAULT_LANG_SUFFIX, $value); + if (!in_array($defaultAnalyzer, $indexAnalyzers)) { + throw new \LogicException(sprintf('There must be a default language analyzer "%s" defined for index', $defaultAnalyzer)); + } + $value = str_replace(self::LANGUAGE_PLACEHOLDER, $settings['language'], $value); + if (!in_array($value, $indexAnalyzers)) { + $value = $defaultAnalyzer; + } + } + }, $settings); + } + return $result; + } } diff --git a/Mapping/DocumentMetadataCollector.php b/Mapping/DocumentMetadataCollector.php index a7d7ad8..a28be61 100644 --- a/Mapping/DocumentMetadataCollector.php +++ b/Mapping/DocumentMetadataCollector.php @@ -84,7 +84,6 @@ public function __construct(array $indexManagers, DocumentLocator $documentLocat */ private function isCacheFresh() { - return false; $documentDirs = $this->documentLocator->getAllDocumentDirs(); foreach ($documentDirs as $dir) { diff --git a/Mapping/DocumentParser.php b/Mapping/DocumentParser.php index 79fe113..0643769 100644 --- a/Mapping/DocumentParser.php +++ b/Mapping/DocumentParser.php @@ -4,9 +4,8 @@ use Doctrine\Common\Annotations\AnnotationRegistry; use Doctrine\Common\Annotations\Reader; -use Sineflow\ElasticsearchBundle\Annotation\AbstractProperty; use Sineflow\ElasticsearchBundle\Annotation\Document; -use Sineflow\ElasticsearchBundle\Annotation\MLProperty; +use Sineflow\ElasticsearchBundle\Annotation\Property; use Sineflow\ElasticsearchBundle\LanguageProvider\LanguageProviderInterface; /** @@ -17,7 +16,7 @@ class DocumentParser /** * @const string */ - const PROPERTY_ANNOTATION = 'Sineflow\ElasticsearchBundle\Annotation\AbstractProperty'; + const PROPERTY_ANNOTATION = 'Sineflow\ElasticsearchBundle\Annotation\Property'; /** * @const string @@ -148,7 +147,7 @@ private function getDocumentType(\ReflectionClass $documentReflection) * * @param \ReflectionProperty $property * - * @return AbstractProperty + * @return Property */ public function getPropertyAnnotationData($property) { @@ -189,13 +188,9 @@ private function getPropertiesMetadata(\ReflectionClass $documentReflection) $propertyMetadata[$propertyAnnotation->name] = [ 'propertyName' => $propertyName, 'type' => $propertyAnnotation->type, + 'multilanguage' => $propertyAnnotation->multilanguage, ]; - // If property is multilanguage - if ($propertyAnnotation instanceof MLProperty) { - $propertyMetadata[$propertyAnnotation->name]['multilanguage'] = true; - } - // If property is a (nested) object if (in_array($propertyAnnotation->type, ['object', 'nested'])) { if (!$propertyAnnotation->objectName) { @@ -313,19 +308,19 @@ private function getProperties(\ReflectionClass $documentReflection, array $inde } // If it is a multi-language property - if ($propertyAnnotation instanceof MLProperty) { + if (true === $propertyAnnotation->multilanguage) { if ($propertyAnnotation->type != 'string') { - throw new \InvalidArgumentException(sprintf('"%s" property in %s is declared as "MLProperty", so can only be of type "string"', $propertyAnnotation->name, $documentReflection->getName())); + throw new \InvalidArgumentException(sprintf('"%s" property in %s is declared as multilanguage, so can only be of type "string"', $propertyAnnotation->name, $documentReflection->getName())); } if (!$this->languageProvider) { - throw new \InvalidArgumentException('There must be a service tagged as "sfes.language_provider" in order to use MLProperty'); + throw new \InvalidArgumentException('There must be a service tagged as "sfes.language_provider" in order to use multilanguage properties'); } foreach ($this->languageProvider->getLanguages() as $language) { $mapping[$propertyAnnotation->name . $this->languageSeparator . $language] = $this->getPropertyMapping($propertyAnnotation, $language, $indexAnalyzers); } // 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] = [ + $mapping[$propertyAnnotation->name . $this->languageSeparator . Property::DEFAULT_LANG_SUFFIX] = [ 'type' => 'string', 'index' => 'not_analyzed' ]; @@ -338,7 +333,7 @@ private function getProperties(\ReflectionClass $documentReflection, array $inde return $mapping; } - private function getPropertyMapping(AbstractProperty $propertyAnnotation, $language = null, array $indexAnalyzers = []) + private function getPropertyMapping(Property $propertyAnnotation, $language = null, array $indexAnalyzers = []) { $propertyMapping = $propertyAnnotation->dump([ 'language' => $language,