From a8b918af35f4be340a76c31a7826a418f2b3029c Mon Sep 17 00:00:00 2001 From: Mo Alsharaf Date: Sun, 11 Sep 2022 22:32:37 +1200 Subject: [PATCH] Use environment variable to set subsite_id in YML --- docs/en/configuration.md | 2 +- .../DataObjectDocumentInterface.php | 2 +- src/Service/IndexConfiguration.php | 28 +++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/docs/en/configuration.md b/docs/en/configuration.md index c6d4d9a..89d9a74 100644 --- a/docs/en/configuration.md +++ b/docs/en/configuration.md @@ -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 diff --git a/src/Interfaces/DataObjectDocumentInterface.php b/src/Interfaces/DataObjectDocumentInterface.php index 26985f3..f310e4d 100644 --- a/src/Interfaces/DataObjectDocumentInterface.php +++ b/src/Interfaces/DataObjectDocumentInterface.php @@ -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 { diff --git a/src/Service/IndexConfiguration.php b/src/Service/IndexConfiguration.php index 5d309a6..b80363c 100644 --- a/src/Service/IndexConfiguration.php +++ b/src/Service/IndexConfiguration.php @@ -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; @@ -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; } @@ -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; + } + }