PhpMyObject
[ class tree: PhpMyObject ] [ index: PhpMyObject ] [ all elements ]

Source for file PMO_MyConfig.php

Documentation is available at PMO_MyConfig.php

  1. <?php
  2. /**
  3.  * This file contains PMO_MyConfig class which is used to store
  4.  * the PhpMyObject configuration.
  5.  *
  6.  * This file is part of the PhpMyObject project.
  7.  * 
  8.  * For questions, help, comments, discussion, etc., please join our
  9.  * forum at {@link http://www.developpez.net/forums/forumdisplay.php?f=770}
  10.  * or our mailing list at {@link http://groups.google.com/group/pmo-dev}.
  11.  *
  12.  * PhpMyObject is free software: you can redistribute it and/or modify
  13.  * it under the terms of the GNU General Public License as published by
  14.  * the Free Software Foundation, either version 3 of the License, or
  15.  * (at your option) any later version.
  16.  *
  17.  * This program is distributed in the hope that it will be useful,
  18.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.    See the
  20.  * GNU General Public License for more details.
  21.  *
  22.  * You should have received a copy of the GNU General Public License
  23.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  24.  *
  25.  * @package        PhpMyObject
  26.  * @subpackage PMO_Core
  27.  * @author        Nicolas Boiteux <nicolas_boiteux@yahoo.fr>
  28.  * @link            http://pmo.developpez.com/
  29.  * @since        PhpMyObject v0.14
  30.  * @version        $Revision: $
  31.  * @copyright    Copyright (C) 2007-2008 Nicolas Boiteux
  32.  * @license        GPLv3 {@link http://www.gnu.org/licenses/gpl}
  33.  * @filesource
  34.  *
  35.  */ 
  36.  
  37. /** requires the interface */
  38. require_once(dirname(__FILE__).'/PMO_Config.php');
  39.  
  40. /**
  41.  * PMO_MyConfig manages your system configuration.
  42.  *
  43.  * Use this class to takes care of database config and paths to the
  44.  * class_loader/ and PMO_MyTable/ directories
  45.  *
  46.  * @package        PhpMyObject
  47.  * @subpackage PMO_Core
  48.  */
  49. class PMO_MyConfig implements PMO_Config {
  50.  
  51.     /**
  52.      * the static instance of PMO_MyConfig that will be returned
  53.      * @var object $INSTANCE 
  54.      * @static
  55.      * @access protected
  56.      */
  57.     protected static $INSTANCE;
  58.  
  59.     /**
  60.      * PMO_Myarray where the PMO configuration variables are stored
  61.      *
  62.      * On instanciation, this array is filled with default values for
  63.      * the collector name, classpaths and classname prefixes. The database
  64.      * configuration properties are set to empty strings for wich you will
  65.      * need to provide values.
  66.      *
  67.      * PhpMyObject Configuration Properties:
  68.      *
  69.      * - PMO_MyController.OBJECT_COLLECTOR_NAME = "collector"
  70.      * - PMO_MyDbms.DRIVER = ""
  71.      * - PMO_MyDbms.PDODRIVER = ""
  72.      * - PMO_MyDbms.HOST = ""
  73.      * - PMO_MyDbms.BASE = ""
  74.      * - PMO_MyDbms.USER = ""
  75.      * - PMO_MyDbms.PASS = ""
  76.      * - PMO_MyDbms.DSN = ""
  77.      * - PMO_MyDbms.LOG = FALSE
  78.      * - PMO_MyDbms.LOG_FORMAT = "Y-m-d H:i:s"
  79.      * - PMO_MyObject.CLASSPATH = dirname(__FILE__).'/../class_loader/';
  80.      *                                        located at the same level as PMO_Core
  81.      * - PMO_MyObject.CLASS_FILENAME_PREFIX = "class_";
  82.      *                                        prefix to use before table names in the
  83.      *                                        class_loader directory
  84.      * - PMO_MyTable.CLASSPATH  = dirname(__FILE__).'/PMO_MyTable/';
  85.      *                                        located under PMO_Core
  86.      * - PMO_MyTable.CLASS_FILENAME_PREFIX =    "PMO_MyTable_";
  87.      *                                        prefix to use in naming the table file names
  88.      *                                        that are persisted by PMO
  89.      * - PMO_MyMemCache.ACTIVE = FALSE
  90.      * - PMO_MyMemCache.HOST = ""
  91.      * - PMO_MyMemCache.PORT = ""
  92.      * - PMO_MyMemCache.TIMEOUT = 10
  93.      * 
  94.      * 
  95.      * @var array 
  96.      * @access protected
  97.      */
  98.     protected $structure;
  99.  
  100.     /**
  101.      * the constuctor
  102.      *
  103.      * the constructor initializes variables that are needed by PMO, e.g.
  104.      *
  105.      * - the PMO_MyController.OBJECT_COLLECTOR_NAME
  106.      * - the PMO_MyDbms.DRIVER
  107.      * - the PMO_MyDbms.PDODRIVER
  108.      * - the PMO_MyDbms.HOST
  109.      * - the PMO_MyDbms.BASE
  110.      * - the PMO_MyDbms.USER
  111.      * - the PMO_MyDbms.PASS
  112.      * - the PMO_MyDbms.DSN
  113.      * - the PMO_MyDbms.LOG
  114.      * - the PMO_MyDbms.LOG_FORMAT
  115.      * - the PMO_MyObject.CLASSPATH
  116.      * - the PMO_MyObject.CLASS_FILENAME_PREFIX
  117.      * - PMO_MyMemCache.ACTIVE
  118.      * - PMO_MyMemCache.HOST
  119.      * - PMO_MyMemCache.PORT
  120.      * - PMO_MyMemCache.TIMEOUT
  121.      * - the PMO_MyTable.CLASSPATH
  122.      * - the PMO_MyTable.CLASS_FILENAME_PREFIX
  123.      * - the PMO_MyTable.CLASS_WRITE_ON_DISK
  124.      *
  125.      * @see your_config.php
  126.      *
  127.      * @return PMO_MyConfig        the PMO_MyConfig instance
  128.      */
  129.     private function __construct(){
  130.         $this->structure = new PMO_MyArray();
  131.         $this->structure->offsetSet('PMO_MyController.OBJECT_COLLECTOR_NAME'"collector");
  132.         $this->structure->offsetSet('PMO_MyDbms.DRIVER'"");
  133.         $this->structure->offsetSet('PMO_MyDbms.PDODRIVER'"");
  134.         $this->structure->offsetSet('PMO_MyDbms.HOST'"");
  135.         $this->structure->offsetSet('PMO_MyDbms.BASE'"");
  136.         $this->structure->offsetSet('PMO_MyDbms.USER'"");
  137.         $this->structure->offsetSet('PMO_MyDbms.PASS'"");
  138.         $this->structure->offsetSet('PMO_MyDbms.DSN'"");
  139.         $this->structure->offsetSet('PMO_MyDbms.LOG'FALSE);
  140.         $this->structure->offsetSet('PMO_MyDbms.LOG_FORMAT'"Y-m-d H:i:s");
  141.         $this->structure->offsetSet('PMO_MyObject.CLASSPATH'dirname(__FILE__).'/../class_loader/');
  142.         $this->structure->offsetSet('PMO_MyObject.CLASS_FILENAME_PREFIX'"class_");
  143.         $this->structure->offsetSet('PMO_MyMemCache.ACTIVE'FALSE);
  144.         $this->structure->offsetSet('PMO_MyMemCache.HOST'"");
  145.         $this->structure->offsetSet('PMO_MyMemCache.PORT'"");
  146.         $this->structure->offsetSet('PMO_MyMemCache.TIMEOUT'10);
  147.         $this->structure->offsetSet('PMO_MyTable.CLASSPATH'dirname(__FILE__).'/PMO_MyTable/');
  148.         $this->structure->offsetSet('PMO_MyTable.CLASS_FILENAME_PREFIX'"PMO_MyTable_");
  149.         $this->structure->offsetSet('PMO_MyTable.CLASS_WRITE_ON_DISK'FALSE);
  150.     }
  151.     
  152.     /**
  153.      * var_dump all configuration
  154.      */
  155.     public function show(){    
  156.         print'<pre>';var_dump($this->structure);print'</pre>';
  157.     }
  158.     
  159.     /**
  160.      * Returns the PMO_MyConfig instance.
  161.      *
  162.      * If the instance does not exist yet, it is created.
  163.      *
  164.      * @return PMO_MyConfig        the PMO_MyConfig instance.
  165.      * @static
  166.      */
  167.     public static function factory(){
  168.             if (!isset(self::$INSTANCE))
  169.                 self::$INSTANCE new PMO_MyConfig;
  170.             
  171.         return self::$INSTANCE
  172.     }
  173.  
  174.     /**
  175.      * Sets a PMO_MyConfig variable.
  176.      *
  177.      * Example:
  178.      *
  179.      * <code>
  180.      * $conf = PMO_MyConfig::factory();
  181.      * $conf->set('PMO_MyDbms.HOST', 'localhost');
  182.      * </code>
  183.      *
  184.      * @param string $varname    the variable name
  185.      * @param mixed  $value        the value to set the variable with
  186.      * @return void 
  187.      */
  188.     public function set($varname$value null){
  189.         if($this->structure->offsetExists($varname))    
  190.             $this->structure->offsetSet($varname$value);
  191.         else
  192.             throw new Exception("Error: Parameter $varname doesn't exist");
  193.     }     
  194.  
  195.     /**
  196.      * alias of the {@link set()} function
  197.      *
  198.      * @param string $varname    the variable name
  199.      * @param mixed  $value        the value to set the variable with
  200.      */
  201.     public function __set($varname$value{
  202.             $this->set($varname$value);
  203.     }     
  204.  
  205.     /**
  206.      * alias of the {@link get()} function
  207.      *
  208.      * @param string $varname        Variable to read. Must exist.
  209.      * @throws Exception
  210.      * @return mixed 
  211.      */
  212.     public function __get($varname{
  213.             return $this->get($varname);
  214.     }        
  215.     
  216.     /**
  217.      * Returns the value of a PMO_MyConfig variable.
  218.      *
  219.      * Will throw an exception if the variable is undefined.
  220.      *
  221.      * @param string $varname        variable to read. Must exist.
  222.      * @throws Exception                thrown if the variable does not exist.
  223.      * @return mixed 
  224.      */
  225.     public function get($varname){
  226.         if($this->structure->offsetExists($varname))
  227.             return $this->structure->offsetGet($varname);
  228.     
  229.         throw new Exception("Error: Parameter $varname doesn't exist");
  230.     }
  231.     
  232.     /**
  233.      * returns true if the provided variable name exists, false otherwise.
  234.      *
  235.      * @param string $varname        variable name to test for
  236.      * @return true|false           true if the variable exists, false otherwise
  237.      * 
  238.      */
  239.     //public function exists($varname){
  240.     //    if (empty($varname))
  241.     //        throw new Exception("Error: Empty parameter");
  242.     //
  243.     //    return isset($this->structure->offsetGet($varname));
  244.     //}
  245.     
  246. }
  247.  
  248. ?>

Documentation generated on Wed, 15 Oct 2008 09:16:49 -0400 by phpDocumentor 1.4.1