Skip to content

Commit

Permalink
Use environment variable to set subsite_id in YML
Browse files Browse the repository at this point in the history
  • Loading branch information
satrun77 committed Sep 11, 2022
1 parent 9529cba commit 98a5286
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/Service/IndexConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SilverStripe\SearchService\Service;

use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Environment;
use SilverStripe\Core\Extensible;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\SearchService\Interfaces\DocumentInterface;
Expand Down Expand Up @@ -95,6 +96,11 @@ public function getIndexes(): array
{
$indexes = $this->config()->get('indexes');

// Convert environment variable defined in YML config to their value
array_walk($indexes, function (array &$configuration) {
$configuration = $this->environmentVariableToValue($configuration);
});

if (!$this->onlyIndexes) {
return $indexes;
}
Expand Down Expand Up @@ -267,4 +273,25 @@ public function getFieldsForIndex(string $index): array
return $fields;
}

/**
* For every configuration item if value is environment variable then convert it to its value
*/
protected function environmentVariableToValue(array $configuration): array
{
foreach ($configuration as $name => $value) {
if (!is_string($value)) {
continue;
}

$environmentValue = Environment::getEnv($value);
if (!$environmentValue) {
continue;
}

$configuration[$name] = $environmentValue;
}

return $configuration;
}

}

0 comments on commit 98a5286

Please sign in to comment.