Go to the documentation of this file.00001 <?php
00016 class axIniConfiguration implements axConfiguration {
00017
00022 const CACHE_FILE = "config.cache.php";
00023
00028 protected $_file;
00029
00034 protected $_section;
00035
00040 protected $_cache_dir;
00041
00046 protected $_tree;
00047
00055 public function __construct ($file, $section, $cache_dir = false) {
00056 $this->_file = $file;
00057 $this->_section = $section;
00058 $this->_cache_dir = $cache_dir !== false ? realpath($cache_dir) : false;
00059 }
00060
00064 public function __get ($key) {
00065 return $this->getIterator()->$key;
00066 }
00067
00071 public function getIterator() {
00072 if (!isset($this->_tree)) {
00073 if ($this->_cache_dir && is_readable($c = $this->_cache_dir . '/' . self::CACHE_FILE)) {
00074 require $c;
00075 $this->_tree = $tree;
00076 }
00077 else {
00078 $this->_generateTree($this->_section);
00079 $this->_cache();
00080 }
00081 }
00082
00083 return $this->_tree;
00084 }
00085
00092 protected function _generateTree ($section) {
00093 if (!is_file($this->_file) || !is_readable($this->_file))
00094 throw new axMissingFileException($this->_file);
00095
00096 if (!$ini = parse_ini_file($this->_file, true))
00097 throw new RuntimeException("Cannot parse $file");
00098
00099 foreach (array_keys($ini) as $key) {
00100 if (($offset = strpos($key, ':')) !== false && isset($ini[trim(substr($key, $offset+1))]))
00101 $ini[$key] += $ini[trim(substr($key, $offset+1))];
00102 if (strpos(trim($key), $section) === 0)
00103 $section = $key;
00104 }
00105
00106 if (!isset($ini[$section]))
00107 throw new RuntimeException("Unable to find section $section");
00108
00109 $this->_tree = new axTreeItem;
00110 foreach ($ini[$section] as $key => $value) {
00111 $p = explode('.', $key);
00112 $c = $this->_tree;
00113 foreach ($p as $k)
00114 $c = $c->__get($k);
00115 $c->setValue($value);
00116 }
00117 }
00118
00126 protected function _cache () {
00127 if (!$this->_cache_dir)
00128 return false;
00129
00130 $buffer = '<?php $tree=' . var_export($this->_tree, true) . '; ?>';
00131 return (boolean)file_put_contents($this->_cache_dir . '/' . self::CACHE_FILE, $buffer);
00132 }
00133 }