Merge pull request #179 from creative-commoners/pulls/3.0/env-var-support

NEW Support environment variables for Solr index prefix and suffix
This commit is contained in:
Dylan Wagstaff 2017-12-04 11:53:22 +13:00 committed by GitHub
commit 71d837d7b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 5 deletions

View File

@ -3,6 +3,7 @@
namespace SilverStripe\FullTextSearch\Solr;
use SilverStripe\Control\Director;
use SilverStripe\Core\Environment;
use SilverStripe\FulltextSearch\Search\Indexes\SearchIndex;
use SilverStripe\FullTextSearch\Solr\Services\SolrService;
use SilverStripe\FulltextSearch\Search\Queries\SearchQuery;
@ -108,16 +109,17 @@ abstract class SolrIndex extends SearchIndex
public function getIndexName()
{
$name = get_class($this);
$indexParts = [$name];
if (defined('SS_SOLR_INDEX_PREFIX')) {
$name = SS_SOLR_INDEX_PREFIX . ''. $name;
if ($indexPrefix = Environment::getEnv('SS_SOLR_INDEX_PREFIX')) {
array_unshift($indexParts, $indexPrefix);
}
if (defined('SS_SOLR_INDEX_SUFFIX')) {
$name = $name . '' . SS_SOLR_INDEX_SUFFIX;
if ($indexSuffix = Environment::getEnv('SS_SOLR_INDEX_SUFFIX')) {
$indexParts[] = $indexSuffix;
}
return $name;
return implode($indexParts);
}
public function getTypes()

View File

@ -3,6 +3,7 @@
namespace SilverStripe\FullTextSearch\Tests;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Environment;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Kernel;
use SilverStripe\Dev\SapphireTest;
@ -337,6 +338,22 @@ class SolrIndexTest extends SapphireTest
);
}
public function testGetIndexName()
{
$index = new SolrIndexTest_FakeIndex2;
$this->assertSame(SolrIndexTest_FakeIndex2::class, $index->getIndexName());
}
public function testGetIndexNameWithPrefixAndSuffixFromEnvironment()
{
$index = new SolrIndexTest_FakeIndex2;
Environment::putEnv('SS_SOLR_INDEX_PREFIX="foo_"');
Environment::putEnv('SS_SOLR_INDEX_SUFFIX="_bar"');
$this->assertSame('foo_' . SolrIndexTest_FakeIndex2::class . '_bar', $index->getIndexName());
}
protected function getFakeRawSolrResponse()
{
return new \Apache_Solr_Response(