2011-04-10 23:08:51 +02:00
|
|
|
<?php
|
|
|
|
|
2013-03-27 18:44:06 +01:00
|
|
|
namespace FOS\ElasticaBundle;
|
2011-04-10 23:08:51 +02:00
|
|
|
|
2013-04-22 13:20:10 +02:00
|
|
|
use Elastica\Index;
|
|
|
|
|
2011-04-10 23:08:51 +02:00
|
|
|
class IndexManager
|
|
|
|
{
|
2012-03-02 05:32:04 +01:00
|
|
|
protected $indexesByName;
|
|
|
|
protected $defaultIndexName;
|
2011-04-10 23:08:51 +02:00
|
|
|
|
2012-03-02 05:32:04 +01:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
2013-03-02 12:23:44 +01:00
|
|
|
* @param array $indexesByName
|
2013-04-22 13:20:10 +02:00
|
|
|
* @param Index $defaultIndex
|
2012-03-02 05:32:04 +01:00
|
|
|
*/
|
2013-04-22 13:20:10 +02:00
|
|
|
public function __construct(array $indexesByName, Index $defaultIndex)
|
2011-04-10 23:08:51 +02:00
|
|
|
{
|
2012-03-02 05:32:04 +01:00
|
|
|
$this->indexesByName = $indexesByName;
|
2012-08-10 10:40:01 +02:00
|
|
|
$this->defaultIndexName = $defaultIndex->getName();
|
2011-04-10 23:08:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets all registered indexes
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getAllIndexes()
|
|
|
|
{
|
2012-03-02 05:32:04 +01:00
|
|
|
return $this->indexesByName;
|
2011-04-10 23:08:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-04-13 00:15:48 +02:00
|
|
|
* Gets an index by its name
|
2011-04-10 23:08:51 +02:00
|
|
|
*
|
2012-03-02 05:32:04 +01:00
|
|
|
* @param string $name Index to return, or the default index if null
|
2013-04-22 13:20:10 +02:00
|
|
|
* @return Index
|
2013-03-02 12:23:44 +01:00
|
|
|
* @throws \InvalidArgumentException if no index exists for the given name
|
2012-03-02 05:32:04 +01:00
|
|
|
*/
|
|
|
|
public function getIndex($name = null)
|
2011-04-10 23:08:51 +02:00
|
|
|
{
|
2012-03-02 05:32:04 +01:00
|
|
|
if (null === $name) {
|
|
|
|
$name = $this->defaultIndexName;
|
2011-04-13 00:15:48 +02:00
|
|
|
}
|
2012-03-02 05:32:04 +01:00
|
|
|
|
|
|
|
if (!isset($this->indexesByName[$name])) {
|
|
|
|
throw new \InvalidArgumentException(sprintf('The index "%s" does not exist', $name));
|
2011-04-10 23:08:51 +02:00
|
|
|
}
|
2011-04-13 00:15:48 +02:00
|
|
|
|
2012-03-02 05:32:04 +01:00
|
|
|
return $this->indexesByName[$name];
|
2011-04-13 00:15:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the default index
|
|
|
|
*
|
2013-04-22 13:20:10 +02:00
|
|
|
* @return Index
|
2012-03-02 05:32:04 +01:00
|
|
|
*/
|
2011-04-13 00:15:48 +02:00
|
|
|
public function getDefaultIndex()
|
|
|
|
{
|
2012-03-02 05:32:04 +01:00
|
|
|
return $this->getIndex($this->defaultIndexName);
|
2011-04-10 23:08:51 +02:00
|
|
|
}
|
|
|
|
}
|