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);
- }
-}