From 57995ad3b65ddb84310429616dcb247661da1913 Mon Sep 17 00:00:00 2001 From: TiMESPLiNTER Date: Sat, 5 Nov 2016 10:24:05 +0100 Subject: [PATCH 1/5] Add support for parameters --- src/Config.php | 37 +++++++++++++++++++++++++++++++------ src/DataConfig.php | 7 +++++++ 2 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 src/DataConfig.php diff --git a/src/Config.php b/src/Config.php index 11be5bd..c3fa70b 100644 --- a/src/Config.php +++ b/src/Config.php @@ -3,6 +3,7 @@ namespace Noodlehaus; use Noodlehaus\Exception\FileNotFoundException; +use Noodlehaus\Exception\ParseException; use Noodlehaus\Exception\UnsupportedFormatException; use Noodlehaus\Exception\EmptyDirectoryException; @@ -37,19 +38,18 @@ class Config extends AbstractConfig * * @return Config */ - public static function load($path) + public static function load($path, array $parameters = []) { - return new static($path); + return new static($path, $parameters); } /** * Loads a supported configuration file format. * - * @param string|array $path - * - * @throws EmptyDirectoryException If `$path` is an empty directory + * @param string|array $path + * @param array $parameters */ - public function __construct($path) + public function __construct($path, array $parameters = []) { $paths = $this->getValidPath($path); $this->data = array(); @@ -69,9 +69,34 @@ public function __construct($path) $this->data = array_replace_recursive($this->data, (array) $parser->parse($path)); } + $this->replaceParameters($this->data, new Config($parameters)); + parent::__construct($this->data); } + /** + * @param array $data + * @param ConfigInterface $parameters + */ + protected function replaceParameters(array &$data, ConfigInterface $parameters) + { + foreach ($data as &$value) { + if (is_array($value)) { + $this->replaceParameters($value, $parameters); + continue; + } elseif (is_string($value)) { + $value = preg_replace_callback('/%([^%]+)%/', function ($m) use ($parameters) { + if (null === $parameterValue = $parameters->get($m[1])) { + throw new ParseException( + sprintf('Parameter "%s" does not exist', $m[1]) + ); + } + return $parameterValue; + }, $value); + } + } + } + /** * Gets a parser for a given file extension * diff --git a/src/DataConfig.php b/src/DataConfig.php new file mode 100644 index 0000000..a33337e --- /dev/null +++ b/src/DataConfig.php @@ -0,0 +1,7 @@ + Date: Sat, 5 Nov 2016 10:27:12 +0100 Subject: [PATCH 2/5] Do not parse config for parameters if none are given --- src/Config.php | 6 ++++-- src/DataConfig.php | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Config.php b/src/Config.php index c3fa70b..11f29bb 100644 --- a/src/Config.php +++ b/src/Config.php @@ -49,7 +49,7 @@ public static function load($path, array $parameters = []) * @param string|array $path * @param array $parameters */ - public function __construct($path, array $parameters = []) + public function __construct($path, array $parameters = null) { $paths = $this->getValidPath($path); $this->data = array(); @@ -69,7 +69,9 @@ public function __construct($path, array $parameters = []) $this->data = array_replace_recursive($this->data, (array) $parser->parse($path)); } - $this->replaceParameters($this->data, new Config($parameters)); + if (null !== $parameters) { + $this->replaceParameters($this->data, new DataConfig($parameters)); + } parent::__construct($this->data); } diff --git a/src/DataConfig.php b/src/DataConfig.php index a33337e..522a9f5 100644 --- a/src/DataConfig.php +++ b/src/DataConfig.php @@ -2,6 +2,6 @@ namespace Noodlehaus; -class Config extends AbstractConfig +class DataConfig extends AbstractConfig { } From 7e1c8fcf6f6c99fb3fef4bd509cc49f6abeb5537 Mon Sep 17 00:00:00 2001 From: TiMESPLiNTER Date: Sat, 5 Nov 2016 10:29:24 +0100 Subject: [PATCH 3/5] Remove unnecessary continue statement --- src/Config.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Config.php b/src/Config.php index 11f29bb..53957fb 100644 --- a/src/Config.php +++ b/src/Config.php @@ -83,10 +83,9 @@ public function __construct($path, array $parameters = null) protected function replaceParameters(array &$data, ConfigInterface $parameters) { foreach ($data as &$value) { - if (is_array($value)) { + if (true === is_array($value)) { $this->replaceParameters($value, $parameters); - continue; - } elseif (is_string($value)) { + } elseif (true === is_string($value)) { $value = preg_replace_callback('/%([^%]+)%/', function ($m) use ($parameters) { if (null === $parameterValue = $parameters->get($m[1])) { throw new ParseException( From a6da99b61961ef746be6af2339a4a16238e19f99 Mon Sep 17 00:00:00 2001 From: TiMESPLiNTER Date: Sat, 5 Nov 2016 10:32:03 +0100 Subject: [PATCH 4/5] Do not throw exception if parameter is missing because else any %% encapsulated string which is not a parameter will make the config fail to load --- src/Config.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Config.php b/src/Config.php index 53957fb..f354bf3 100644 --- a/src/Config.php +++ b/src/Config.php @@ -88,9 +88,7 @@ protected function replaceParameters(array &$data, ConfigInterface $parameters) } elseif (true === is_string($value)) { $value = preg_replace_callback('/%([^%]+)%/', function ($m) use ($parameters) { if (null === $parameterValue = $parameters->get($m[1])) { - throw new ParseException( - sprintf('Parameter "%s" does not exist', $m[1]) - ); + return $m[0]; } return $parameterValue; }, $value); From 3c235cf88d7847051b5878ed05e055249a2c76a8 Mon Sep 17 00:00:00 2001 From: TiMESPLiNTER Date: Sat, 5 Nov 2016 11:03:37 +0100 Subject: [PATCH 5/5] Set default value to null --- src/Config.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Config.php b/src/Config.php index f354bf3..4687971 100644 --- a/src/Config.php +++ b/src/Config.php @@ -34,11 +34,12 @@ class Config extends AbstractConfig /** * Static method for loading a Config instance. * - * @param string|array $path + * @param string|array $path + * @param array $parameters * * @return Config */ - public static function load($path, array $parameters = []) + public static function load($path, array $parameters = null) { return new static($path, $parameters); }