Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
magnus-eriksson committed May 19, 2015
0 parents commit cf85bfb
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor
composer.lock
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# A small config library

> Documentation is coming soon.
20 changes: 20 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
92 changes: 92 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php namespace Maer\Config;

class Config
{
protected $files = [];
protected $conf = [];

public function __construct(array $files = array())
{
if ($files) {
$this->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;
}
}
}

}

0 comments on commit cf85bfb

Please sign in to comment.