Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Command/TypeUpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}
}
Expand Down
5 changes: 0 additions & 5 deletions DSL/Filter/NestedFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ class NestedFilter implements BuilderInterface
*/
private $query;

/**
* @var array
*/
private $parameters;

/**
* @param string $path
* @param BuilderInterface $query
Expand Down
7 changes: 4 additions & 3 deletions DependencyInjection/Compiler/MappingPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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());
}
Expand Down
11 changes: 1 addition & 10 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
42 changes: 29 additions & 13 deletions Mapping/MetadataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -89,17 +89,18 @@ public function getMapping($bundle, $force = false)
);
}
}
$this->documents[$bundle] = $mappings;

return $this->documents[$bundle];
$this->documents[$namespace] = $mappings;

return $this->documents[$namespace];
}

/**
* Retrieves document mapping by namespace.
*
* @param string $namespace Document namespace.
*
* @return ClassMetadata|null
* @return array|null
*/
public function getMappingByNamespace($namespace)
{
Expand All @@ -111,21 +112,36 @@ public function getMappingByNamespace($namespace)
*
* @param DocumentInterface $document
*
* @return ClassMetadata|null
* @return array|null
*/
public function getDocumentMapping(DocumentInterface $document)
{
return $this->getDocumentReflectionMapping(new \ReflectionObject($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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -102,7 +106,10 @@ public function testContainerDefaultParams()
'default' => [
'connection' => 'default',
'debug' => true,
'mappings' => ['AcmeTestBundle'],
'mappings' => [
'AcmeTestBundle',
'AcmeFooBundle:Media',
],
],
'bar' => [
'connection' => 'bar',
Expand Down
4 changes: 2 additions & 2 deletions Tests/Unit/DependencyInjection/Compiler/MappingPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [],
Expand All @@ -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')
Expand Down
23 changes: 0 additions & 23 deletions Tests/Unit/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = [
[
Expand Down
1 change: 1 addition & 0 deletions Tests/app/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
];
}

Expand Down
1 change: 1 addition & 0 deletions Tests/app/config/config_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ ongr_elasticsearch:
debug: true
mappings:
- AcmeTestBundle
- AcmeFooBundle:Media
bar:
connection: bar
mappings:
Expand Down
21 changes: 21 additions & 0 deletions Tests/app/fixture/Acme/FooBundle/AcmeFooBundle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* 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
{
}
30 changes: 30 additions & 0 deletions Tests/app/fixture/Acme/FooBundle/Document/Media.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* 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;
}