From 8a64c4298bfc34eeaf05e5edfd077aaa38668c70 Mon Sep 17 00:00:00 2001 From: K Widholm <279278+apotek@users.noreply.github.com> Date: Mon, 12 Aug 2024 18:03:02 -0400 Subject: [PATCH] Standardize yaml writing and allow for it to be configurable (#215) * Standardize yaml writing and allow for it to be configurable Resolves #214 --- .../Commands/DevelopmentModeCommands.php | 2 +- src/Robo/Plugin/Commands/ToolingCommands.php | 2 +- src/Robo/Plugin/Traits/RoboConfigTrait.php | 65 +++++++++++++++++++ src/Robo/Plugin/Traits/SitesConfigTrait.php | 4 +- 4 files changed, 70 insertions(+), 3 deletions(-) diff --git a/src/Robo/Plugin/Commands/DevelopmentModeCommands.php b/src/Robo/Plugin/Commands/DevelopmentModeCommands.php index 9f2ea72..4106b4c 100644 --- a/src/Robo/Plugin/Commands/DevelopmentModeCommands.php +++ b/src/Robo/Plugin/Commands/DevelopmentModeCommands.php @@ -390,7 +390,7 @@ public function frontendDevEnable(string $siteDir = 'default', array $opts = ['y 'auto_reload' => true, 'cache' => false, ]; - file_put_contents($this->devServicesPath, Yaml::dump($devServices)); + $this->writeYaml($this->devServicesPath, $devServices); $this->say("disabling render and dynamic_page_cache in settings.local.php."); // https://github.com/consolidation/robo/issues/1059#issuecomment-967732068 $result = $this->collectionBuilder() diff --git a/src/Robo/Plugin/Commands/ToolingCommands.php b/src/Robo/Plugin/Commands/ToolingCommands.php index 85ab034..15298ce 100644 --- a/src/Robo/Plugin/Commands/ToolingCommands.php +++ b/src/Robo/Plugin/Commands/ToolingCommands.php @@ -107,7 +107,7 @@ protected function updateRoboConfig(string $key, string $value): void $roboConfigPath = "$this->cwd/robo.yml"; $roboConfig = Yaml::parse((string) file_get_contents($roboConfigPath)); $roboConfig[$key] = $value; - file_put_contents($roboConfigPath, Yaml::dump($roboConfig)); + $this->writeYaml($roboConfigPath, $roboConfig); } /** diff --git a/src/Robo/Plugin/Traits/RoboConfigTrait.php b/src/Robo/Plugin/Traits/RoboConfigTrait.php index 8c31d49..d43178d 100644 --- a/src/Robo/Plugin/Traits/RoboConfigTrait.php +++ b/src/Robo/Plugin/Traits/RoboConfigTrait.php @@ -4,6 +4,7 @@ use Robo\Robo; use Robo\Exception\TaskException; +use Symfony\Component\Yaml\Yaml; use Usher\Robo\Plugin\Enums\ConfigTypes; /** @@ -11,6 +12,47 @@ */ trait RoboConfigTrait { + protected array $defaultYamlConf = [ + 'inline_level' => 10, + 'indent' => 2, + 'dump_bits' => Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE | + Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE | + Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK | + Yaml::DUMP_NUMERIC_KEY_AS_STRING | + Yaml::DUMP_OBJECT_AS_MAP, + ]; + + /** + * Get the default YAML configuration. + */ + protected function getDefaultYamlConf(): array + { + return $this->defaultYamlConf; + } + + /** + * Get an optional Robo configuration array. + * + * @param string $key + * The key of the configuration to load. + * + * @throws \Robo\Exception\TaskException + */ + protected function getOptionalRoboConfigArrayFor(string $key): mixed + { + $configValue = Robo::config()->get($key); + if (is_null($configValue)) { + $configValue = []; + } else { + $this->validateRoboConfigValueMatchesType( + configValue: $configValue, + expectedType: ConfigTypes::array, + key: $key, + ); + } + return $configValue; + } + /** * Get Robo configuration array value. * @@ -98,4 +140,27 @@ private function validateRoboConfigValueMatchesType( } return true; } + + /** + * Write data as yaml to a file. + * + * @param string $filepath + * The file to write the data to. + * @param string[] $data + * An array of data to be written out as yaml. + */ + protected function writeYaml(string $filepath, array $data): void + { + $yaml_conf = $this->getOptionalRoboConfigArrayFor('yaml_conf'); + $yaml_conf = [...$this->getDefaultYamlConf(), ...$yaml_conf]; + file_put_contents( + $filepath, + "---\n" . Yaml::dump( + $data, + $yaml_conf['inline_level'], + $yaml_conf['indent'], + $yaml_conf['dump_bits'], + ) + ); + } } diff --git a/src/Robo/Plugin/Traits/SitesConfigTrait.php b/src/Robo/Plugin/Traits/SitesConfigTrait.php index be0210c..098f28b 100644 --- a/src/Robo/Plugin/Traits/SitesConfigTrait.php +++ b/src/Robo/Plugin/Traits/SitesConfigTrait.php @@ -10,6 +10,8 @@ */ trait SitesConfigTrait { + use RoboConfigTrait; + /** * Filename for a site's configuration file. * @@ -102,7 +104,7 @@ public function getSiteConfigItem(string $key, string $siteName = 'default', boo protected function writeSitesConfig(array $sitesConfig): void { ksort($sitesConfig); - file_put_contents($this->sitesConfigFile, Yaml::dump($sitesConfig)); + $this->writeYaml($this->sitesConfigFile, $sitesConfig); } /**