From 77357476d316745e6f3c94188e454ed4642b49f5 Mon Sep 17 00:00:00 2001 From: Martynas Sudintas Date: Mon, 23 Feb 2015 14:36:07 +0200 Subject: [PATCH] configuration mappings are now able to accept separate documents --- Command/TypeUpdateCommand.php | 2 +- DSL/Filter/NestedFilter.php | 5 --- DependencyInjection/Compiler/MappingPass.php | 7 ++-- DependencyInjection/Configuration.php | 11 +---- Mapping/MetadataCollector.php | 42 +++++++++++++------ .../ElasticsearchExtensionTest.php | 9 +++- .../Compiler/MappingPassTest.php | 4 +- .../DependencyInjection/ConfigurationTest.php | 23 ---------- Tests/app/AppKernel.php | 1 + Tests/app/config/config_test.yml | 1 + .../fixture/Acme/FooBundle/AcmeFooBundle.php | 21 ++++++++++ .../fixture/Acme/FooBundle/Document/Media.php | 30 +++++++++++++ 12 files changed, 98 insertions(+), 58 deletions(-) create mode 100644 Tests/app/fixture/Acme/FooBundle/AcmeFooBundle.php create mode 100644 Tests/app/fixture/Acme/FooBundle/Document/Media.php diff --git a/Command/TypeUpdateCommand.php b/Command/TypeUpdateCommand.php index 9cee426a..d3e1ec35 100644 --- a/Command/TypeUpdateCommand.php +++ b/Command/TypeUpdateCommand.php @@ -99,7 +99,7 @@ protected function clearMappingCache($managerName, $type = '') foreach ($setting['mappings'] as $bundle) { $mappings = array_replace_recursive( $mappings, - $collector->getMapping($bundle, true) + $collector->getClientMapping($bundle, true) ); } } diff --git a/DSL/Filter/NestedFilter.php b/DSL/Filter/NestedFilter.php index bba9a974..397f1c1b 100644 --- a/DSL/Filter/NestedFilter.php +++ b/DSL/Filter/NestedFilter.php @@ -31,11 +31,6 @@ class NestedFilter implements BuilderInterface */ private $query; - /** - * @var array - */ - private $parameters; - /** * @param string $path * @param BuilderInterface $query diff --git a/DependencyInjection/Compiler/MappingPass.php b/DependencyInjection/Compiler/MappingPass.php index 0fbf1f76..a4c1835c 100644 --- a/DependencyInjection/Compiler/MappingPass.php +++ b/DependencyInjection/Compiler/MappingPass.php @@ -102,11 +102,12 @@ private function getBundlesMetadata(ContainerBuilder $container, $settings) /** @var MetadataCollector $collector */ $collector = $container->get('es.metadata_collector'); foreach ($settings['mappings'] as $bundle) { - foreach ($collector->getBundleMapping($bundle) as $repository => $metadata) { + foreach ($collector->getMapping($bundle) as $repository => $metadata) { $metadataDefinition = new Definition('ONGR\ElasticsearchBundle\Mapping\ClassMetadata'); $metadataDefinition->addArgument([$repository => $metadata]); - $out[$bundle . ':' . $metadata['class']] = $metadataDefinition; + $out[strpos($bundle, ':') === false ? $bundle + . ':' . $metadata['class'] : $bundle] = $metadataDefinition; } } @@ -209,7 +210,7 @@ private function getIndexParams(array $connection, array $manager, ContainerBuil foreach ($bundles as $bundle) { $mappings = array_replace_recursive( $mappings, - $metadataCollector->getMapping($bundle) + $metadataCollector->getClientMapping($bundle) ); $paths = array_replace($paths, $metadataCollector->getProxyPaths()); } diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 626d8144..5a32655b 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -137,16 +137,7 @@ private function getManagersNode() ->end() ->arrayNode('mappings') ->info('Maps manager to bundles. f.e. AcmeDemoBundle') - ->prototype('scalar') - ->validate() - ->ifTrue( - function ($v) { - return substr(strtolower($v), -strlen('bundle')) !== 'bundle'; - } - ) - ->thenInvalid('%s is not a bundle.') - ->end() - ->end() + ->prototype('scalar')->end() ->end() ->end() ->end(); diff --git a/Mapping/MetadataCollector.php b/Mapping/MetadataCollector.php index 249b196e..2cfb857a 100644 --- a/Mapping/MetadataCollector.php +++ b/Mapping/MetadataCollector.php @@ -67,19 +67,19 @@ public function __construct($finder, $parser, $proxyLoader) /** * Retrieves mapping from local cache otherwise runs through bundle files. * - * @param string $bundle Bundle name to retrieve mappings from. - * @param bool $force Forces to rescan bundles and skip local cache. + * @param string $namespace Bundle name to retrieve mappings from. + * @param bool $force Forces to rescan bundles and skip local cache. * * @return array */ - public function getMapping($bundle, $force = false) + public function getClientMapping($namespace, $force = false) { - if (!$force && array_key_exists($bundle, $this->documents)) { - return $this->documents[$bundle]; + if (!$force && array_key_exists($namespace, $this->documents)) { + return $this->documents[$namespace]; } $mappings = []; - foreach ($this->getBundleMapping($bundle) as $type => $mapping) { + foreach ($this->getMapping($namespace) as $type => $mapping) { if (!empty($mapping['properties'])) { $mappings[$type] = array_filter( array_merge( @@ -89,9 +89,10 @@ public function getMapping($bundle, $force = false) ); } } - $this->documents[$bundle] = $mappings; - return $this->documents[$bundle]; + $this->documents[$namespace] = $mappings; + + return $this->documents[$namespace]; } /** @@ -99,7 +100,7 @@ public function getMapping($bundle, $force = false) * * @param string $namespace Document namespace. * - * @return ClassMetadata|null + * @return array|null */ public function getMappingByNamespace($namespace) { @@ -111,7 +112,7 @@ public function getMappingByNamespace($namespace) * * @param DocumentInterface $document * - * @return ClassMetadata|null + * @return array|null */ public function getDocumentMapping(DocumentInterface $document) { @@ -119,13 +120,28 @@ public function getDocumentMapping(DocumentInterface $document) } /** - * Searches for documents in bundle and tries to read them. + * Returns mapping with metadata. * - * Returns empty array on containing zero documents + * @param string $namespace Bundle or document namespace. + * + * @return array + */ + public function getMapping($namespace) + { + if (strpos($namespace, ':') === false) { + return $this->getBundleMapping($namespace); + } + $mapping = $this->getMappingByNamespace($namespace); + + return $mapping === null ? [] : $mapping; + } + + /** + * Searches for documents in bundle and tries to read them. * * @param string $bundle * - * @return ClassMetadata[] + * @return array Empty array on containing zero documents. */ public function getBundleMapping($bundle) { diff --git a/Tests/Functional/DependencyInjection/ElasticsearchExtensionTest.php b/Tests/Functional/DependencyInjection/ElasticsearchExtensionTest.php index 44dd3da4..d04f4c6e 100644 --- a/Tests/Functional/DependencyInjection/ElasticsearchExtensionTest.php +++ b/Tests/Functional/DependencyInjection/ElasticsearchExtensionTest.php @@ -52,6 +52,10 @@ public function getTestContainerData() 'es.manager.default.colordocument', 'ONGR\ElasticsearchBundle\ORM\Repository', ], + [ + 'es.manager.default.media', + 'ONGR\ElasticsearchBundle\ORM\Repository', + ], [ 'es.metadata_collector', 'ONGR\ElasticsearchBundle\Mapping\MetadataCollector', @@ -102,7 +106,10 @@ public function testContainerDefaultParams() 'default' => [ 'connection' => 'default', 'debug' => true, - 'mappings' => ['AcmeTestBundle'], + 'mappings' => [ + 'AcmeTestBundle', + 'AcmeFooBundle:Media', + ], ], 'bar' => [ 'connection' => 'bar', diff --git a/Tests/Unit/DependencyInjection/Compiler/MappingPassTest.php b/Tests/Unit/DependencyInjection/Compiler/MappingPassTest.php index 5491e87f..6698831f 100644 --- a/Tests/Unit/DependencyInjection/Compiler/MappingPassTest.php +++ b/Tests/Unit/DependencyInjection/Compiler/MappingPassTest.php @@ -34,7 +34,7 @@ public function setUp() ->disableOriginalConstructor() ->getMock(); - $metadataCollectorMock->expects($this->any())->method('getMapping')->willReturn( + $metadataCollectorMock->expects($this->any())->method('getClientMapping')->willReturn( [ 'AcmeTestBundle:Bar' => [ 'properties' => [], @@ -48,7 +48,7 @@ public function setUp() ], ] ); - $metadataCollectorMock->expects($this->any())->method('getBundleMapping')->willReturn([$bundleMappingData]); + $metadataCollectorMock->expects($this->any())->method('getMapping')->willReturn([$bundleMappingData]); $metadataCollectorMock->expects($this->any())->method('getProxyPaths')->willReturn([]); $this->container = $this->getMockBuilder('\Symfony\Component\DependencyInjection\ContainerBuilder') diff --git a/Tests/Unit/DependencyInjection/ConfigurationTest.php b/Tests/Unit/DependencyInjection/ConfigurationTest.php index e5ff87ec..a582c5eb 100644 --- a/Tests/Unit/DependencyInjection/ConfigurationTest.php +++ b/Tests/Unit/DependencyInjection/ConfigurationTest.php @@ -126,29 +126,6 @@ public function getTestConfigurationData() 'Host must be configured under hosts configuration tree.', ]; - // Case #4: invalid bundle name. - $out[] = [ - [ - 'connections' => [ - 'acme' => [ - 'hosts' => [ - ['host' => '127.0.0.1'], - ], - 'index_name' => 'acme', - ], - ], - 'managers' => [ - 'acme' => [ - 'connection' => 'acme', - 'mappings' => ['foo'], - ], - ], - ], - $expectedConfiguration, - true, - '"foo" is not a bundle.', - ]; - // Case #4: using auth. $out[] = [ [ diff --git a/Tests/app/AppKernel.php b/Tests/app/AppKernel.php index f8257130..ec1bbf88 100644 --- a/Tests/app/AppKernel.php +++ b/Tests/app/AppKernel.php @@ -28,6 +28,7 @@ public function registerBundles() new Symfony\Bundle\FrameworkBundle\FrameworkBundle(), new ONGR\ElasticsearchBundle\ONGRElasticsearchBundle(), new ONGR\ElasticsearchBundle\Tests\app\fixture\Acme\TestBundle\AcmeTestBundle(), + new ONGR\ElasticsearchBundle\Tests\app\fixture\Acme\FooBundle\AcmeFooBundle(), ]; } diff --git a/Tests/app/config/config_test.yml b/Tests/app/config/config_test.yml index 86d44b99..aabe064a 100644 --- a/Tests/app/config/config_test.yml +++ b/Tests/app/config/config_test.yml @@ -28,6 +28,7 @@ ongr_elasticsearch: debug: true mappings: - AcmeTestBundle + - AcmeFooBundle:Media bar: connection: bar mappings: diff --git a/Tests/app/fixture/Acme/FooBundle/AcmeFooBundle.php b/Tests/app/fixture/Acme/FooBundle/AcmeFooBundle.php new file mode 100644 index 00000000..9beb09ac --- /dev/null +++ b/Tests/app/fixture/Acme/FooBundle/AcmeFooBundle.php @@ -0,0 +1,21 @@ + + * + * 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\FooBundle; + +use Symfony\Component\HttpKernel\Bundle\Bundle; + +/** + * AcmeTestBundle for testing. + */ +class AcmeFooBundle extends Bundle +{ +} diff --git a/Tests/app/fixture/Acme/FooBundle/Document/Media.php b/Tests/app/fixture/Acme/FooBundle/Document/Media.php new file mode 100644 index 00000000..36357b72 --- /dev/null +++ b/Tests/app/fixture/Acme/FooBundle/Document/Media.php @@ -0,0 +1,30 @@ + + * + * 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\FooBundle\Document; + +use ONGR\ElasticsearchBundle\Annotation as ES; +use ONGR\ElasticsearchBundle\Document\AbstractDocument; + +/** + * Testing document for representing media. + * + * @ES\Document(); + */ +class Media extends AbstractDocument +{ + /** + * @var string + * + * @ES\Property(name="media", type="string", index="not_analyzed") + */ + public $url; +}