diff --git a/Command/PopulateCommand.php b/Command/PopulateCommand.php
index f17ca4c..89d2ebc 100644
--- a/Command/PopulateCommand.php
+++ b/Command/PopulateCommand.php
@@ -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('Resetting %s', $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('Populating %s/%s, %s', $index, $type, $message));
- };
-
- $provider->populate($loggerClosure, $options);
- }
+ $this->populate($output, $providers, $index, null, $reset, $options);
$output->writeln(sprintf('Refreshing %s', $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('Resetting %s/%s', $index, $type));
- $this->resetter->resetIndexType($index, $type);
- }
-
- $loggerClosure = function($message) use ($output, $index, $type) {
- $output->writeln(sprintf('Populating %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('Refreshing %s', $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('Resetting %s/%s', $index, $type));
+ } else {
+ $output->writeln(sprintf('Resetting %s', $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('Populating %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));
+ }
}
diff --git a/DependencyInjection/FOSElasticaExtension.php b/DependencyInjection/FOSElasticaExtension.php
index 565ba33..705da12 100644
--- a/DependencyInjection/FOSElasticaExtension.php
+++ b/DependencyInjection/FOSElasticaExtension.php
@@ -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));
}
diff --git a/Event/PopulateEvent.php b/Event/PopulateEvent.php
new file mode 100644
index 0000000..9bf9dbb
--- /dev/null
+++ b/Event/PopulateEvent.php
@@ -0,0 +1,93 @@
+
+ *
+ * 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
+ */
+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;
+ }
+}
diff --git a/EventListener/PopulateListener.php b/EventListener/PopulateListener.php
new file mode 100644
index 0000000..a2a0f5a
--- /dev/null
+++ b/EventListener/PopulateListener.php
@@ -0,0 +1,59 @@
+
+ *
+ * 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
+ */
+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());
+ }
+}
diff --git a/Resources/config/listener.xml b/Resources/config/listener.xml
new file mode 100644
index 0000000..6c586bf
--- /dev/null
+++ b/Resources/config/listener.xml
@@ -0,0 +1,18 @@
+
+
+
+
+
+ FOS\ElasticaBundle\EventListener\PopulateListener
+
+
+
+
+
+
+
+
+
+