Changeset 2943
- Timestamp:
- 05/30/10 04:50:40 (21 months ago)
- Location:
- trunk
- Files:
-
- 6 edited
-
CHANGELOG (modified) (1 diff)
-
gui/include/IspCP/Config.php (modified) (4 diffs)
-
gui/include/IspCP/ConfigHandler.php (modified) (13 diffs)
-
gui/include/IspCP/ConfigHandler/Db.php (modified) (13 diffs)
-
gui/include/IspCP/ConfigHandler/File.php (modified) (3 diffs)
-
gui/include/IspCP/Registry.php (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/CHANGELOG
r2941 r2943 1 1 ispCP ω 1.0.6 ChangeLog 2 2 ~~~~~~~~~~~~~~~~~~~~~~~~~~ 3 4 2010-05-30 Laurent Declercq 5 - GUI: 6 * Added both Added both __isset() and __unset() methods in 7 IspCP_ConfigHandler classes to be able to operate on inaccessible 8 members 9 * Added methods in IspCP_ConfigHandler classes to be able to remove 10 configuration parameters 11 * Added a wrapper method in Config class for the 12 IspCP_ConfigHandler::del() methods 13 * Fixed doc/typo in Config, IspCP_ConfigHandler and IspCP_Registry 14 classes 3 15 4 16 2010-05-29 Laurent Declercq -
trunk/gui/include/IspCP/Config.php
r2940 r2943 32 32 * Important consideration: 33 33 * 34 * This class implement the Singleton design pattern, so, each 35 * {@link ispCP_ConfigHandler} objects are instanciated only once.34 * This class implement the Singleton design pattern, so, each type of 35 * {@link IspCP_ConfigHandler} objects are instanciated only once. 36 36 * 37 * If you want use several instances of a ispCP_ConfigHandler object (e.g: To37 * If you want use several instances of an IspCP_ConfigHandler object (e.g: To 38 38 * handle separate configuration parameters that are stored in another container 39 39 * such as a configuration file linked to a specific plugin) you should not use 40 * this class. 40 * this class. Instead of this, register your own IspCP_ConfigHandler objects 41 * into the ispCP_Registry object to be able to use them from all contexts. 41 42 * 42 * To resume, this class acts as a registry for the ispCP_ConfigHandler objects 43 * where the registered values (that are ispCP_ConfigHandler objects) are 44 * indexed by they class name. 43 * Example: 44 * 45 * $parameters = array('PLUGIN_NAME => 'billing', PLUGIN_VERSION => '1.0.0'); 46 * IspCP_Registry::set('My_ConfigHandler', new IspCP_ConfigHandler($parameters)); 47 * 48 * From another context: 49 * 50 * $my_cfg = IspCP_Registry::get('My_ConfigHandler'); 51 * echo $my_cfg->PLUGIN_NAME; // billing 52 * echo $my_cfg->PLUGIN_VERSION; // 1.0.0 53 * 54 * See {@link IspCP_Registry} for more information. 55 * 56 * To resume, the Config class acts as a registry for the IspCP_ConfigHandler 57 * objects where the registered values (that are IspCP_ConfigHandler objects) 58 * are indexed by they class name. 45 59 */ 46 60 final class Config { 47 61 48 62 /** 49 * List of all the spCP_ConfigHandler object that this class can/willhandle63 * List of all the IspCP_ConfigHandler objects that this class can handle 50 64 */ 51 65 const … … 58 72 59 73 /** 60 * Array that contain references to {@link ispCP_ConfigHandler} objects74 * Array that contain references to {@link IspCP_ConfigHandler} objects 61 75 * indexed by they class name 62 76 * … … 73 87 * The default handler object is set to {@link IspCP_ConfigHandler_File} 74 88 * 75 * @param string $type Type of IspCP_ConfigHandler object that should be 76 * returned 77 * @param mixed $params Parameters that are passed to the 78 * IspCP_ConfigHandler object constructor 89 * @param string $classname IspCP_ConfigHandler class name 90 * @param mixed $params Parameters that are passed to IspCP_ConfigHandler 91 * object constructor 79 92 * @throws Exception 80 93 * @return IspCP_ConfigHandler 81 94 */ 82 public static function &getInstance($ type = self::FILE, $params = null) {95 public static function &getInstance($classname = self::FILE, $params = null) { 83 96 84 if(!array_key_exists($ type, self::$_instances)) {97 if(!array_key_exists($classname, self::$_instances)) { 85 98 86 if($ type === false) {99 if($classname === false) { 87 100 throw new Exception( 88 101 'The IspCP_ConfigHandler object you trying to use is not ' . 89 102 'yet implemented!' 90 103 ); 91 } elseif (!class_exists($ type, true)) {104 } elseif (!class_exists($classname, true)) { 92 105 throw new Exception( 93 "The class `$ type` is not reachable!"106 "The class `$classname` is not reachable!" 94 107 ); 95 } elseif (!is_subclass_of($ type, 'IspCP_ConfigHandler')) {108 } elseif (!is_subclass_of($classname, 'IspCP_ConfigHandler')) { 96 109 throw new Exception( 97 110 'Only IspCP_ConfigHandler objects can be handling by the ' . … … 100 113 } 101 114 102 self::$_instances[$ type] = new $type($params);115 self::$_instances[$classname] = new $classname($params); 103 116 } 104 117 105 return self::$_instances[$ type];118 return self::$_instances[$classname]; 106 119 } 107 120 108 121 /** 109 * Wrapper for getter method of a IspCP_ConfigHandler object122 * Wrapper for getter method of an IspCP_ConfigHandler object 110 123 * 111 * @s tatic124 * @see IspCP_ConfigHandler::get() 112 125 * @param string $index Configuration parameter key name 126 * @param string $classname IspCP_ConfigHandler class name 113 127 * @return Configuration parameter value 114 128 */ 115 public static function get($index, $type = self::FILE) { 116 return self::getInstance($type)->get($index); 129 public static function get($index, $classname = self::FILE) { 130 131 return self::getInstance($classname)->get($index); 117 132 } 118 133 119 134 /** 120 * Wrapper for setter method of a IspCP_ConfigHandler object135 * Wrapper for setter method of an IspCP_ConfigHandler object 121 136 * 122 * @s tatic137 * @see IspCP_ConfigHandler::set() 123 138 * @param string $index Configuration parameter key name 124 139 * @param mixed $value Configuration parameter value 140 * @param string $classname IspCP_ConfigHandler class name 125 141 * @return void 126 142 */ 127 public static function set($index, $value, $type = self::FILE) { 128 self::getInstance($type)->set($index, $value); 143 public static function set($index, $value, $classname = self::FILE) { 144 145 self::getInstance($classname)->set($index, $value); 146 } 147 148 /** 149 * Wrapper for {@link IspCP_ConfigHandler::del()} method 150 * 151 * @see IspCP_ConfigHandler::del() 152 * @param string $index Configuration parameter key name 153 * @param string $classname IspCP_ConfigHandler class name 154 * @return void 155 */ 156 public static function del($index, $classname = self::FILE) { 157 158 self::getInstance($classname)->del($index); 129 159 } 130 160 } -
trunk/gui/include/IspCP/ConfigHandler.php
r2938 r2943 56 56 57 57 /** 58 * Loads all configuration parameters from an Array58 * Loads all configuration parameters from an array 59 59 * 60 60 * @param array $parameters Configuration parameters … … 80 80 * Allow access as object propertie 81 81 * 82 * @see set() 82 83 * @param string $name Configuration parameter key name 83 84 * @param mixed $value Configuration parameter value 84 * @see set()85 85 * @return void 86 86 */ … … 110 110 * 111 111 * @see get(); 112 * @param string Configuration parameter key name 112 113 * @return mixed Configuration parameter value 113 114 */ … … 115 116 116 117 return $this->get($index); 118 } 119 120 /** 121 * Methods to delete a configuration parameters 122 * 123 * @param $string $index Configuration parameter key name 124 * @return void 125 */ 126 public function del($index) { 127 unset($this->parameters[$index]); 128 } 129 130 /** 131 * PHP Overloading for call of isset() on inaccessible members. 132 * 133 * @param string Configuration parameter key name 134 * @return TRUE if the configuration parameter exists, FALSE otherwise 135 */ 136 public function __isset($index) { 137 138 return isset($this->parameters[$index]); 139 } 140 141 /** 142 * PHP Overloading for call of unset() on inaccessible members 143 * 144 * @param string Configuration parameter key name 145 * @return void 146 */ 147 public function __unset($index) { 148 149 $this->del($index); 117 150 } 118 151 … … 158 191 */ 159 192 public function current() { 193 160 194 return current($this->parameters); 161 195 } … … 167 201 */ 168 202 public function next() { 203 169 204 next($this->parameters); 170 205 } … … 176 211 */ 177 212 public function valid() { 213 178 214 return array_key_exists(key($this->parameters), $this->parameters); 179 215 } … … 185 221 */ 186 222 public function rewind() { 223 187 224 reset($this->parameters); 188 225 return $this; … … 195 232 */ 196 233 public function key() { 234 197 235 return key($this->parameters); 198 236 } … … 204 242 */ 205 243 public function offsetExists($index) { 244 206 245 return $this->exists($index); 207 246 } … … 213 252 */ 214 253 public function offsetGet($index) { 254 215 255 return $this->get($index); 216 256 } … … 222 262 */ 223 263 public function offsetSet($index, $value) { 264 224 265 $this->set($index, $value); 225 266 } … … 231 272 */ 232 273 public function offsetUnset($index) { 233 unset($this->parameters[$index]); 274 275 $this->del($index); 234 276 } 235 277 } -
trunk/gui/include/IspCP/ConfigHandler/Db.php
r2939 r2943 46 46 47 47 /** 48 * Reference to a rawPDO instance49 * 50 * @var Reference to a PDO instance48 * Reference to a PDO instance 49 * 50 * @var PDO 51 51 */ 52 52 private $_db; … … 56 56 * 57 57 * For performance reason, the PDOStatement object is created only once at 58 * the first execution of the {@link insert _to_db()} method.59 * 60 * @var Reference to a PDOStatement instance61 */ 62 private $ insert_stmt = null;58 * the first execution of the {@link insert()} method. 59 * 60 * @var PDOStatement 61 */ 62 private $_insert_stmt = null; 63 63 64 64 /** … … 66 66 * 67 67 * For performance reason, the PDOStatement object is created only once at 68 * the first execution of the {@link update_to_db()} method. 69 * 70 * @var Reference to a PDOStatement instance 71 */ 72 private $update_stmt = null; 68 * the first execution of the {@link update()} method. 69 * 70 * @var PDOStatement 71 */ 72 private $_update_stmt = null; 73 74 /** 75 * PDOStatement to delete a configuration parameter in the database 76 * 77 * For performance reason, the PDOStatement object is created only once at 78 * the first execution of the {@link delete()} method. 79 * 80 * @var PDOStatement 81 */ 82 private $_delete_stmt = null; 73 83 74 84 /** 75 85 * Variable bound to the PDOStatement objects 76 86 * 77 * Th e value of this variable is bound to the PDOStatementthat are used by78 * the both method {@link insert_to_db()} and {@link update_to_db()}79 * 80 * @var Configuration parameter key name81 */ 82 private $ index = null;87 * This variable is bound to the PDOStatement objects that are used by 88 * {@link insert()} , {@link update()} and {@link delete()} methods. 89 * 90 * @var string Configuration parameter key name 91 */ 92 private $_index = null; 83 93 84 94 /** 85 95 * Variable bound to the PDOStatement objects 86 96 * 87 * Th e value of this variable is bound to the PDOStatement that are used by88 * the both method {@link insert_to_db()} and {@link update_to_db()}89 * 90 * @var Configuration parameter value91 */ 92 private $ value = null;97 * This variable is bound to the PDOStatement objects that are used by both 98 * {@link insert()} and {@link update()} methods. 99 * 100 * @var mixed Configuration parameter value 101 */ 102 private $_value = null; 93 103 94 104 /** 95 105 * Database table where the configuration parameters are stored 106 * 96 107 * @var 97 108 */ … … 129 140 * Note: The three last parameters are optionals. 130 141 * 131 * for a single parameter, only a raw PDO (unwrapped)instance is accepted.142 * For a single parameter, only a PDO instance is accepted. 132 143 * 133 144 * @param PDO|array A PDO instance or an array of parameter that contain at … … 169 180 $this->_db = $params; 170 181 171 parent::__construct($this-> load_all());182 parent::__construct($this->_load_all()); 172 183 } 173 184 … … 184 195 public function set($index, $value) { 185 196 186 $this-> index = $index;187 $this-> value = $value;197 $this->_index = $index; 198 $this->_value = $value; 188 199 189 200 if(!array_key_exists($index, $this->parameters)) { 190 $this-> insert_to_db();201 $this->_insert(); 191 202 } elseif($this->parameters[$index] != $value) { 192 $this-> update_to_db();203 $this->_update(); 193 204 } else { 194 205 return; … … 204 215 * @return void 205 216 */ 206 private function load_all() {217 private function _load_all() { 207 218 208 219 $query = " … … 235 246 * @return void 236 247 */ 237 private function insert_to_db() {238 if(!$this-> insert_stmt instanceof PDOStatement) {248 private function _insert() { 249 if(!$this->_insert_stmt instanceof PDOStatement) { 239 250 240 251 $query = " 241 252 INSERT INTO 242 253 `{$this->table_name}` 243 ( {$this->keys_column}, `{$this->values_column}`)254 (`{$this->keys_column}`, `{$this->values_column}`) 244 255 VALUES 245 256 (:index, :value) … … 247 258 "; 248 259 249 $this-> insert_stmt = $this->_db->prepare($query);250 $this-> insert_stmt->BindParam(':index', $this->index);251 $this-> insert_stmt->BindParam(':value', $this->value);252 } 253 254 if($this-> insert_stmt->execute() === false) {260 $this->_insert_stmt = $this->_db->prepare($query); 261 $this->_insert_stmt->BindParam(':index', $this->_index); 262 $this->_insert_stmt->BindParam(':value', $this->_value); 263 } 264 265 if($this->_insert_stmt->execute() === false) { 255 266 throw new Exception( 256 267 'Unable to insert the configuration parameter in the database!' … … 265 276 * @return void 266 277 */ 267 private function update_to_db() {268 269 if(!$this-> update_stmt instanceof PDOStatement) {278 private function _update() { 279 280 if(!$this->_update_stmt instanceof PDOStatement) { 270 281 271 282 $query = " … … 279 290 "; 280 291 281 $this-> update_stmt = $this->_db->prepare($query);282 $this-> update_stmt->BindParam(':index', $this->index);283 $this-> update_stmt->BindParam(':value', $this->value);284 } 285 286 if($this-> update_stmt->execute() === false) {292 $this->_update_stmt = $this->_db->prepare($query); 293 $this->_update_stmt->BindParam(':index', $this->_index); 294 $this->_update_stmt->BindParam(':value', $this->_value); 295 } 296 297 if($this->_update_stmt->execute() === false) { 287 298 throw new Exception( 288 299 'Unable to update the configuration parameter in the database!' … … 292 303 293 304 /** 305 * Delete a configuration parameter in the database 306 * 307 * @throws Exception 308 * @return void 309 */ 310 private function _delete() { 311 312 if(!$this->_delete_stmt instanceof PDOStatement) { 313 314 $query = " 315 DELETE FROM 316 `{$this->table_name}` 317 WHERE 318 `{$this->keys_column}` = :index 319 ; 320 "; 321 322 $this->_delete_stmt = $this->_db->prepare($query); 323 $this->_delete_stmt->BindParam(':index', $this->_index); 324 } 325 326 if($this->_delete_stmt->execute() === false) { 327 throw new Exception( 328 'Unable to delete the configuration parameter in the database!' 329 ); 330 } 331 } 332 333 /** 294 334 * Force reload of all configuration parameters from the database 295 335 * … … 297 337 */ 298 338 public function force_reload() { 299 $this->parameters = $this->load_all(); 339 340 $this->parameters = $this->_load_all(); 341 } 342 343 /** 344 * Defined by SPL ArrayAccess interface 345 * 346 * See {@link http://www.php.net/~helly/php/ext/spl} 347 */ 348 public function offsetUnset($index) { 349 350 $this->_index = $index; 351 $this->_delete(); 352 parent::offsetUnset($index); 353 } 354 355 /** 356 * PHP Overloading for call of unset() on inaccessible members 357 * 358 * @param string $index Configuration parameter key name 359 * @return void 360 */ 361 public function __unset($index) { 362 363 $this->_index = $index; 364 $this->_delete(); 365 parent::__unset($index); 300 366 } 301 367 } -
trunk/gui/include/IspCP/ConfigHandler/File.php
r2939 r2943 35 35 * 36 36 * IspCP_ConfigHandler adapter to handle configuration parameters that are stored 37 * in a flat file where each pair of key values are separated by the equal sign.37 * in a flat file where each pair of key-values are separated by the equal sign. 38 38 * 39 * By default, this object parse the default ispCP configuration file. 40 * 41 * @See ispCP_ConfigHandler 39 * @see IspCP_ConfigHandler 42 40 */ 43 class ispCP_ConfigHandler_File extends ispCP_ConfigHandler {41 class ispCP_ConfigHandler_File extends IspCP_ConfigHandler { 44 42 45 43 /** … … 51 49 52 50 /** 53 * Loads the ispCP config file (default directory: /etc/ispcp/ispcp.conf) 51 * Loads all configuration parameters from a file 52 * 53 * Note: default file path is set to: {/usr/local}/etc/ispcp/ispcp.conf 54 * depending of the used distribution. 54 55 * 55 56 * @param string $path_file Configuration file path 56 * @throws Exception57 57 * @return void 58 58 */ … … 77 77 78 78 /** 79 * Opens a configuration file and parses its K EY= Value pairs into the79 * Opens a configuration file and parses its Key = Value pairs into the 80 80 * {@link IspCP_ConfigHangler::parameters} array. 81 81 * 82 82 * @throws Exception 83 * @return Configuration parameters83 * @return Array that contain all Configuration parameters 84 84 */ 85 85 private function parseFile() { -
trunk/gui/include/IspCP/Registry.php
r2940 r2943 72 72 73 73 /** 74 * Get a IspCP_Registry instance74 * Get an IspCP_Registry instance 75 75 * 76 * Returns a reference to a{@link IspCP_Registry} instance, only creating76 * Returns a reference to {@link IspCP_Registry} instance, only creating 77 77 * it if it doesn't already exist. 78 78 * … … 89 89 90 90 /** 91 * Getter method to get adata that is stored in the register91 * Getter method to get data that is stored in the register 92 92 * 93 93 * @param string Data key name … … 109 109 110 110 /** 111 * Setter method to register anew data in the register111 * Setter method to register new data in the register 112 112 * 113 113 * @param string Data key name … … 124 124 125 125 /** 126 * Check if adata exists in the registry126 * Check if data exists in the registry 127 127 * 128 128 * @param string $name Data key name
Note: See TracChangeset
for help on using the changeset viewer.
