Skip to content

Commit

Permalink
Added index-builder (#211)
Browse files Browse the repository at this point in the history
* added index-builder

* added output
  • Loading branch information
wachterjohannes authored and trickreich committed Jun 29, 2017
1 parent 617aafb commit 683c86a
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
74 changes: 74 additions & 0 deletions Builder/ArticleIndexBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

/*
* This file is part of Sulu.
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ArticleBundle\Builder;

use ONGR\ElasticsearchBundle\Service\Manager;
use Sulu\Bundle\CoreBundle\Build\SuluBuilder;

/**
* Builder for article-index.
*/
class ArticleIndexBuilder extends SuluBuilder
{
/**
* {@inheritdoc}
*/
public function getName()
{
return 'article_index';
}

/**
* {@inheritdoc}
*/
public function getDependencies()
{
return ['cache'];
}

/**
* {@inheritdoc}
*/
public function build()
{
$this->buildForManager($this->container->get('es.manager.live'), $this->input->getOption('destroy'));
$this->buildForManager($this->container->get('es.manager.default'), $this->input->getOption('destroy'));
}

/**
* Build index for given manager.
*
* If index not exists - it will be created.
* If index exists and destroy flag is true - drop and create index.
* Else do nothing.
*
* @param Manager $manager
* @param bool $destroy
*/
private function buildForManager(Manager $manager, $destroy)
{
$name = $manager->getName();
if (!$manager->indexExists()) {
$this->output->writeln(sprintf('Create index for "<comment>%s</comment>" manager.', $name));
$manager->createIndex();

return;
}

if (!$destroy) {
return;
}

$this->output->writeln(sprintf('Drop and create index for "<comment>%s</comment>" manager.', $name));
$manager->dropAndCreateIndex();
}
}
25 changes: 25 additions & 0 deletions DependencyInjection/SuluArticleExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,31 @@ public function prepend(ContainerBuilder $container)
]
);
}

if ($container->hasExtension('massive_build')) {
$container->prependExtensionConfig(
'massive_build',
[
'targets' => [
'prod' => [
'dependencies' => [
'article_index' => [],
],
],
'dev' => [
'dependencies' => [
'article_index' => [],
],
],
'maintain' => [
'dependencies' => [
'article_index' => [],
],
],
],
]
);
}
}

/**
Expand Down
4 changes: 4 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -327,5 +327,9 @@

<tag name="twig.extension"/>
</service>

<service id="sulu_article.builder.index" class="Sulu\Bundle\ArticleBundle\Builder\ArticleIndexBuilder">
<tag name="massive_build.builder" />
</service>
</services>
</container>

0 comments on commit 683c86a

Please sign in to comment.