| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class to load config files |
| 4 |
* |
| 5 |
* @author Pavel Kulbakin <p.kulbakin@gmail.com> |
| 6 |
*/ |
| 7 |
class PMXE_Config implements IteratorAggregate { |
| 8 |
/** |
| 9 |
* Config variables stored |
| 10 |
* @var array |
| 11 |
*/ |
| 12 |
protected $config = array(); |
| 13 |
/** |
| 14 |
* List of loaded files in order to avoid loading same file several times |
| 15 |
* @var array |
| 16 |
*/ |
| 17 |
protected $loaded = array(); |
| 18 |
|
| 19 |
/** |
| 20 |
* Static method to create config instance from file on disc |
| 21 |
* @param string $filePath |
| 22 |
* @param string[optional] $section |
| 23 |
* @return PMXE_Config |
| 24 |
*/ |
| 25 |
public static function createFromFile($filePath, $section = NULL) { |
| 26 |
$config = new self(); |
| 27 |
return $config->loadFromFile($filePath, $section); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Load config file |
| 32 |
* @param string $filePath |
| 33 |
* @param string[optional] $section |
| 34 |
* @return PMXE_Config |
| 35 |
*/ |
| 36 |
public function loadFromFile($filePath, $section = NULL) { |
| 37 |
if ( ! is_null($section)) { |
| 38 |
$this->config[$section] = self::createFromFile($filePath); |
| 39 |
} else { |
| 40 |
$filePath = realpath($filePath); |
| 41 |
if ($filePath and ! in_array($filePath, $this->loaded)) { |
| 42 |
require $filePath; |
| 43 |
|
| 44 |
$sandbox = create_function('', "require '$filePath'; if(array_keys(get_defined_vars()) != array('config')) return array(); return \$config;"); |
| 45 |
$config = $sandbox(); |
| 46 |
$this->loaded[] = $filePath; |
| 47 |
$this->config = array_merge($this->config, $config); |
| 48 |
} |
| 49 |
} |
| 50 |
return $this; |
| 51 |
} |
| 52 |
/** |
| 53 |
* Return value of setting with specified name |
| 54 |
* @param string $field Setting name |
| 55 |
* @param string[optional] $section Section name to look setting in |
| 56 |
* @return mixed |
| 57 |
*/ |
| 58 |
public function get($field, $section = NULL) { |
| 59 |
return ! is_null($section) ? $this->config[$section]->get($field) : $this->config[$field]; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Magic method for checking whether some config option are set |
| 64 |
* @param string $field |
| 65 |
* @return bool |
| 66 |
*/ |
| 67 |
public function __isset($field) { |
| 68 |
return isset($this->config[$field]); |
| 69 |
} |
| 70 |
/** |
| 71 |
* Magic method to implement object-like access to config parameters |
| 72 |
* @param string $field |
| 73 |
* @return mixed |
| 74 |
*/ |
| 75 |
public function __get($field) { |
| 76 |
return $this->config[$field]; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Return all config options as array |
| 81 |
* @return array |
| 82 |
*/ |
| 83 |
public function toArray($section = NULL) { |
| 84 |
return ! is_null($section) ? $this->config[$section]->toArray() : $this->config; |
| 85 |
} |
| 86 |
|
| 87 |
public function getIterator() { |
| 88 |
return new ArrayIterator($this->config); |
| 89 |
} |
| 90 |
|
| 91 |
} |