Changeset 2943


Ignore:
Timestamp:
05/30/10 04:50:40 (21 months ago)
Author:
nuxwin
Message:
  • [GUI] Added both Added both isset() and unset() methods in IspCP_ConfigHandler classes to be able to operate on inaccessible members
  • [GUI] Added methods in IspCP_ConfigHandler classes to be able to remove configuration parameters
  • [GUI] Added a wrapper method in Config class for the IspCP_ConfigHandler::del() methods
  • [GUI] Fixed doc/typo in Config, IspCP_ConfigHandler and IspCP_Registry classes
Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/CHANGELOG

    r2941 r2943  
    11ispCP ω 1.0.6 ChangeLog 
    22~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     3 
     42010-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 
    315 
    4162010-05-29 Laurent Declercq 
  • trunk/gui/include/IspCP/Config.php

    r2940 r2943  
    3232 * Important consideration: 
    3333 * 
    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. 
    3636 * 
    37  * If you want use several instances of a ispCP_ConfigHandler object (e.g: To 
     37 * If you want use several instances of an IspCP_ConfigHandler object (e.g: To 
    3838 * handle separate configuration parameters that are stored in another container 
    3939 * 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. 
    4142 * 
    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. 
    4559 */ 
    4660final class Config { 
    4761 
    4862        /** 
    49          * List of all the spCP_ConfigHandler object that this class can/will handle 
     63         * List of all the IspCP_ConfigHandler objects that this class can handle 
    5064         */ 
    5165        const 
     
    5872 
    5973        /** 
    60          * Array that contain references to {@link ispCP_ConfigHandler} objects 
     74         * Array that contain references to {@link IspCP_ConfigHandler} objects 
    6175         * indexed by they class name 
    6276         * 
     
    7387         * The default handler object is set to {@link IspCP_ConfigHandler_File} 
    7488         * 
    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 
    7992         * @throws Exception 
    8093         * @return IspCP_ConfigHandler 
    8194         */ 
    82         public static function &getInstance($type = self::FILE, $params = null) { 
     95        public static function &getInstance($classname = self::FILE, $params = null) { 
    8396 
    84                 if(!array_key_exists($type, self::$_instances)) { 
     97                if(!array_key_exists($classname, self::$_instances)) { 
    8598 
    86                         if($type === false) { 
     99                        if($classname === false) { 
    87100                                throw new Exception( 
    88101                                        'The IspCP_ConfigHandler object you trying to use is not ' . 
    89102                                                'yet implemented!' 
    90103                                ); 
    91                         } elseif (!class_exists($type, true)) { 
     104                        } elseif (!class_exists($classname, true)) { 
    92105                                throw new Exception( 
    93                                         "The class `$type` is not reachable!" 
     106                                        "The class `$classname` is not reachable!" 
    94107                                ); 
    95                 } elseif (!is_subclass_of($type, 'IspCP_ConfigHandler')) { 
     108                } elseif (!is_subclass_of($classname, 'IspCP_ConfigHandler')) { 
    96109                                throw new Exception( 
    97110                                        'Only IspCP_ConfigHandler objects can be handling by the ' . 
     
    100113                        } 
    101114 
    102                         self::$_instances[$type] = new $type($params); 
     115                        self::$_instances[$classname] = new $classname($params); 
    103116                } 
    104117 
    105                 return self::$_instances[$type]; 
     118                return self::$_instances[$classname]; 
    106119        } 
    107120 
    108121        /** 
    109          * Wrapper for getter method of a IspCP_ConfigHandler object 
     122         * Wrapper for getter method of an IspCP_ConfigHandler object 
    110123         * 
    111          * @static 
     124         * @see IspCP_ConfigHandler::get() 
    112125         * @param string $index Configuration parameter key name 
     126         * @param string $classname IspCP_ConfigHandler class name 
    113127         * @return Configuration parameter value 
    114128         */ 
    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); 
    117132        } 
    118133 
    119134        /** 
    120          * Wrapper for setter method of a IspCP_ConfigHandler object 
     135         * Wrapper for setter method of an IspCP_ConfigHandler object 
    121136         * 
    122          * @static 
     137         * @see IspCP_ConfigHandler::set() 
    123138         * @param string $index Configuration parameter key name 
    124139         * @param mixed $value Configuration parameter value 
     140         * @param string $classname IspCP_ConfigHandler class name 
    125141         * @return void 
    126142         */ 
    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); 
    129159        } 
    130160} 
  • trunk/gui/include/IspCP/ConfigHandler.php

    r2938 r2943  
    5656 
    5757        /** 
    58          * Loads all configuration parameters from an Array 
     58         * Loads all configuration parameters from an array 
    5959         * 
    6060         * @param array $parameters Configuration parameters 
     
    8080         * Allow access as object propertie 
    8181         * 
     82         * @see set() 
    8283         * @param string $name Configuration parameter key name 
    8384         * @param mixed  $value Configuration parameter value 
    84          * @see set() 
    8585         * @return void 
    8686         */ 
     
    110110         * 
    111111         * @see get(); 
     112         * @param string Configuration parameter key name 
    112113         * @return mixed Configuration parameter value 
    113114         */ 
     
    115116 
    116117                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); 
    117150        } 
    118151 
     
    158191         */ 
    159192        public function current() { 
     193 
    160194                return current($this->parameters); 
    161195        } 
     
    167201         */ 
    168202        public function next() { 
     203 
    169204                next($this->parameters); 
    170205        } 
     
    176211         */ 
    177212        public function valid() { 
     213 
    178214                return array_key_exists(key($this->parameters), $this->parameters); 
    179215        } 
     
    185221         */ 
    186222        public function rewind() { 
     223 
    187224                reset($this->parameters); 
    188225        return $this; 
     
    195232         */ 
    196233        public function key() { 
     234 
    197235                return key($this->parameters); 
    198236        } 
     
    204242         */ 
    205243        public function offsetExists($index) { 
     244 
    206245                return $this->exists($index); 
    207246        } 
     
    213252         */ 
    214253        public function offsetGet($index) { 
     254 
    215255                return $this->get($index); 
    216256        } 
     
    222262         */ 
    223263        public function offsetSet($index, $value) { 
     264 
    224265                $this->set($index, $value); 
    225266        } 
     
    231272         */ 
    232273        public function offsetUnset($index) { 
    233                 unset($this->parameters[$index]); 
     274 
     275                $this->del($index); 
    234276        } 
    235277} 
  • trunk/gui/include/IspCP/ConfigHandler/Db.php

    r2939 r2943  
    4646 
    4747        /** 
    48          * Reference to a raw PDO instance 
    49          * 
    50          * @var Reference to a PDO instance 
     48         * Reference to a PDO instance 
     49         * 
     50         * @var PDO 
    5151         */ 
    5252        private $_db; 
     
    5656         * 
    5757         * 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 instance 
    61          */ 
    62         private $insert_stmt = null; 
     58         * the first execution of the {@link insert()} method. 
     59         * 
     60         * @var PDOStatement 
     61         */ 
     62        private $_insert_stmt = null; 
    6363 
    6464        /** 
     
    6666         * 
    6767         * 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; 
    7383 
    7484        /** 
    7585         * Variable bound to the PDOStatement objects 
    7686         * 
    77          * The value of this variable is bound to the PDOStatement that are used by 
    78          * the both method {@link insert_to_db()} and {@link update_to_db()} 
    79          * 
    80          * @var Configuration parameter key name 
    81          */ 
    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; 
    8393 
    8494        /** 
    8595         * Variable bound to the PDOStatement objects 
    8696         * 
    87          * The value of this variable is bound to the PDOStatement that are used by 
    88          * the both method {@link insert_to_db()} and {@link update_to_db()} 
    89          * 
    90          * @var Configuration parameter value 
    91          */ 
    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; 
    93103 
    94104        /** 
    95105         * Database table where the configuration parameters are stored 
     106         * 
    96107         * @var 
    97108         */ 
     
    129140         * Note: The three last parameters are optionals. 
    130141         * 
    131          * for a single parameter, only a raw PDO (unwrapped) instance is accepted. 
     142         * For a single parameter, only a PDO instance is accepted. 
    132143         * 
    133144         * @param PDO|array A PDO instance or an array of parameter that contain at 
     
    169180                $this->_db = $params; 
    170181 
    171                 parent::__construct($this->load_all()); 
     182                parent::__construct($this->_load_all()); 
    172183        } 
    173184 
     
    184195        public function set($index, $value) { 
    185196 
    186                 $this->index = $index; 
    187                 $this->value = $value; 
     197                $this->_index = $index; 
     198                $this->_value = $value; 
    188199 
    189200                if(!array_key_exists($index, $this->parameters)) { 
    190                         $this->insert_to_db(); 
     201                        $this->_insert(); 
    191202                } elseif($this->parameters[$index] != $value) { 
    192                         $this->update_to_db(); 
     203                        $this->_update(); 
    193204                } else { 
    194205                        return; 
     
    204215         * @return void 
    205216         */ 
    206         private function load_all() { 
     217        private function _load_all() { 
    207218 
    208219                $query = " 
     
    235246         * @return void 
    236247         */ 
    237         private function insert_to_db() { 
    238                 if(!$this->insert_stmt instanceof PDOStatement) { 
     248        private function _insert() { 
     249                if(!$this->_insert_stmt instanceof PDOStatement) { 
    239250 
    240251                        $query = " 
    241252                                INSERT INTO 
    242253                                        `{$this->table_name}` 
    243                                         ({$this->keys_column}, `{$this->values_column}`) 
     254                                        (`{$this->keys_column}`, `{$this->values_column}`) 
    244255                                VALUES 
    245256                                        (:index, :value) 
     
    247258                        "; 
    248259 
    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) { 
    255266                        throw new Exception( 
    256267                                'Unable to insert the configuration parameter in the database!' 
     
    265276         * @return void 
    266277         */ 
    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) { 
    270281 
    271282                        $query = " 
     
    279290                        "; 
    280291 
    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) { 
    287298                        throw new Exception( 
    288299                                'Unable to update the configuration parameter in the database!' 
     
    292303 
    293304        /** 
     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        /** 
    294334         * Force reload of all configuration parameters from the database 
    295335         * 
     
    297337         */ 
    298338        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); 
    300366        } 
    301367} 
  • trunk/gui/include/IspCP/ConfigHandler/File.php

    r2939 r2943  
    3535 * 
    3636 * 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. 
    3838 * 
    39  * By default, this object parse the default ispCP configuration file. 
    40  * 
    41  * @See ispCP_ConfigHandler 
     39 * @see IspCP_ConfigHandler 
    4240 */ 
    43 class ispCP_ConfigHandler_File extends ispCP_ConfigHandler { 
     41class ispCP_ConfigHandler_File extends IspCP_ConfigHandler { 
    4442 
    4543        /** 
     
    5149 
    5250        /** 
    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. 
    5455         * 
    5556         * @param string $path_file Configuration file path 
    56          * @throws Exception 
    5757         * @return void 
    5858         */ 
     
    7777 
    7878        /** 
    79          * Opens a configuration file and parses its KEY = Value pairs into the 
     79         * Opens a configuration file and parses its Key = Value pairs into the 
    8080         * {@link IspCP_ConfigHangler::parameters} array. 
    8181         * 
    8282         * @throws Exception 
    83          * @return Configuration parameters 
     83         * @return Array that contain all Configuration parameters 
    8484         */ 
    8585        private function parseFile() { 
  • trunk/gui/include/IspCP/Registry.php

    r2940 r2943  
    7272 
    7373        /** 
    74          * Get a IspCP_Registry instance 
     74         * Get an IspCP_Registry instance 
    7575         * 
    76          * Returns a reference to a {@link IspCP_Registry} instance, only creating 
     76         * Returns a reference to {@link IspCP_Registry} instance, only creating 
    7777         * it if it doesn't already exist. 
    7878         * 
     
    8989 
    9090        /** 
    91          * Getter method to get a data that is stored in the register 
     91         * Getter method to get data that is stored in the register 
    9292         * 
    9393         * @param string Data key name 
     
    109109 
    110110        /** 
    111          * Setter method to register a new data in the register 
     111         * Setter method to register new data in the register 
    112112         * 
    113113         * @param string Data key name 
     
    124124 
    125125        /** 
    126          * Check if a data exists in the registry 
     126         * Check if data exists in the registry 
    127127         * 
    128128         * @param  string $name Data key name 
Note: See TracChangeset for help on using the changeset viewer.