From 13fc978cb58c0850add395b23e956d79aab7af56 Mon Sep 17 00:00:00 2001 From: Martynas Sudintas Date: Fri, 23 Jan 2015 16:48:43 +0200 Subject: [PATCH 1/2] repositories for documents without fields are now created --- DependencyInjection/Compiler/MappingPass.php | 25 ++++++++++++++++-- Mapping/MetadataCollector.php | 16 +++++++----- .../ElasticsearchExtensionTest.php | 8 ++++++ .../Compiler/MappingPassTest.php | 15 ++++++++++- .../fixture/Acme/TestBundle/Document/Bar.php | 26 +++++++++++++++++++ 5 files changed, 80 insertions(+), 10 deletions(-) create mode 100644 Tests/app/fixture/Acme/TestBundle/Document/Bar.php diff --git a/DependencyInjection/Compiler/MappingPass.php b/DependencyInjection/Compiler/MappingPass.php index e6b54fc3..fb2ebfbf 100644 --- a/DependencyInjection/Compiler/MappingPass.php +++ b/DependencyInjection/Compiler/MappingPass.php @@ -26,6 +26,11 @@ */ class MappingPass implements CompilerPassInterface { + /** + * @var MetadataCollector + */ + private $metadataCollector; + /** * {@inheritdoc} */ @@ -92,7 +97,7 @@ private function getBundlesMetadata(ContainerBuilder $container, $settings) $out = []; /** @var MetadataCollector $collector */ - $collector = $container->get('es.metadata_collector'); + $collector = $this->getMetadataCollector($container); foreach ($settings['mappings'] as $bundle) { foreach ($collector->getBundleMapping($bundle) as $repository => $metadata) { $metadataDefinition = new Definition('ONGR\ElasticsearchBundle\Mapping\ClassMetadata'); @@ -189,7 +194,7 @@ private function getIndexParams(array $connection, array $manager, ContainerBuil $mappings = []; /** @var MetadataCollector $metadataCollector */ - $metadataCollector = $container->get('es.metadata_collector'); + $metadataCollector = $this->getMetadataCollector($container); $paths = []; if (!empty($manager['mappings'])) { @@ -246,4 +251,20 @@ private function setWarmers($connectionDefinition, $connection, ContainerBuilder return $warmers; } + + /** + * Returns metadata collector. + * + * @param ContainerBuilder $containerBuilder + * + * @return MetadataCollector + */ + private function getMetadataCollector(ContainerBuilder $containerBuilder) + { + if (!$this->metadataCollector) { + $this->metadataCollector = $containerBuilder->get('es.metadata_collector'); + } + + return $this->metadataCollector; + } } diff --git a/Mapping/MetadataCollector.php b/Mapping/MetadataCollector.php index aa47176b..249b196e 100644 --- a/Mapping/MetadataCollector.php +++ b/Mapping/MetadataCollector.php @@ -80,12 +80,14 @@ public function getMapping($bundle, $force = false) $mappings = []; foreach ($this->getBundleMapping($bundle) as $type => $mapping) { - $mappings[$type] = array_filter( - array_merge( - ['properties' => $mapping['properties']], - $mapping['fields'] - ) - ); + if (!empty($mapping['properties'])) { + $mappings[$type] = array_filter( + array_merge( + ['properties' => $mapping['properties']], + $mapping['fields'] + ) + ); + } } $this->documents[$bundle] = $mappings; @@ -142,7 +144,7 @@ public function getBundleMapping($bundle) ); $documentMapping = $this->getDocumentReflectionMapping($documentReflection); - if ($documentMapping !== null && !empty(reset($documentMapping)['properties'])) { + if ($documentMapping !== null) { $mappings = array_replace_recursive($mappings, $documentMapping); } } diff --git a/Tests/Functional/DependencyInjection/ElasticsearchExtensionTest.php b/Tests/Functional/DependencyInjection/ElasticsearchExtensionTest.php index cca07feb..73454e4d 100644 --- a/Tests/Functional/DependencyInjection/ElasticsearchExtensionTest.php +++ b/Tests/Functional/DependencyInjection/ElasticsearchExtensionTest.php @@ -33,6 +33,14 @@ public function getTestContainerData() 'es.manager.bar', 'ONGR\ElasticsearchBundle\ORM\Manager', ], + [ + 'es.manager.default.product', + 'ONGR\ElasticsearchBundle\ORM\Repository', + ], + [ + 'es.manager.default.bar', + 'ONGR\ElasticsearchBundle\ORM\Repository', + ], ]; } diff --git a/Tests/Unit/DependencyInjection/Compiler/MappingPassTest.php b/Tests/Unit/DependencyInjection/Compiler/MappingPassTest.php index 9ca33267..2e30526e 100644 --- a/Tests/Unit/DependencyInjection/Compiler/MappingPassTest.php +++ b/Tests/Unit/DependencyInjection/Compiler/MappingPassTest.php @@ -34,7 +34,20 @@ public function setUp() ->disableOriginalConstructor() ->getMock(); - $metadataCollectorMock->expects($this->any())->method('getMapping')->willReturn(['connection' => 'bar']); + $metadataCollectorMock->expects($this->any())->method('getMapping')->willReturn( + [ + 'AcmeTestBundle:Bar' => [ + 'properties' => [], + ], + 'AcmeTestBundle:Product' => [ + 'properties' => [ + 'name' => [ + 'type' => 'string', + ], + ], + ], + ] + ); $metadataCollectorMock->expects($this->any())->method('getBundleMapping')->willReturn([$bundleMappingData]); $metadataCollectorMock->expects($this->any())->method('getProxyPaths')->willReturn([]); diff --git a/Tests/app/fixture/Acme/TestBundle/Document/Bar.php b/Tests/app/fixture/Acme/TestBundle/Document/Bar.php new file mode 100644 index 00000000..4c143157 --- /dev/null +++ b/Tests/app/fixture/Acme/TestBundle/Document/Bar.php @@ -0,0 +1,26 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ElasticsearchBundle\Tests\app\fixture\Acme\TestBundle\Document; + +use ONGR\ElasticsearchBundle\Annotation as ES; +use ONGR\ElasticsearchBundle\Document\DocumentInterface; +use ONGR\ElasticsearchBundle\Document\DocumentTrait; + +/** + * Empty test document. + * + * @ES\Document(type="bar") + */ +class Bar implements DocumentInterface +{ + use DocumentTrait; +} From afb3f5bb9b68047bd17926a6c8ebfe071306b0e8 Mon Sep 17 00:00:00 2001 From: Martynas Sudintas Date: Tue, 27 Jan 2015 10:32:37 +0200 Subject: [PATCH 2/2] Removed MetadataCollector getter from MappingPass --- DependencyInjection/Compiler/MappingPass.php | 25 ++------------------ 1 file changed, 2 insertions(+), 23 deletions(-) diff --git a/DependencyInjection/Compiler/MappingPass.php b/DependencyInjection/Compiler/MappingPass.php index fb2ebfbf..e6b54fc3 100644 --- a/DependencyInjection/Compiler/MappingPass.php +++ b/DependencyInjection/Compiler/MappingPass.php @@ -26,11 +26,6 @@ */ class MappingPass implements CompilerPassInterface { - /** - * @var MetadataCollector - */ - private $metadataCollector; - /** * {@inheritdoc} */ @@ -97,7 +92,7 @@ private function getBundlesMetadata(ContainerBuilder $container, $settings) $out = []; /** @var MetadataCollector $collector */ - $collector = $this->getMetadataCollector($container); + $collector = $container->get('es.metadata_collector'); foreach ($settings['mappings'] as $bundle) { foreach ($collector->getBundleMapping($bundle) as $repository => $metadata) { $metadataDefinition = new Definition('ONGR\ElasticsearchBundle\Mapping\ClassMetadata'); @@ -194,7 +189,7 @@ private function getIndexParams(array $connection, array $manager, ContainerBuil $mappings = []; /** @var MetadataCollector $metadataCollector */ - $metadataCollector = $this->getMetadataCollector($container); + $metadataCollector = $container->get('es.metadata_collector'); $paths = []; if (!empty($manager['mappings'])) { @@ -251,20 +246,4 @@ private function setWarmers($connectionDefinition, $connection, ContainerBuilder return $warmers; } - - /** - * Returns metadata collector. - * - * @param ContainerBuilder $containerBuilder - * - * @return MetadataCollector - */ - private function getMetadataCollector(ContainerBuilder $containerBuilder) - { - if (!$this->metadataCollector) { - $this->metadataCollector = $containerBuilder->get('es.metadata_collector'); - } - - return $this->metadataCollector; - } }