adding populate events
This commit is contained in:
parent
7fac93ff8b
commit
303af508b2
|
@ -2,15 +2,16 @@
|
|||
|
||||
namespace FOS\ElasticaBundle\Command;
|
||||
|
||||
use FOS\ElasticaBundle\Event\PopulateEvent;
|
||||
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
|
||||
use Symfony\Component\Console\Helper\DialogHelper;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use FOS\ElasticaBundle\IndexManager;
|
||||
use FOS\ElasticaBundle\Index\IndexManager;
|
||||
use FOS\ElasticaBundle\Provider\ProviderRegistry;
|
||||
use FOS\ElasticaBundle\Resetter;
|
||||
use FOS\ElasticaBundle\Provider\ProviderInterface;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
|
||||
/**
|
||||
* Populate the search index
|
||||
|
@ -28,9 +29,9 @@ class PopulateCommand extends ContainerAwareCommand
|
|||
private $providerRegistry;
|
||||
|
||||
/**
|
||||
* @var Resetter
|
||||
* @var EventDispatcherInterface
|
||||
*/
|
||||
private $resetter;
|
||||
private $eventDispatcher;
|
||||
|
||||
/**
|
||||
* @see Symfony\Component\Console\Command\Command::configure()
|
||||
|
@ -57,7 +58,7 @@ class PopulateCommand extends ContainerAwareCommand
|
|||
{
|
||||
$this->indexManager = $this->getContainer()->get('fos_elastica.index_manager');
|
||||
$this->providerRegistry = $this->getContainer()->get('fos_elastica.provider_registry');
|
||||
$this->resetter = $this->getContainer()->get('fos_elastica.resetter');
|
||||
$this->eventDispatcher = $this->getContainer()->get('event_dispatcher');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -109,24 +110,12 @@ class PopulateCommand extends ContainerAwareCommand
|
|||
*/
|
||||
private function populateIndex(OutputInterface $output, $index, $reset, $options)
|
||||
{
|
||||
if ($reset) {
|
||||
$output->writeln(sprintf('<info>Resetting</info> <comment>%s</comment>', $index));
|
||||
$this->resetter->resetIndex($index, true);
|
||||
}
|
||||
|
||||
/** @var $providers ProviderInterface[] */
|
||||
$providers = $this->providerRegistry->getIndexProviders($index);
|
||||
|
||||
foreach ($providers as $type => $provider) {
|
||||
$loggerClosure = function($message) use ($output, $index, $type) {
|
||||
$output->writeln(sprintf('<info>Populating</info> %s/%s, %s', $index, $type, $message));
|
||||
};
|
||||
|
||||
$provider->populate($loggerClosure, $options);
|
||||
}
|
||||
$this->populate($output, $providers, $index, null, $reset, $options);
|
||||
|
||||
$output->writeln(sprintf('<info>Refreshing</info> <comment>%s</comment>', $index));
|
||||
$this->resetter->postPopulate($index);
|
||||
$this->indexManager->getIndex($index)->refresh();
|
||||
}
|
||||
|
||||
|
@ -141,19 +130,48 @@ class PopulateCommand extends ContainerAwareCommand
|
|||
*/
|
||||
private function populateIndexType(OutputInterface $output, $index, $type, $reset, $options)
|
||||
{
|
||||
if ($reset) {
|
||||
$output->writeln(sprintf('<info>Resetting</info> <comment>%s/%s</comment>', $index, $type));
|
||||
$this->resetter->resetIndexType($index, $type);
|
||||
}
|
||||
|
||||
$loggerClosure = function($message) use ($output, $index, $type) {
|
||||
$output->writeln(sprintf('<info>Populating</info> %s/%s, %s', $index, $type, $message));
|
||||
};
|
||||
|
||||
$provider = $this->providerRegistry->getProvider($index, $type);
|
||||
$provider->populate($loggerClosure, $options);
|
||||
|
||||
$this->populate($output, array($type => $provider), $index, $type, $reset, $options);
|
||||
|
||||
$output->writeln(sprintf('<info>Refreshing</info> <comment>%s</comment>', $index));
|
||||
$this->indexManager->getIndex($index)->refresh();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param OutputInterface $output
|
||||
* @param ProviderInterface[] $providers
|
||||
* @param string $index
|
||||
* @param string $type
|
||||
* @param boolean $reset
|
||||
* @param array $options
|
||||
*/
|
||||
private function populate(OutputInterface $output, array $providers, $index, $type, $reset, $options)
|
||||
{
|
||||
if ($reset) {
|
||||
if ($type) {
|
||||
$output->writeln(sprintf('<info>Resetting</info> <comment>%s/%s</comment>', $index, $type));
|
||||
} else {
|
||||
$output->writeln(sprintf('<info>Resetting</info> <comment>%s</comment>', $index));
|
||||
}
|
||||
}
|
||||
|
||||
$this->eventDispatcher->dispatch(PopulateEvent::PRE_INDEX_POPULATE, new PopulateEvent($index, $type, $reset, $options));
|
||||
|
||||
foreach ($providers as $providerType => $provider) {
|
||||
$event = new PopulateEvent($index, $providerType, $reset, $options);
|
||||
|
||||
$this->eventDispatcher->dispatch(PopulateEvent::PRE_TYPE_POPULATE, $event);
|
||||
|
||||
$loggerClosure = function($message) use ($output, $index, $providerType) {
|
||||
$output->writeln(sprintf('<info>Populating</info> %s/%s, %s', $index, $providerType, $message));
|
||||
};
|
||||
|
||||
$provider->populate($loggerClosure, $options);
|
||||
|
||||
$this->eventDispatcher->dispatch(PopulateEvent::POST_TYPE_POPULATE, $event);
|
||||
}
|
||||
|
||||
$this->eventDispatcher->dispatch(PopulateEvent::POST_INDEX_POPULATE, new PopulateEvent($index, $type, $reset, $options));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -46,7 +46,7 @@ class FOSElasticaExtension extends Extension
|
|||
return;
|
||||
}
|
||||
|
||||
foreach (array('config', 'index', 'persister', 'provider', 'source', 'transformer') as $basename) {
|
||||
foreach (array('config', 'index', 'persister', 'provider', 'source', 'transformer', 'listener') as $basename) {
|
||||
$loader->load(sprintf('%s.xml', $basename));
|
||||
}
|
||||
|
||||
|
|
93
Event/PopulateEvent.php
Normal file
93
Event/PopulateEvent.php
Normal file
|
@ -0,0 +1,93 @@
|
|||
<?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\Event;
|
||||
|
||||
use Symfony\Component\EventDispatcher\Event;
|
||||
|
||||
/**
|
||||
* Populate Event
|
||||
*
|
||||
* @author Oleg Andreyev <oleg.andreyev@intexsys.lv>
|
||||
*/
|
||||
class PopulateEvent extends Event
|
||||
{
|
||||
const PRE_INDEX_POPULATE = 'elastica.index.index_pre_populate';
|
||||
const POST_INDEX_POPULATE = 'elastica.index.index_post_populate';
|
||||
|
||||
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 bool
|
||||
*/
|
||||
private $reset;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $options;
|
||||
|
||||
/**
|
||||
* @param string $index
|
||||
* @param string $type
|
||||
* @param boolean $reset
|
||||
* @param array $options
|
||||
*/
|
||||
public function __construct($index, $type, $reset, $options)
|
||||
{
|
||||
$this->index = $index;
|
||||
$this->type = $type;
|
||||
$this->reset = $reset;
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getIndex()
|
||||
{
|
||||
return $this->index;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getType()
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return boolean
|
||||
*/
|
||||
public function isReset()
|
||||
{
|
||||
return $this->reset;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getOptions()
|
||||
{
|
||||
return $this->options;
|
||||
}
|
||||
}
|
59
EventListener/PopulateListener.php
Normal file
59
EventListener/PopulateListener.php
Normal file
|
@ -0,0 +1,59 @@
|
|||
<?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());
|
||||
}
|
||||
}
|
18
Resources/config/listener.xml
Normal file
18
Resources/config/listener.xml
Normal file
|
@ -0,0 +1,18 @@
|
|||
<?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>
|
Loading…
Reference in a new issue