Changes after review

This commit is contained in:
Tim Nagel 2015-03-11 14:30:10 +11:00
parent f6df88cc67
commit 4c87d24fc1
11 changed files with 148 additions and 269 deletions

View file

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

View file

@ -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('<info>Resetting</info> <comment>%s/%s</comment>', $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);
}

View file

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

View file

@ -1,4 +1,5 @@
<?php
/**
* This file is part of the FOSElasticaBundle project.
*
@ -13,7 +14,7 @@ namespace FOS\ElasticaBundle\Event;
use Symfony\Component\EventDispatcher\Event;
/**
* Populate Event
* Index Populate Event
*
* @author Oleg Andreyev <oleg.andreyev@intexsys.lv>
*/
@ -72,4 +73,12 @@ class IndexPopulateEvent extends Event
{
return $this->options;
}
/**
* @param boolean $reset
*/
public function setReset($reset)
{
$this->reset = $reset;
}
}

View file

@ -1,70 +1,69 @@
<?php
/**
* This file is part of the FOSElasticaBundle project.
*
* (c) Infinite Networks Pty Ltd <http://www.infinite.net.au>
*
* 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 <oleg.andreyev@intexsys.lv>
*/
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;
}
}
}

View file

@ -1,4 +1,5 @@
<?php
/**
* This file is part of the FOSElasticaBundle project.
*
@ -13,48 +14,31 @@ namespace FOS\ElasticaBundle\Event;
use Symfony\Component\EventDispatcher\Event;
/**
* Populate Event
* Type Populate Event
*
* @author Oleg Andreyev <oleg.andreyev@intexsys.lv>
*/
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;
}
}

61
Event/TypeResetEvent.php Normal file
View file

@ -0,0 +1,61 @@
<?php
/**
* This file is part of the FOSElasticaBundle project.
*
* (c) Infinite Networks Pty Ltd <http://www.infinite.net.au>
*
* 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 <oleg.andreyev@intexsys.lv>
*/
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;
}
}

View file

@ -1,59 +0,0 @@
<?php
/**
* This file is part of the FOSElasticaBundle project.
*
* (c) FriendsOfSymfony <https://github.com/FriendsOfSymfony/FOSElasticaBundle/graphs/contributors>
*
* 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 <oleg.andreyev@intexsys.lv>
*/
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());
}
}

View file

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

View file

@ -1,18 +0,0 @@
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<parameters>
<parameter key="fos_elastica.populate_listener.class">FOS\ElasticaBundle\EventListener\PopulateListener</parameter>
</parameters>
<services>
<service id="fos_elastica.populate_listener" class="%fos_elastica.populate_listener.class%">
<tag name="kernel.event_listener" event="elastica.index.index_pre_populate" method="preIndexPopulate"/>
<tag name="kernel.event_listener" event="elastica.index.index_post_populate" method="postIndexPopulate"/>
<argument type="service" id="fos_elastica.resetter"/>
</service>
</services>
</container>

View file

@ -1,79 +0,0 @@
<?php
namespace FOS\ElasticaBundle\Tests\EventListener;
use FOS\ElasticaBundle\Event\PopulateEvent;
use FOS\ElasticaBundle\EventListener\PopulateListener;
use PHPUnit_Framework_MockObject_MockObject;
class PopulateListenerTest extends \PHPUnit_Framework_TestCase
{
/**
* @var PHPUnit_Framework_MockObject_MockObject
*/
private $resetter;
/**
* @var PopulateListener
*/
private $listener;
protected function setUp()
{
$this->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);
}
}