Overview

Namespaces

  • None
  • PHP
  • Pry
    • Auth
      • Interfaces
    • Config
    • Controller
    • Date
    • Db
    • Feed
      • Abstracts
      • Writers
    • File
      • Decorator
    • Form
      • Element
    • Image
    • Log
      • Writer
    • Net
      • Exception
    • Session
    • Util
    • Validate
      • Validator
    • View

Classes

  • Config
  • Ini
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * Pry Framework
  5:  *
  6:  * LICENSE
  7:  *
  8:  * This source file is subject to the new BSD license that is bundled
  9:  * with this package in the file LICENSE.txt.
 10:  * 
 11:  */
 12: 
 13: namespace Pry\Config;
 14: 
 15: /**
 16:  * Classe générique pour la configuration. Basée sur Zend_Config
 17:  * @package Config
 18:  * @version 1.0.0
 19:  * @author Olivier ROGER <oroger.fr>
 20:  * @copyright  2007-2012 Prynel
 21:  * @see Zend_Config
 22:  *
 23:  */
 24: class Config implements \Countable, \Iterator
 25: {
 26: 
 27:     /**
 28:      * Index d'itération
 29:      * @var int 
 30:      */
 31:     protected $index = 0;
 32: 
 33:     /**
 34:      * Nombre d'élément dans le tableau de config
 35:      * @var int 
 36:      */
 37:     protected $count;
 38: 
 39:     /**
 40:      * Flag pour éviter de sauter un élément après un unset
 41:      * @var boolean 
 42:      */
 43:     private $skipNextIteration = false;
 44: 
 45:     /**
 46:      * Tableau des données de config
 47:      * @var array 
 48:      */
 49:     protected $datas = array();
 50: 
 51:     /**
 52:      * Chaine contenant une erreur
 53:      * @var string 
 54:      */
 55:     protected $errorStr = null;
 56: 
 57:     /**
 58:      * This is used to track section inheritance. The keys are names of sections that
 59:      * extend other sections, and the values are the extended sections.
 60:      *
 61:      * @var array
 62:      */
 63:     protected $_extends = array();
 64: 
 65:     public function __construct(array $array)
 66:     {
 67:         foreach ($array as $key => $value) {
 68:             if (is_array($value))
 69:             {
 70:                 $this->datas[$key] = new self($value);
 71:             }
 72:             else
 73:             {
 74:                 $this->datas[$key] = $value;
 75:             }
 76: 
 77:             $this->count++;
 78:         }
 79:     }
 80: 
 81:     /**
 82:      * Gère les erreurs pouvant survenir avec parse_ini_file
 83:      * @param int $errno
 84:      * @param string $errstr
 85:      * @param string $errfile
 86:      * @param int $errline 
 87:      */
 88:     protected function errorHandler($errno, $errstr, $errfile, $errline)
 89:     {
 90:         if ($this->errorStr === null)
 91:         {
 92:             $this->errorStr = $errno . ':' . $errstr . ' on ' . $errfile . ' at ' . $errline;
 93:         }
 94:         else
 95:         {
 96:             $this->errorStr .= (PHP_EOL . $errstr);
 97:         }
 98:     }
 99: 
100:     /**
101:      * Throws an exception if $extendingSection may not extend $extendedSection,
102:      * and tracks the section extension if it is valid.
103:      *
104:      * @param  string $extendingSection
105:      * @param  string $extendedSection
106:      * @throws Zend_Config_Exception
107:      * @return void
108:      */
109:     protected function checkForCircularInheritance($extendingSection, $extendedSection)
110:     {
111:         // detect circular section inheritance
112:         $extendedSectionCurrent = $extendedSection;
113:         while (array_key_exists($extendedSectionCurrent, $this->_extends)) {
114:             if ($this->_extends[$extendedSectionCurrent] == $extendingSection)
115:             {
116:                 throw new \Exception('Illegal circular inheritance detected');
117:             }
118:             $extendedSectionCurrent            = $this->_extends[$extendedSectionCurrent];
119:         }
120:         // remember that this section extends another section
121:         $this->_extends[$extendingSection] = $extendedSection;
122:     }
123: 
124:     /**
125:      * Convertit l'objet de config en tableau associatif
126:      * @return array 
127:      */
128:     public function toArray()
129:     {
130:         $array = array();
131:         foreach ($this->datas as $key => $value) {
132:             if ($value instanceof Config_Config)
133:                 $array[$key] = $value->toArray();
134:             else
135:                 $array[$key] = $value;
136:         }
137:         return $array;
138:     }
139: 
140:     /**
141:      * Merge two arrays recursively, overwriting keys of the same name
142:      * in $firstArray with the value in $secondArray.
143:      *
144:      * @param  mixed $firstArray  First array
145:      * @param  mixed $secondArray Second array to merge into first array
146:      * @return array
147:      */
148:     protected function _arrayMergeRecursive($firstArray, $secondArray)
149:     {
150:         if (is_array($firstArray) && is_array($secondArray))
151:         {
152:             foreach ($secondArray as $key => $value) {
153:                 if (isset($firstArray[$key]))
154:                 {
155:                     $firstArray[$key] = $this->_arrayMergeRecursive($firstArray[$key], $value);
156:                 }
157:                 else
158:                 {
159:                     if ($key === 0)
160:                     {
161:                         $firstArray = array(0 => $this->_arrayMergeRecursive($firstArray, $value));
162:                     }
163:                     else
164:                     {
165:                         $firstArray[$key] = $value;
166:                     }
167:                 }
168:             }
169:         }
170:         else
171:         {
172:             $firstArray = $secondArray;
173:         }
174: 
175:         return $firstArray;
176:     }
177: 
178:     private function get($name, $default = null)
179:     {
180:         $result = $default;
181:         if (array_key_exists($name, $this->datas))
182:         {
183:             $result = $this->datas[$name];
184:         }
185:         return $result;
186:     }
187: 
188:     public function __get($name)
189:     {
190:         return $this->get($name);
191:     }
192: 
193:     public function __isset($name)
194:     {
195:         return isset($this->datas[$name]);
196:     }
197: 
198:     public function __unset($name)
199:     {
200:         unset($this->datas[$name]);
201:         $this->count             = count($this->datas);
202:         $this->skipNextIteration = true;
203:     }
204: 
205:     public function count()
206:     {
207:         return count($this->datas);
208:     }
209: 
210:     public function current()
211:     {
212:         $this->skipNextIteration = false;
213:         return current($this->datas);
214:     }
215: 
216:     public function key()
217:     {
218:         return key($this->datas);
219:     }
220: 
221:     public function next()
222:     {
223:         if ($this->skipNextIteration)
224:         {
225:             $this->skipNextIteration = false;
226:             return;
227:         }
228:         next($this->datas);
229:         $this->index++;
230:     }
231: 
232:     public function rewind()
233:     {
234:         $this->_skipNextIteration = false;
235:         reset($this->datas);
236:         $this->index              = 0;
237:     }
238: 
239:     public function valid()
240:     {
241:         return $this->index < count($this->datas);
242:     }
243: 
244: }
Pry API documentation generated by ApiGen 2.8.0