adding ResetEvent (pre/post index and pre/post type reset), injected EventDispatcher into Resetter
This commit is contained in:
parent
afbe1e03a1
commit
7efcdad97c
85
Event/ResetEvent.php
Normal file
85
Event/ResetEvent.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -6,6 +6,8 @@ use Elastica\Index;
|
||||||
use Elastica\Exception\ResponseException;
|
use Elastica\Exception\ResponseException;
|
||||||
use Elastica\Type\Mapping;
|
use Elastica\Type\Mapping;
|
||||||
use FOS\ElasticaBundle\Configuration\ConfigManager;
|
use FOS\ElasticaBundle\Configuration\ConfigManager;
|
||||||
|
use FOS\ElasticaBundle\Event\ResetEvent;
|
||||||
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deletes and recreates indexes
|
* Deletes and recreates indexes
|
||||||
|
@ -18,7 +20,7 @@ class Resetter
|
||||||
private $aliasProcessor;
|
private $aliasProcessor;
|
||||||
|
|
||||||
/***
|
/***
|
||||||
* @var \FOS\ElasticaBundle\Configuration\Manager
|
* @var ConfigManager
|
||||||
*/
|
*/
|
||||||
private $configManager;
|
private $configManager;
|
||||||
|
|
||||||
|
@ -32,12 +34,30 @@ class Resetter
|
||||||
*/
|
*/
|
||||||
private $mappingBuilder;
|
private $mappingBuilder;
|
||||||
|
|
||||||
public function __construct(ConfigManager $configManager, IndexManager $indexManager, AliasProcessor $aliasProcessor, MappingBuilder $mappingBuilder)
|
/**
|
||||||
{
|
* @var EventDispatcherInterface
|
||||||
$this->aliasProcessor = $aliasProcessor;
|
*/
|
||||||
$this->configManager = $configManager;
|
private $eventDispatcher;
|
||||||
$this->indexManager = $indexManager;
|
|
||||||
$this->mappingBuilder = $mappingBuilder;
|
/**
|
||||||
|
* @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)
|
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);
|
$indexConfig = $this->configManager->getIndexConfiguration($indexName);
|
||||||
$index = $this->indexManager->getIndex($indexName);
|
$index = $this->indexManager->getIndex($indexName);
|
||||||
|
|
||||||
|
@ -74,6 +97,8 @@ class Resetter
|
||||||
if (!$populating and $indexConfig->isUseAlias()) {
|
if (!$populating and $indexConfig->isUseAlias()) {
|
||||||
$this->aliasProcessor->switchIndexAlias($indexConfig, $index, $force);
|
$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)
|
public function resetIndexType($indexName, $typeName)
|
||||||
{
|
{
|
||||||
|
$event = new ResetEvent($indexName, $typeName);
|
||||||
|
$this->eventDispatcher->dispatch(ResetEvent::PRE_TYPE_RESET, $event);
|
||||||
|
|
||||||
$typeConfig = $this->configManager->getTypeConfiguration($indexName, $typeName);
|
$typeConfig = $this->configManager->getTypeConfiguration($indexName, $typeName);
|
||||||
$type = $this->indexManager->getIndex($indexName)->getType($typeName);
|
$type = $this->indexManager->getIndex($indexName)->getType($typeName);
|
||||||
|
|
||||||
|
@ -103,6 +131,8 @@ class Resetter
|
||||||
}
|
}
|
||||||
|
|
||||||
$type->setMapping($mapping);
|
$type->setMapping($mapping);
|
||||||
|
|
||||||
|
$this->eventDispatcher->dispatch(ResetEvent::POST_TYPE_RESET, $event);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -41,6 +41,7 @@
|
||||||
<argument type="service" id="fos_elastica.index_manager" />
|
<argument type="service" id="fos_elastica.index_manager" />
|
||||||
<argument type="service" id="fos_elastica.alias_processor" />
|
<argument type="service" id="fos_elastica.alias_processor" />
|
||||||
<argument type="service" id="fos_elastica.mapping_builder" />
|
<argument type="service" id="fos_elastica.mapping_builder" />
|
||||||
|
<argument type="service" id="event_dispatcher"/>
|
||||||
</service>
|
</service>
|
||||||
|
|
||||||
<!-- Abstract definition for all finders. -->
|
<!-- Abstract definition for all finders. -->
|
||||||
|
|
|
@ -36,8 +36,11 @@ class ResetterTest extends \PHPUnit_Framework_TestCase
|
||||||
$this->mappingBuilder = $this->getMockBuilder('FOS\\ElasticaBundle\\Index\\MappingBuilder')
|
$this->mappingBuilder = $this->getMockBuilder('FOS\\ElasticaBundle\\Index\\MappingBuilder')
|
||||||
->disableOriginalConstructor()
|
->disableOriginalConstructor()
|
||||||
->getMock();
|
->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(
|
/*$this->indexConfigsByName = array(
|
||||||
'foo' => array(
|
'foo' => array(
|
||||||
|
|
Loading…
Reference in a new issue