From cf85bfbb4f9b289e8e786ca571f64348dfa17718 Mon Sep 17 00:00:00 2001 From: Magnus Eriksson Date: Tue, 19 May 2015 15:17:42 +0200 Subject: [PATCH] Initial commit --- .gitignore | 2 ++ LICENSE | 21 ++++++++++++ README.md | 3 ++ composer.json | 20 +++++++++++ src/Config.php | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 138 insertions(+) create mode 100755 .gitignore create mode 100755 LICENSE create mode 100755 README.md create mode 100755 composer.json create mode 100755 src/Config.php diff --git a/.gitignore b/.gitignore new file mode 100755 index 0000000..dfd6caa --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/vendor +composer.lock \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100755 index 0000000..981c174 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2015 Magnus Eriksson + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100755 index 0000000..1b286b3 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# A small config library + +> Documentation is coming soon. \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100755 index 0000000..5c43a4c --- /dev/null +++ b/composer.json @@ -0,0 +1,20 @@ +{ + "name": "maer/config", + "description": "A small config library", + "license": "MIT", + "authors": [ + { + "name": "Magnus Eriksson", + "email": "mange@reloop.se" + } + ], + "autoload": { + "psr-4": { + "Maer\\Config\\": "src/" + } + }, + "minimum-stability": "stable", + "require": { + "php": ">=5.3.0" + } +} diff --git a/src/Config.php b/src/Config.php new file mode 100755 index 0000000..fb8934a --- /dev/null +++ b/src/Config.php @@ -0,0 +1,92 @@ +load($files); + } + } + + + /** + * {@inheritdoc} + */ + public function get($key = null, $default = null) + { + if (!$key) { + // No segments? Then return the complete cache + return $this->conf; + } + + $conf =& $this->conf; + + foreach(explode('.', $key) as $segment) { + + if (!array_key_exists($segment, $conf)) { + return $default; + } + + $conf =& $conf[$segment]; + + } + + return $conf; + } + + + /** + * {@inheritdoc} + */ + public function set($key, $value) + { + $conf =& $this->conf; + + $segments = explode('.', $key); + + while (count($segments) > 1) + { + $segment = array_shift($segments); + if ( ! isset($conf[$segment]) || ! is_array($conf[$segment])) { + $conf[$segment] = array(); + } + $conf =& $conf[$segment]; + } + + return $conf[array_shift($segments)] = $value; + } + + + /** + * {@inheritdoc} + */ + public function load($files, $forceReload = false) + { + if (!is_array($files)) { + // Make it an array so we can use the same code + $files = array($files); + } + + foreach($files as $file) { + + if ((array_key_exists($file, $this->files) && !$forceReload) + || !is_file($file)) { + // It's already loaded, or doesn't exist, so let's skip it + continue; + } + + $conf = include $file; + + if (is_array($conf)) { + // We're only interested if it is an array + $this->conf = array_replace_recursive($this->conf, $conf); + $this->files[$file] = true; + } + } + } + +} \ No newline at end of file