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 a8b918a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/en/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ SilverStripe\SearchService\Service\IndexConfiguration:
summary:
property: Summary
content-subsite4:
subsite_id: 4
subsite_id: 4 # or you can use environment variable such as 'NAME_OF_ENVIRONMENT_VARIABLE'
includeClasses:
Page:
<<: *page_defaults
Expand Down
2 changes: 1 addition & 1 deletion src/Interfaces/DataObjectDocumentInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use SilverStripe\ORM\DataObject;

/**
* The contract to indicate that the Elastic Document source its data from Silverstripe DataObject class
* The contract to indicate that the Elastic Document sources its data from Silverstripe DataObject class
*/
interface DataObjectDocumentInterface
{
Expand Down
28 changes: 28 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 its value
array_walk($indexes, function (array &$configuration): void {
$configuration = $this->environmentVariableToValue($configuration);
});

if (!$this->onlyIndexes) {
return $indexes;
}
Expand Down Expand Up @@ -267,4 +273,26 @@ 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 a8b918a

Please sign in to comment.