From f76285a43c95fd3f795b2f5713d2efbf5fa101ee Mon Sep 17 00:00:00 2001 From: Stephan Huber Date: Thu, 11 Nov 2021 18:37:58 +0100 Subject: [PATCH] Add protected properties --- docs/configuration.md | 1 + docs/local-overrides.md | 10 +++++++++ src/Configuration/ConfigurationService.php | 24 +++++++++++++++++++--- src/Utilities/Utilities.php | 2 +- 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 74707597..1bd2abef 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -173,6 +173,7 @@ This will print all host configuration for the host `staging`. - source.factorial.io:2222 ``` They can be overridden on a per host-basis. +* `protectedProperties` a list of properties which wont be affected by a override.yaml file. See [local overrides](local-overrides.md) ### Configuration for the local-method diff --git a/docs/local-overrides.md b/docs/local-overrides.md index c655f723..c09f486f 100644 --- a/docs/local-overrides.md +++ b/docs/local-overrides.md @@ -43,3 +43,13 @@ dockerNetRcFile: /home/user/.netrc Another possibility is to place a socalled override file side by side to the original ymal-file. Name it the same as the original file, and add `.override` before the file extension, e.g. `fabfile.yml` becomes `fabfile.override.yml`. The data of the override will be merged with the data of the original file. No inheritance or other advanced features are supported. + +## Prevent overriding of certain values + +3.7.1 supports a new property on the root level of the fabfile called `protectedProperties`. These properties will be prevented for being overridden: + +```yaml +protectedOverrides: + - dockerHosts.mbb.environment +``` +If your override-file wants to override `dockerHosts > mbb > environment` phab will prevent this and restore the original value as in the fabfile. diff --git a/src/Configuration/ConfigurationService.php b/src/Configuration/ConfigurationService.php index 38d94054..ded305a8 100644 --- a/src/Configuration/ConfigurationService.php +++ b/src/Configuration/ConfigurationService.php @@ -303,9 +303,27 @@ public function getFabfileLocation() return $this->fabfileLocation; } - public function mergeData(array $data, array $override_data): array - { - return Utilities::mergeData($data, $override_data); + public function mergeData( + array $data, + array $override_data, + $protected_properties_key = 'protectedProperties' + ): array { + $properties_to_restore = []; + if ($protected_properties = $data[$protected_properties_key] ?? false) { + if (!is_array($protected_properties)) { + $protected_properties = [ $protected_properties ]; + } + foreach ($protected_properties as $prop) { + $properties_to_restore[$prop] = Utilities::getProperty($data, $prop); + } + } + + $data = Utilities::mergeData($data, $override_data); + + foreach ($properties_to_restore as $prop => $value) { + Utilities::setProperty($data, $prop, $value); + } + return $data; } private function applyDefaults(array $data, array $defaults, array $disallowed_keys = []): array diff --git a/src/Utilities/Utilities.php b/src/Utilities/Utilities.php index 17d40a2d..ce587878 100644 --- a/src/Utilities/Utilities.php +++ b/src/Utilities/Utilities.php @@ -12,7 +12,7 @@ class Utilities { - const FALLBACK_VERSION = '3.7.0'; + const FALLBACK_VERSION = '3.7.1'; const COMBINED_ARGUMENTS = 'combined'; const UNNAMED_ARGUMENTS = 'unnamedArguments';