From 303af508b2029dd199b3a1b21d93fe30114587e9 Mon Sep 17 00:00:00 2001 From: Oleg Andreyev Date: Fri, 21 Nov 2014 16:25:40 +0200 Subject: [PATCH 1/5] adding populate events --- Command/PopulateCommand.php | 74 ++++++++++------ DependencyInjection/FOSElasticaExtension.php | 2 +- Event/PopulateEvent.php | 93 ++++++++++++++++++++ EventListener/PopulateListener.php | 59 +++++++++++++ Resources/config/listener.xml | 18 ++++ 5 files changed, 217 insertions(+), 29 deletions(-) create mode 100644 Event/PopulateEvent.php create mode 100644 EventListener/PopulateListener.php create mode 100644 Resources/config/listener.xml diff --git a/Command/PopulateCommand.php b/Command/PopulateCommand.php index f17ca4c..89d2ebc 100644 --- a/Command/PopulateCommand.php +++ b/Command/PopulateCommand.php @@ -2,15 +2,16 @@ namespace FOS\ElasticaBundle\Command; +use FOS\ElasticaBundle\Event\PopulateEvent; use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; use Symfony\Component\Console\Helper\DialogHelper; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; -use FOS\ElasticaBundle\IndexManager; +use FOS\ElasticaBundle\Index\IndexManager; use FOS\ElasticaBundle\Provider\ProviderRegistry; -use FOS\ElasticaBundle\Resetter; use FOS\ElasticaBundle\Provider\ProviderInterface; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; /** * Populate the search index @@ -28,9 +29,9 @@ class PopulateCommand extends ContainerAwareCommand private $providerRegistry; /** - * @var Resetter + * @var EventDispatcherInterface */ - private $resetter; + private $eventDispatcher; /** * @see Symfony\Component\Console\Command\Command::configure() @@ -57,7 +58,7 @@ class PopulateCommand extends ContainerAwareCommand { $this->indexManager = $this->getContainer()->get('fos_elastica.index_manager'); $this->providerRegistry = $this->getContainer()->get('fos_elastica.provider_registry'); - $this->resetter = $this->getContainer()->get('fos_elastica.resetter'); + $this->eventDispatcher = $this->getContainer()->get('event_dispatcher'); } /** @@ -109,24 +110,12 @@ class PopulateCommand extends ContainerAwareCommand */ private function populateIndex(OutputInterface $output, $index, $reset, $options) { - if ($reset) { - $output->writeln(sprintf('Resetting %s', $index)); - $this->resetter->resetIndex($index, true); - } - /** @var $providers ProviderInterface[] */ $providers = $this->providerRegistry->getIndexProviders($index); - foreach ($providers as $type => $provider) { - $loggerClosure = function($message) use ($output, $index, $type) { - $output->writeln(sprintf('Populating %s/%s, %s', $index, $type, $message)); - }; - - $provider->populate($loggerClosure, $options); - } + $this->populate($output, $providers, $index, null, $reset, $options); $output->writeln(sprintf('Refreshing %s', $index)); - $this->resetter->postPopulate($index); $this->indexManager->getIndex($index)->refresh(); } @@ -141,19 +130,48 @@ class PopulateCommand extends ContainerAwareCommand */ private function populateIndexType(OutputInterface $output, $index, $type, $reset, $options) { - if ($reset) { - $output->writeln(sprintf('Resetting %s/%s', $index, $type)); - $this->resetter->resetIndexType($index, $type); - } - - $loggerClosure = function($message) use ($output, $index, $type) { - $output->writeln(sprintf('Populating %s/%s, %s', $index, $type, $message)); - }; - $provider = $this->providerRegistry->getProvider($index, $type); - $provider->populate($loggerClosure, $options); + + $this->populate($output, array($type => $provider), $index, $type, $reset, $options); $output->writeln(sprintf('Refreshing %s', $index)); $this->indexManager->getIndex($index)->refresh(); } + + /** + * @param OutputInterface $output + * @param ProviderInterface[] $providers + * @param string $index + * @param string $type + * @param boolean $reset + * @param array $options + */ + private function populate(OutputInterface $output, array $providers, $index, $type, $reset, $options) + { + if ($reset) { + if ($type) { + $output->writeln(sprintf('Resetting %s/%s', $index, $type)); + } else { + $output->writeln(sprintf('Resetting %s', $index)); + } + } + + $this->eventDispatcher->dispatch(PopulateEvent::PRE_INDEX_POPULATE, new PopulateEvent($index, $type, $reset, $options)); + + foreach ($providers as $providerType => $provider) { + $event = new PopulateEvent($index, $providerType, $reset, $options); + + $this->eventDispatcher->dispatch(PopulateEvent::PRE_TYPE_POPULATE, $event); + + $loggerClosure = function($message) use ($output, $index, $providerType) { + $output->writeln(sprintf('Populating %s/%s, %s', $index, $providerType, $message)); + }; + + $provider->populate($loggerClosure, $options); + + $this->eventDispatcher->dispatch(PopulateEvent::POST_TYPE_POPULATE, $event); + } + + $this->eventDispatcher->dispatch(PopulateEvent::POST_INDEX_POPULATE, new PopulateEvent($index, $type, $reset, $options)); + } } diff --git a/DependencyInjection/FOSElasticaExtension.php b/DependencyInjection/FOSElasticaExtension.php index 565ba33..705da12 100644 --- a/DependencyInjection/FOSElasticaExtension.php +++ b/DependencyInjection/FOSElasticaExtension.php @@ -46,7 +46,7 @@ class FOSElasticaExtension extends Extension return; } - foreach (array('config', 'index', 'persister', 'provider', 'source', 'transformer') as $basename) { + foreach (array('config', 'index', 'persister', 'provider', 'source', 'transformer', 'listener') as $basename) { $loader->load(sprintf('%s.xml', $basename)); } diff --git a/Event/PopulateEvent.php b/Event/PopulateEvent.php new file mode 100644 index 0000000..9bf9dbb --- /dev/null +++ b/Event/PopulateEvent.php @@ -0,0 +1,93 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOS\ElasticaBundle\Event; + +use Symfony\Component\EventDispatcher\Event; + +/** + * Populate Event + * + * @author Oleg Andreyev + */ +class PopulateEvent extends Event +{ + const PRE_INDEX_POPULATE = 'elastica.index.index_pre_populate'; + const POST_INDEX_POPULATE = 'elastica.index.index_post_populate'; + + const PRE_TYPE_POPULATE = 'elastica.index.type_pre_populate'; + const POST_TYPE_POPULATE = 'elastica.index.type_post_populate'; + + /** + * @var string + */ + private $index; + + /** + * @var string + */ + private $type; + + /** + * @var bool + */ + private $reset; + + /** + * @var array + */ + private $options; + + /** + * @param string $index + * @param string $type + * @param boolean $reset + * @param array $options + */ + public function __construct($index, $type, $reset, $options) + { + $this->index = $index; + $this->type = $type; + $this->reset = $reset; + $this->options = $options; + } + + /** + * @return string + */ + public function getIndex() + { + return $this->index; + } + + /** + * @return string + */ + public function getType() + { + return $this->type; + } + + /** + * @return boolean + */ + public function isReset() + { + return $this->reset; + } + + /** + * @return array + */ + public function getOptions() + { + return $this->options; + } +} diff --git a/EventListener/PopulateListener.php b/EventListener/PopulateListener.php new file mode 100644 index 0000000..a2a0f5a --- /dev/null +++ b/EventListener/PopulateListener.php @@ -0,0 +1,59 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOS\ElasticaBundle\EventListener; + +use FOS\ElasticaBundle\Event\PopulateEvent; +use FOS\ElasticaBundle\Index\Resetter; + +/** + * PopulateListener + * + * @author Oleg Andreyev + */ +class PopulateListener +{ + /** + * @var Resetter + */ + private $resetter; + + /** + * @param Resetter $resetter + */ + public function __construct(Resetter $resetter) + { + $this->resetter = $resetter; + } + + /** + * @param PopulateEvent $event + */ + public function preIndexPopulate(PopulateEvent $event) + { + if (!$event->isReset()) { + return; + } + + if (null !== $event->getType()) { + $this->resetter->resetIndexType($event->getIndex(), $event->getType()); + } else { + $this->resetter->resetIndex($event->getIndex(), true); + } + } + + /** + * @param PopulateEvent $event + */ + public function postIndexPopulate(PopulateEvent $event) + { + $this->resetter->postPopulate($event->getIndex()); + } +} diff --git a/Resources/config/listener.xml b/Resources/config/listener.xml new file mode 100644 index 0000000..6c586bf --- /dev/null +++ b/Resources/config/listener.xml @@ -0,0 +1,18 @@ + + + + + + FOS\ElasticaBundle\EventListener\PopulateListener + + + + + + + + + + From afbe1e03a1cf9e3e978decbf26ac0b3b1877dcae Mon Sep 17 00:00:00 2001 From: Oleg Andreyev Date: Sun, 4 Jan 2015 13:34:59 +0200 Subject: [PATCH 2/5] adding unit test for PopulateListener --- Event/PopulateEvent.php | 8 +-- Tests/EventListener/PopulateListenerTest.php | 54 ++++++++++++++++++++ 2 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 Tests/EventListener/PopulateListenerTest.php diff --git a/Event/PopulateEvent.php b/Event/PopulateEvent.php index 9bf9dbb..305c393 100644 --- a/Event/PopulateEvent.php +++ b/Event/PopulateEvent.php @@ -46,10 +46,10 @@ class PopulateEvent extends Event private $options; /** - * @param string $index - * @param string $type - * @param boolean $reset - * @param array $options + * @param string $index + * @param string|null $type + * @param boolean $reset + * @param array $options */ public function __construct($index, $type, $reset, $options) { diff --git a/Tests/EventListener/PopulateListenerTest.php b/Tests/EventListener/PopulateListenerTest.php new file mode 100644 index 0000000..6388e79 --- /dev/null +++ b/Tests/EventListener/PopulateListenerTest.php @@ -0,0 +1,54 @@ +resetter = $this->getMockBuilder('FOS\ElasticaBundle\Index\Resetter') + ->disableOriginalConstructor() + ->getMock(); + + $this->listener = new PopulateListener($this->resetter); + } + + public function testPostIndexPopulate() + { + $this->resetter->expects($this->once())->method('postPopulate')->with('indexName'); + $this->listener->postIndexPopulate(new PopulateEvent('indexName', null, true, array())); + } + + public function testPreIndexPopulateWhenNoResetRequired() + { + $this->resetter->expects($this->never())->method('resetIndex'); + $this->resetter->expects($this->never())->method('resetIndexType'); + $this->listener->preIndexPopulate(new PopulateEvent('indexName', null, false, array())); + } + + public function testPreIndexPopulateWhenResetIsRequiredAndNoTypeIsSpecified() + { + $this->resetter->expects($this->once())->method('resetIndex')->with('indexName'); + $this->listener->preIndexPopulate(new PopulateEvent('indexName', null, true, array())); + } + + public function testPreIndexPopulateWhenResetIsRequiredAndTypeIsSpecified() + { + $this->resetter->expects($this->once())->method('resetIndexType')->with('indexName', 'indexType'); + $this->listener->preIndexPopulate(new PopulateEvent('indexName', 'indexType', true, array())); + } +} From 7efcdad97c3278407e08c74221641a8a7faf258d Mon Sep 17 00:00:00 2001 From: Oleg Andreyev Date: Sun, 4 Jan 2015 14:00:12 +0200 Subject: [PATCH 3/5] adding ResetEvent (pre/post index and pre/post type reset), injected EventDispatcher into Resetter --- Event/ResetEvent.php | 85 ++++++++++++++++++++++++++++++++++++ Index/Resetter.php | 44 ++++++++++++++++--- Resources/config/index.xml | 1 + Tests/Index/ResetterTest.php | 5 ++- 4 files changed, 127 insertions(+), 8 deletions(-) create mode 100644 Event/ResetEvent.php diff --git a/Event/ResetEvent.php b/Event/ResetEvent.php new file mode 100644 index 0000000..04034af --- /dev/null +++ b/Event/ResetEvent.php @@ -0,0 +1,85 @@ + + */ +class ResetEvent extends Event +{ + const PRE_INDEX_RESET = 'elastica.index.pre_reset'; + const POST_INDEX_RESET = 'elastica.index.post_reset'; + + const PRE_TYPE_RESET = 'elastica.index.type_pre_reset'; + const POST_TYPE_RESET = 'elastica.index.type_post_reset'; + + /** + * @var string + */ + private $indexName; + + /** + * @var string + */ + private $indexType; + + /** + * @var bool + */ + private $populating; + + /** + * @var bool + */ + private $force; + + /** + * @param string $indexName + * @param string $indexType + * @param bool $populating + * @param bool $force + */ + public function __construct($indexName, $indexType, $populating = false, $force = false) + { + $this->indexName = $indexName; + $this->indexType = $indexType; + $this->populating = (bool)$populating; + $this->force = (bool)$force; + } + + /** + * @return string + */ + public function getIndexName() + { + return $this->indexName; + } + + /** + * @return string + */ + public function getIndexType() + { + return $this->indexType; + } + + /** + * @return boolean + */ + public function isPopulating() + { + return $this->populating; + } + + /** + * @return boolean + */ + public function isForce() + { + return $this->force; + } +} \ No newline at end of file diff --git a/Index/Resetter.php b/Index/Resetter.php index 9b65a8f..996bfdf 100644 --- a/Index/Resetter.php +++ b/Index/Resetter.php @@ -6,6 +6,8 @@ use Elastica\Index; use Elastica\Exception\ResponseException; use Elastica\Type\Mapping; use FOS\ElasticaBundle\Configuration\ConfigManager; +use FOS\ElasticaBundle\Event\ResetEvent; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; /** * Deletes and recreates indexes @@ -18,7 +20,7 @@ class Resetter private $aliasProcessor; /*** - * @var \FOS\ElasticaBundle\Configuration\Manager + * @var ConfigManager */ private $configManager; @@ -32,12 +34,30 @@ class Resetter */ private $mappingBuilder; - public function __construct(ConfigManager $configManager, IndexManager $indexManager, AliasProcessor $aliasProcessor, MappingBuilder $mappingBuilder) - { - $this->aliasProcessor = $aliasProcessor; - $this->configManager = $configManager; - $this->indexManager = $indexManager; - $this->mappingBuilder = $mappingBuilder; + /** + * @var EventDispatcherInterface + */ + private $eventDispatcher; + + /** + * @param ConfigManager $configManager + * @param IndexManager $indexManager + * @param AliasProcessor $aliasProcessor + * @param MappingBuilder $mappingBuilder + * @param EventDispatcherInterface $eventDispatcher + */ + public function __construct( + ConfigManager $configManager, + IndexManager $indexManager, + AliasProcessor $aliasProcessor, + MappingBuilder $mappingBuilder, + EventDispatcherInterface $eventDispatcher + ) { + $this->aliasProcessor = $aliasProcessor; + $this->configManager = $configManager; + $this->indexManager = $indexManager; + $this->mappingBuilder = $mappingBuilder; + $this->eventDispatcher = $eventDispatcher; } /** @@ -61,6 +81,9 @@ class Resetter */ public function resetIndex($indexName, $populating = false, $force = false) { + $event = new ResetEvent($indexName, null, $populating, $force); + $this->eventDispatcher->dispatch(ResetEvent::PRE_INDEX_RESET, $event); + $indexConfig = $this->configManager->getIndexConfiguration($indexName); $index = $this->indexManager->getIndex($indexName); @@ -74,6 +97,8 @@ class Resetter if (!$populating and $indexConfig->isUseAlias()) { $this->aliasProcessor->switchIndexAlias($indexConfig, $index, $force); } + + $this->eventDispatcher->dispatch(ResetEvent::POST_INDEX_RESET, $event); } /** @@ -86,6 +111,9 @@ class Resetter */ public function resetIndexType($indexName, $typeName) { + $event = new ResetEvent($indexName, $typeName); + $this->eventDispatcher->dispatch(ResetEvent::PRE_TYPE_RESET, $event); + $typeConfig = $this->configManager->getTypeConfiguration($indexName, $typeName); $type = $this->indexManager->getIndex($indexName)->getType($typeName); @@ -103,6 +131,8 @@ class Resetter } $type->setMapping($mapping); + + $this->eventDispatcher->dispatch(ResetEvent::POST_TYPE_RESET, $event); } /** diff --git a/Resources/config/index.xml b/Resources/config/index.xml index 11586ff..3ae2e50 100644 --- a/Resources/config/index.xml +++ b/Resources/config/index.xml @@ -41,6 +41,7 @@ + diff --git a/Tests/Index/ResetterTest.php b/Tests/Index/ResetterTest.php index 28f0a68..35a0bd9 100644 --- a/Tests/Index/ResetterTest.php +++ b/Tests/Index/ResetterTest.php @@ -36,8 +36,11 @@ class ResetterTest extends \PHPUnit_Framework_TestCase $this->mappingBuilder = $this->getMockBuilder('FOS\\ElasticaBundle\\Index\\MappingBuilder') ->disableOriginalConstructor() ->getMock(); + $this->resetter = $this->getMockBuilder('FOS\ElasticaBundle\Index\Resetter') + ->disableOriginalConstructor() + ->getMock(); - $this->resetter = new Resetter($this->configManager, $this->indexManager, $this->aliasProcessor, $this->mappingBuilder); + $this->resetter = new Resetter($this->configManager, $this->indexManager, $this->aliasProcessor, $this->mappingBuilder, $this->resetter); /*$this->indexConfigsByName = array( 'foo' => array( From 7c90660e02e45e7dbab20bfd2b3e5159c089a29d Mon Sep 17 00:00:00 2001 From: Oleg Andreyev Date: Sun, 4 Jan 2015 20:57:08 +0200 Subject: [PATCH 4/5] attempt to make scrutinizer happy, about duplicate code in test --- Tests/EventListener/PopulateListenerTest.php | 49 +++++++++++++++----- 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/Tests/EventListener/PopulateListenerTest.php b/Tests/EventListener/PopulateListenerTest.php index 6388e79..3d653d3 100644 --- a/Tests/EventListener/PopulateListenerTest.php +++ b/Tests/EventListener/PopulateListenerTest.php @@ -33,22 +33,47 @@ class PopulateListenerTest extends \PHPUnit_Framework_TestCase $this->listener->postIndexPopulate(new PopulateEvent('indexName', null, true, array())); } - public function testPreIndexPopulateWhenNoResetRequired() + public function preIndexPopulateDataProvider() { - $this->resetter->expects($this->never())->method('resetIndex'); - $this->resetter->expects($this->never())->method('resetIndexType'); - $this->listener->preIndexPopulate(new PopulateEvent('indexName', null, false, array())); + return array( + array( + array( + array('resetIndex', $this->never()), + array('resetIndexType', $this->never()) + ), + array('indexName', true), + new PopulateEvent('indexName', null, false, array()) + ), + array( + array( + array('resetIndex', $this->once()) + ), + array('indexName', true), + new PopulateEvent('indexName', null, true, array()) + ), + array( + array( + array('resetIndexType', $this->once()) + ), + array('indexName', 'indexType'), + new PopulateEvent('indexName', 'indexType', true, array()) + ) + ); } - public function testPreIndexPopulateWhenResetIsRequiredAndNoTypeIsSpecified() + /** + * @param array $asserts + * @param array $withArgs + * @param PopulateEvent $event + * + * @dataProvider preIndexPopulateDataProvider + */ + public function testPreIndexPopulate(array $asserts, array $withArgs, PopulateEvent $event) { - $this->resetter->expects($this->once())->method('resetIndex')->with('indexName'); - $this->listener->preIndexPopulate(new PopulateEvent('indexName', null, true, array())); - } + foreach ($asserts as $assert) { + $this->resetter->expects($assert[1])->method($assert[0])->with($withArgs[0], $withArgs[1]); + } - public function testPreIndexPopulateWhenResetIsRequiredAndTypeIsSpecified() - { - $this->resetter->expects($this->once())->method('resetIndexType')->with('indexName', 'indexType'); - $this->listener->preIndexPopulate(new PopulateEvent('indexName', 'indexType', true, array())); + $this->listener->preIndexPopulate($event); } } From 4c87d24fc12d3e08580bd6aecbfdd3b289e316f2 Mon Sep 17 00:00:00 2001 From: Tim Nagel Date: Wed, 11 Mar 2015 14:30:10 +1100 Subject: [PATCH 5/5] Changes after review --- CHANGELOG-3.1.md | 6 ++ Command/PopulateCommand.php | 30 +++---- DependencyInjection/FOSElasticaExtension.php | 2 +- Event/IndexPopulateEvent.php | 11 ++- Event/{ResetEvent.php => IndexResetEvent.php} | 71 ++++++++--------- Event/TypePopulateEvent.php | 43 ++-------- Event/TypeResetEvent.php | 61 ++++++++++++++ EventListener/PopulateListener.php | 59 -------------- Index/Resetter.php | 37 ++++----- Resources/config/listener.xml | 18 ----- Tests/EventListener/PopulateListenerTest.php | 79 ------------------- 11 files changed, 148 insertions(+), 269 deletions(-) rename Event/{ResetEvent.php => IndexResetEvent.php} (52%) create mode 100644 Event/TypeResetEvent.php delete mode 100644 EventListener/PopulateListener.php delete mode 100644 Resources/config/listener.xml delete mode 100644 Tests/EventListener/PopulateListenerTest.php diff --git a/CHANGELOG-3.1.md b/CHANGELOG-3.1.md index 19bec1f..51ad513 100644 --- a/CHANGELOG-3.1.md +++ b/CHANGELOG-3.1.md @@ -31,3 +31,9 @@ https://github.com/FriendsOfSymfony/FOSElasticaBundle/compare/v3.0.4...v3.1.0 that property while transforming. Combined with the above POST_TRANSFORM event developers can now create calculated dynamic properties on Elastica documents for indexing. #794 + * New events `PRE_INDEX_POPULATE`, `POST_INDEX_POPULATE`, `PRE_TYPE_POPULATE` and + `POST_TYPE_POPULATE` allow for monitoring when an index is about to be or has + just been populated. #744 + * New events `PRE_INDEX_RESET`, `POST_INDEX_RESET`, `PRE_TYPE_RESET` and + `POST_TYPE_RESET` are run before and after operations that will reset an + index. #744 diff --git a/Command/PopulateCommand.php b/Command/PopulateCommand.php index 8b6c0f2..3e6b684 100644 --- a/Command/PopulateCommand.php +++ b/Command/PopulateCommand.php @@ -109,24 +109,6 @@ class PopulateCommand extends ContainerAwareCommand } } - /** - * @param ProviderInterface $provider - * @param OutputInterface $output - * @param string $index - * @param string $type - * @param array $options - */ - private function doPopulateType(ProviderInterface $provider, OutputInterface $output, $index, $type, $options) - { - $event = new TypePopulateEvent($index, $type, $options); - $this->dispatcher->dispatch(TypePopulateEvent::PRE_TYPE_POPULATE, $event); - - $loggerClosure = $this->getLoggerClosure($output, $index, $type); - $provider->populate($loggerClosure, $event->getOptions()); - - $this->dispatcher->dispatch(TypePopulateEvent::POST_TYPE_POPULATE, $event); - } - /** * Builds a loggerClosure to be called from inside the Provider to update the command * line. @@ -210,7 +192,7 @@ class PopulateCommand extends ContainerAwareCommand $providers = $this->providerRegistry->getIndexProviders($index); foreach ($providers as $type => $provider) { - $this->doPopulateType($provider, $output, $index, $type, $event->getOptions()); + $this->populateIndexType($output, $index, $type, false, $event->getOptions()); } $this->dispatcher->dispatch(IndexPopulateEvent::POST_INDEX_POPULATE, $event); @@ -229,13 +211,19 @@ class PopulateCommand extends ContainerAwareCommand */ private function populateIndexType(OutputInterface $output, $index, $type, $reset, $options) { - if ($reset) { + $event = new TypePopulateEvent($index, $type, $reset, $options); + $this->dispatcher->dispatch(TypePopulateEvent::PRE_TYPE_POPULATE, $event); + + if ($event->isReset()) { $output->writeln(sprintf('Resetting %s/%s', $index, $type)); $this->resetter->resetIndexType($index, $type); } $provider = $this->providerRegistry->getProvider($index, $type); - $this->doPopulateType($provider, $output, $index, $type, $options); + $loggerClosure = $this->getLoggerClosure($output, $index, $type); + $provider->populate($loggerClosure, $event->getOptions()); + + $this->dispatcher->dispatch(TypePopulateEvent::POST_TYPE_POPULATE, $event); $this->refreshIndex($output, $index, false); } diff --git a/DependencyInjection/FOSElasticaExtension.php b/DependencyInjection/FOSElasticaExtension.php index 1e446f7..9c1912e 100644 --- a/DependencyInjection/FOSElasticaExtension.php +++ b/DependencyInjection/FOSElasticaExtension.php @@ -46,7 +46,7 @@ class FOSElasticaExtension extends Extension return; } - foreach (array('config', 'index', 'persister', 'provider', 'source', 'transformer', 'listener') as $basename) { + foreach (array('config', 'index', 'persister', 'provider', 'source', 'transformer') as $basename) { $loader->load(sprintf('%s.xml', $basename)); } diff --git a/Event/IndexPopulateEvent.php b/Event/IndexPopulateEvent.php index 5074f05..56b1e83 100644 --- a/Event/IndexPopulateEvent.php +++ b/Event/IndexPopulateEvent.php @@ -1,4 +1,5 @@ */ @@ -72,4 +73,12 @@ class IndexPopulateEvent extends Event { return $this->options; } + + /** + * @param boolean $reset + */ + public function setReset($reset) + { + $this->reset = $reset; + } } diff --git a/Event/ResetEvent.php b/Event/IndexResetEvent.php similarity index 52% rename from Event/ResetEvent.php rename to Event/IndexResetEvent.php index 04034af..0caf241 100644 --- a/Event/ResetEvent.php +++ b/Event/IndexResetEvent.php @@ -1,70 +1,69 @@ + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + namespace FOS\ElasticaBundle\Event; use Symfony\Component\EventDispatcher\Event; /** - * ResetEvent + * Index ResetEvent * * @author Oleg Andreyev */ -class ResetEvent extends Event +class IndexResetEvent extends Event { const PRE_INDEX_RESET = 'elastica.index.pre_reset'; const POST_INDEX_RESET = 'elastica.index.post_reset'; - const PRE_TYPE_RESET = 'elastica.index.type_pre_reset'; - const POST_TYPE_RESET = 'elastica.index.type_post_reset'; - - /** - * @var string - */ - private $indexName; - - /** - * @var string - */ - private $indexType; - - /** - * @var bool - */ - private $populating; - /** * @var bool */ private $force; /** - * @param string $indexName - * @param string $indexType + * @var string + */ + private $index; + + /** + * @var bool + */ + private $populating; + + /** + * @param string $index * @param bool $populating * @param bool $force */ - public function __construct($indexName, $indexType, $populating = false, $force = false) + public function __construct($index, $populating, $force) { - $this->indexName = $indexName; - $this->indexType = $indexType; - $this->populating = (bool)$populating; - $this->force = (bool)$force; + $this->force = $force; + $this->index = $index; + $this->populating = $populating; } /** * @return string */ - public function getIndexName() + public function getIndex() { - return $this->indexName; + return $this->index; } /** - * @return string + * @return boolean */ - public function getIndexType() + public function isForce() { - return $this->indexType; + return $this->force; } /** @@ -76,10 +75,10 @@ class ResetEvent extends Event } /** - * @return boolean + * @param boolean $force */ - public function isForce() + public function setForce($force) { - return $this->force; + $this->force = $force; } -} \ No newline at end of file +} diff --git a/Event/TypePopulateEvent.php b/Event/TypePopulateEvent.php index 9239c15..e04bdd8 100644 --- a/Event/TypePopulateEvent.php +++ b/Event/TypePopulateEvent.php @@ -1,4 +1,5 @@ */ -class TypePopulateEvent extends Event +class TypePopulateEvent extends IndexPopulateEvent { const PRE_TYPE_POPULATE = 'elastica.index.type_pre_populate'; const POST_TYPE_POPULATE = 'elastica.index.type_post_populate'; - /** - * @var string - */ - private $index; - /** * @var string */ private $type; - /** - * @var array - */ - private $options; - /** * @param string $index * @param string $type + * @param bool $reset * @param array $options */ - public function __construct($index, $type, $options) + public function __construct($index, $type, $reset, $options) { - $this->index = $index; - $this->type = $type; - $this->options = $options; - } + parent::__construct($index, $reset, $options); - /** - * @return string - */ - public function getIndex() - { - return $this->index; + $this->type = $type; } /** @@ -64,17 +48,4 @@ class TypePopulateEvent extends Event { return $this->type; } - - /** - * @return array - */ - public function getOptions() - { - return $this->options; - } - - public function setOptions($options) - { - $this->options = $options; - } } diff --git a/Event/TypeResetEvent.php b/Event/TypeResetEvent.php new file mode 100644 index 0000000..37c2cf8 --- /dev/null +++ b/Event/TypeResetEvent.php @@ -0,0 +1,61 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace FOS\ElasticaBundle\Event; + +use Symfony\Component\EventDispatcher\Event; + +/** + * Type ResetEvent + * + * @author Oleg Andreyev + */ +class TypeResetEvent extends Event +{ + const PRE_TYPE_RESET = 'elastica.index.type_pre_reset'; + const POST_TYPE_RESET = 'elastica.index.type_post_reset'; + + /** + * @var string + */ + private $index; + + /** + * @var string + */ + private $type; + + /** + * @param string $index + * @param string $type + */ + public function __construct($index, $type) + { + $this->type = $type; + $this->index = $index; + } + + /** + * @return string + */ + public function getIndex() + { + return $this->index; + } + + /** + * @return string + */ + public function getType() + { + return $this->type; + } +} diff --git a/EventListener/PopulateListener.php b/EventListener/PopulateListener.php deleted file mode 100644 index a2a0f5a..0000000 --- a/EventListener/PopulateListener.php +++ /dev/null @@ -1,59 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace FOS\ElasticaBundle\EventListener; - -use FOS\ElasticaBundle\Event\PopulateEvent; -use FOS\ElasticaBundle\Index\Resetter; - -/** - * PopulateListener - * - * @author Oleg Andreyev - */ -class PopulateListener -{ - /** - * @var Resetter - */ - private $resetter; - - /** - * @param Resetter $resetter - */ - public function __construct(Resetter $resetter) - { - $this->resetter = $resetter; - } - - /** - * @param PopulateEvent $event - */ - public function preIndexPopulate(PopulateEvent $event) - { - if (!$event->isReset()) { - return; - } - - if (null !== $event->getType()) { - $this->resetter->resetIndexType($event->getIndex(), $event->getType()); - } else { - $this->resetter->resetIndex($event->getIndex(), true); - } - } - - /** - * @param PopulateEvent $event - */ - public function postIndexPopulate(PopulateEvent $event) - { - $this->resetter->postPopulate($event->getIndex()); - } -} diff --git a/Index/Resetter.php b/Index/Resetter.php index d2b3cea..1eb91a5 100644 --- a/Index/Resetter.php +++ b/Index/Resetter.php @@ -6,7 +6,8 @@ use Elastica\Index; use Elastica\Exception\ResponseException; use Elastica\Type\Mapping; use FOS\ElasticaBundle\Configuration\ConfigManager; -use FOS\ElasticaBundle\Event\ResetEvent; +use FOS\ElasticaBundle\Event\IndexResetEvent; +use FOS\ElasticaBundle\Event\TypeResetEvent; use Symfony\Component\EventDispatcher\EventDispatcherInterface; /** @@ -24,6 +25,11 @@ class Resetter */ private $configManager; + /** + * @var EventDispatcherInterface + */ + private $dispatcher; + /** * @var IndexManager */ @@ -34,11 +40,6 @@ class Resetter */ private $mappingBuilder; - /** - * @var EventDispatcherInterface - */ - private $eventDispatcher; - /** * @param ConfigManager $configManager * @param IndexManager $indexManager @@ -53,11 +54,11 @@ class Resetter MappingBuilder $mappingBuilder, EventDispatcherInterface $eventDispatcher ) { - $this->aliasProcessor = $aliasProcessor; - $this->configManager = $configManager; - $this->indexManager = $indexManager; - $this->mappingBuilder = $mappingBuilder; - $this->eventDispatcher = $eventDispatcher; + $this->aliasProcessor = $aliasProcessor; + $this->configManager = $configManager; + $this->dispatcher = $eventDispatcher; + $this->indexManager = $indexManager; + $this->mappingBuilder = $mappingBuilder; } /** @@ -84,8 +85,8 @@ class Resetter */ public function resetIndex($indexName, $populating = false, $force = false) { - $event = new ResetEvent($indexName, null, $populating, $force); - $this->eventDispatcher->dispatch(ResetEvent::PRE_INDEX_RESET, $event); + $event = new IndexResetEvent($indexName, $populating, $force); + $this->dispatcher->dispatch(IndexResetEvent::PRE_INDEX_RESET, $event); $indexConfig = $this->configManager->getIndexConfiguration($indexName); $index = $this->indexManager->getIndex($indexName); @@ -101,7 +102,7 @@ class Resetter $this->aliasProcessor->switchIndexAlias($indexConfig, $index, $force); } - $this->eventDispatcher->dispatch(ResetEvent::POST_INDEX_RESET, $event); + $this->dispatcher->dispatch(IndexResetEvent::POST_INDEX_RESET, $event); } /** @@ -114,8 +115,8 @@ class Resetter */ public function resetIndexType($indexName, $typeName) { - $event = new ResetEvent($indexName, $typeName); - $this->eventDispatcher->dispatch(ResetEvent::PRE_TYPE_RESET, $event); + $event = new TypeResetEvent($indexName, $typeName); + $this->dispatcher->dispatch(TypeResetEvent::PRE_TYPE_RESET, $event); $typeConfig = $this->configManager->getTypeConfiguration($indexName, $typeName); $type = $this->indexManager->getIndex($indexName)->getType($typeName); @@ -128,14 +129,14 @@ class Resetter } } - $mapping = new Mapping; + $mapping = new Mapping(); foreach ($this->mappingBuilder->buildTypeMapping($typeConfig) as $name => $field) { $mapping->setParam($name, $field); } $type->setMapping($mapping); - $this->eventDispatcher->dispatch(ResetEvent::POST_TYPE_RESET, $event); + $this->dispatcher->dispatch(TypeResetEvent::POST_TYPE_RESET, $event); } /** diff --git a/Resources/config/listener.xml b/Resources/config/listener.xml deleted file mode 100644 index 6c586bf..0000000 --- a/Resources/config/listener.xml +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - FOS\ElasticaBundle\EventListener\PopulateListener - - - - - - - - - - diff --git a/Tests/EventListener/PopulateListenerTest.php b/Tests/EventListener/PopulateListenerTest.php deleted file mode 100644 index 3d653d3..0000000 --- a/Tests/EventListener/PopulateListenerTest.php +++ /dev/null @@ -1,79 +0,0 @@ -resetter = $this->getMockBuilder('FOS\ElasticaBundle\Index\Resetter') - ->disableOriginalConstructor() - ->getMock(); - - $this->listener = new PopulateListener($this->resetter); - } - - public function testPostIndexPopulate() - { - $this->resetter->expects($this->once())->method('postPopulate')->with('indexName'); - $this->listener->postIndexPopulate(new PopulateEvent('indexName', null, true, array())); - } - - public function preIndexPopulateDataProvider() - { - return array( - array( - array( - array('resetIndex', $this->never()), - array('resetIndexType', $this->never()) - ), - array('indexName', true), - new PopulateEvent('indexName', null, false, array()) - ), - array( - array( - array('resetIndex', $this->once()) - ), - array('indexName', true), - new PopulateEvent('indexName', null, true, array()) - ), - array( - array( - array('resetIndexType', $this->once()) - ), - array('indexName', 'indexType'), - new PopulateEvent('indexName', 'indexType', true, array()) - ) - ); - } - - /** - * @param array $asserts - * @param array $withArgs - * @param PopulateEvent $event - * - * @dataProvider preIndexPopulateDataProvider - */ - public function testPreIndexPopulate(array $asserts, array $withArgs, PopulateEvent $event) - { - foreach ($asserts as $assert) { - $this->resetter->expects($assert[1])->method($assert[0])->with($withArgs[0], $withArgs[1]); - } - - $this->listener->preIndexPopulate($event); - } -}