adding ResetEvent (pre/post index and pre/post type reset), injected EventDispatcher into Resetter

This commit is contained in:
Oleg Andreyev 2015-01-04 14:00:12 +02:00
parent afbe1e03a1
commit 7efcdad97c
4 changed files with 127 additions and 8 deletions

85
Event/ResetEvent.php Normal file
View file

@ -0,0 +1,85 @@
<?php
namespace FOS\ElasticaBundle\Event;
use Symfony\Component\EventDispatcher\Event;
/**
* ResetEvent
*
* @author Oleg Andreyev <oleg.andreyev@intexsys.lv>
*/
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;
}
}

View file

@ -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);
}
/**

View file

@ -41,6 +41,7 @@
<argument type="service" id="fos_elastica.index_manager" />
<argument type="service" id="fos_elastica.alias_processor" />
<argument type="service" id="fos_elastica.mapping_builder" />
<argument type="service" id="event_dispatcher"/>
</service>
<!-- Abstract definition for all finders. -->

View file

@ -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(