Skip to content

Commit

Permalink
Initial split from Base
Browse files Browse the repository at this point in the history
  • Loading branch information
John Le Drew committed Aug 27, 2012
1 parent 9f0665c commit 380475c
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/config.php
@@ -0,0 +1,49 @@
<?php
namespace Base\Config;

class Config extends Node {

private $_env;

public function __construct($file,$env = 'shared')
{
$this->_env = $env;

$config = include($file);
$this->_data = $this->getConfigForEnvironment($config);
}

public function setModule($module)
{
if (!isset($this->_data['modules'][$module]['config'])) return;
$config = $this->_data['modules'][$module]['config'];
$config = $this->getConfigForEnvironment($config);
$this->_data = $this->mergeArray($this->_data,$config);
}

private function getConfigForEnvironment($config)
{
$env = $this->_env;
$data = isset($config['shared']) ? $config['shared'] : array();

if (isset($config[$env])) $data = $this->mergeArray($data,$config[$env]);

return $data;
}

private function mergeArray($arr1, $arr2)
{
foreach($arr2 as $key => $value)
{
if(array_key_exists($key, $arr1) && is_array($value)) {
$arr1[$key] = $this->mergeArray($arr1[$key], $arr2[$key]);
}
else {
$arr1[$key] = $value;
}
}

return $arr1;
}

}
58 changes: 58 additions & 0 deletions src/node.php
@@ -0,0 +1,58 @@
<?php
namespace Base\Config;

class Node implements \ArrayAccess {

protected $_data;

public function __construct($data) {
$this->_data = $data;
}

public function __get($name) {
return $this->get($name);
}

public function get($name) {
$val = isset($this->_data[$name]) ? $this->_data[$name] : null;

if ($val == null) return null;
if (is_array($val)) {
if (isset($val[0])) {
return $val;
} else {
return new Node($val);
}
}

return $val;
}

public function has($name) {
return isset($this->_data[$name]);
}

public function __isset($name) {
return $this->has($name);
}

public function getData() {
return $this->_data;
}

public function offsetExists($name) {
return $this->has($name);
}

public function offsetGet($name) {
return $this->get($name);
}

public function offsetSet($name,$value) {
return false;
}

public function offsetUnset($name) {
return false;
}
}

0 comments on commit 380475c

Please sign in to comment.