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(