Skip to content

Commit

Permalink
Initial implementation of index template support FriendsOfSymfony#916
Browse files Browse the repository at this point in the history
  • Loading branch information
dbalabka committed Aug 5, 2015
1 parent db6b9ff commit bbb98b3
Show file tree
Hide file tree
Showing 23 changed files with 691 additions and 98 deletions.
2 changes: 2 additions & 0 deletions Command/PopulateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
throw new \InvalidArgumentException('Cannot specify type option without an index.');
}

$this->resetter->resetAllTemplates();

if (null !== $index) {
if (null !== $type) {
$this->populateIndexType($output, $index, $type, $reset, $options);
Expand Down
3 changes: 3 additions & 0 deletions Command/ResetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,16 @@ protected function execute(InputInterface $input, OutputInterface $output)

if (null !== $type) {
$output->writeln(sprintf('<info>Resetting</info> <comment>%s/%s</comment>', $index, $type));
$this->resetter->resetAllTemplates();
$this->resetter->resetIndexType($index, $type);
} else {
$indexes = null === $index
? array_keys($this->indexManager->getAllIndexes())
: array($index)
;

$this->resetter->resetAllTemplates();

foreach ($indexes as $index) {
$output->writeln(sprintf('<info>Resetting</info> <comment>%s</comment>', $index));
$this->resetter->resetIndex($index, false, $force);
Expand Down
39 changes: 36 additions & 3 deletions Configuration/ConfigManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,22 @@ class ConfigManager implements ManagerInterface
private $indexes = array();

/**
* @param Source\SourceInterface[] $sources
* @var IndexTemplateConfig[]
*/
public function __construct(array $sources)
private $indexTemplates = array();

/**
* @param Source\SourceInterface[] $indexSources
* @param Source\SourceInterface[] $indexTemplateSources
*/
public function __construct(array $indexSources, array $indexTemplateSources)
{
foreach ($sources as $source) {
foreach ($indexSources as $source) {
$this->indexes = array_merge($source->getConfiguration(), $this->indexes);
}
foreach ($indexTemplateSources as $source) {
$this->indexTemplates = array_merge($source->getConfiguration(), $this->indexTemplates);
}
}

public function getIndexConfiguration($indexName)
Expand All @@ -40,11 +49,30 @@ public function getIndexConfiguration($indexName)
return $this->indexes[$indexName];
}

/**
* @param string $indexTemplateName
*
* @return IndexTemplateConfig
*/
public function getIndexTemplateConfiguration($indexTemplateName)
{
if (!$this->hasIndexTemplateConfiguration($indexTemplateName)) {
throw new \InvalidArgumentException(sprintf('Index template with name "%s" is not configured.', $indexTemplateName));
}

return $this->indexTemplates[$indexTemplateName];
}

public function getIndexNames()
{
return array_keys($this->indexes);
}

public function getIndexTemplatesNames()
{
return array_keys($this->indexTemplates);
}

public function getTypeConfiguration($indexName, $typeName)
{
$index = $this->getIndexConfiguration($indexName);
Expand All @@ -61,4 +89,9 @@ public function hasIndexConfiguration($indexName)
{
return isset($this->indexes[$indexName]);
}

public function hasIndexTemplateConfiguration($indexTemplateName)
{
return isset($this->indexTemplates[$indexTemplateName]);
}
}
79 changes: 1 addition & 78 deletions Configuration/IndexConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,8 @@

namespace FOS\ElasticaBundle\Configuration;

class IndexConfig
class IndexConfig extends IndexConfigAbstract
{
/**
* The name of the index for ElasticSearch.
*
* @var string
*/
private $elasticSearchName;

/**
* The internal name of the index. May not be the same as the name used in ElasticSearch,
* especially if aliases are enabled.
*
* @var string
*/
private $name;

/**
* An array of settings sent to ElasticSearch when creating the index.
*
* @var array
*/
private $settings;

/**
* All types that belong to this index.
*
* @var TypeConfig[]
*/
private $types;

/**
* Indicates if the index should use an alias, allowing an index repopulation to occur
* without overwriting the current index.
Expand All @@ -66,54 +37,6 @@ public function __construct($name, array $types, array $config)
$this->useAlias = isset($config['useAlias']) ? $config['useAlias'] : false;
}

/**
* @return string
*/
public function getElasticSearchName()
{
return $this->elasticSearchName;
}

/**
* @return string
*/
public function getName()
{
return $this->name;
}

/**
* @return array
*/
public function getSettings()
{
return $this->settings;
}

/**
* @param string $typeName
*
* @return TypeConfig
*
* @throws \InvalidArgumentException
*/
public function getType($typeName)
{
if (!array_key_exists($typeName, $this->types)) {
throw new \InvalidArgumentException(sprintf('Type "%s" does not exist on index "%s"', $typeName, $this->name));
}

return $this->types[$typeName];
}

/**
* @return \FOS\ElasticaBundle\Configuration\TypeConfig[]
*/
public function getTypes()
{
return $this->types;
}

/**
* @return boolean
*/
Expand Down
96 changes: 96 additions & 0 deletions Configuration/IndexConfigAbstract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php
/*
* This file is part of the OpCart software.
*
* (c) 2015, OpticsPlanet, Inc
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\ElasticaBundle\Configuration;

/**
* Index configuration abstract class
*
* @author Dmitry Balabka <[email protected]>
*/
class IndexConfigAbstract
{
/**
* The name of the index for ElasticSearch.
*
* @var string
*/
protected $elasticSearchName;

/**
* The internal name of the index. May not be the same as the name used in ElasticSearch,
* especially if aliases are enabled.
*
* @var string
*/
protected $name;

/**
* An array of settings sent to ElasticSearch when creating the index.
*
* @var array
*/
protected $settings;

/**
* All types that belong to this index.
*
* @var TypeConfig[]
*/
protected $types;

/**
* @return string
*/
public function getElasticSearchName()
{
return $this->elasticSearchName;
}

/**
* @return string
*/
public function getName()
{
return $this->name;
}

/**
* @return array
*/
public function getSettings()
{
return $this->settings;
}

/**
* @param string $typeName
*
* @return TypeConfig
*
* @throws \InvalidArgumentException
*/
public function getType($typeName)
{
if (!array_key_exists($typeName, $this->types)) {
throw new \InvalidArgumentException(sprintf('Type "%s" does not exist on index "%s"', $typeName, $this->name));
}

return $this->types[$typeName];
}

/**
* @return \FOS\ElasticaBundle\Configuration\TypeConfig[]
*/
public function getTypes()
{
return $this->types;
}
}
47 changes: 47 additions & 0 deletions Configuration/IndexTemplateConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace FOS\ElasticaBundle\Configuration;

/**
* Index template configuration class
*
* @author Dmitry Balabka <[email protected]>
*/
class IndexTemplateConfig extends IndexConfigAbstract
{
/**
* Index name pattern
*
* @var string
*/
private $template;

/**
* Constructor expects an array as generated by the Container Configuration builder.
*
* @param string $name
* @param TypeConfig[] $types
* @param array $config
*/
public function __construct($name, array $types, array $config)
{
$this->elasticSearchName = isset($config['elasticSearchName']) ? $config['elasticSearchName'] : $name;
$this->name = $name;
$this->settings = isset($config['settings']) ? $config['settings'] : array();
if (!isset($config['template'])) {
throw new \InvalidArgumentException('Index template value must be set');
}
$this->template = $config['template'];
$this->types = $types;
}

/**
* Gets index name pattern
*
* @return string
*/
public function getTemplate()
{
return $this->template;
}
}
Loading

0 comments on commit bbb98b3

Please sign in to comment.