Overview

Packages

  • Auth
  • Config
  • Controller
  • Date
  • Db
  • Feed
    • Abstract
    • Writers
  • File
    • Decorator
  • Form
    • Element
  • Image
  • Log
    • Writer
  • Net
    • Exception
  • None
  • PHP
  • PHPMailer
  • Session
  • Util
  • Validate
    • Validator
  • Zend
    • Registry

Classes

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