Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
John Le Drew
committed
Aug 27, 2012
1 parent
9f0665c
commit 380475c
Showing
2 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |