phpDocumentor ZNF
Config
[ class tree: ZNF ] [ index: ZNF ] [ all elements ]

Source for file DBConfig.php

Documentation is available at DBConfig.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4.  * PHP version 5
  5.  *
  6.  * This source file is subject to version 2.1 of the GNU Lesser General Public
  7.  * License, that is bundled with this package in the file COPYING, available
  8.  * through the world wide web at the following URI:
  9.  * http://www.gnu.org/copyleft/lesser.html.
  10.  *
  11.  * @package    ZNF
  12.  * @subpackage Config
  13.  * @author     Alessandro Rossini <http://www.alessandrorossini.org>
  14.  * @author     Graziano Liberati <http://www.liberati.org>
  15.  * @copyright  2004-2007 The ZNF Development Team
  16.  * @license    LGPL License 2.1 <http://www.gnu.org/copyleft/lesser.html>
  17.  * @version    SVN $Id: DBConfig.php 43 2007-06-26 23:36:35Z aronnax $
  18.  * @since      Release 0.7.5
  19.  * @link       http://www.zeronotice.org
  20.  */
  21.  
  22. /**
  23.  * <i>ZNF_Config_DBConfig</i> contains the collection of static database configuration informations.
  24.  *
  25.  * Returns the <i>ZNF_Config_DBConfig</i> object reference with a singleton
  26.  * pattern.
  27.  *
  28.  * @access     public
  29.  * @package    ZNF
  30.  * @subpackage Config
  31.  * @author     Alessandro Rossini <http://www.alessandrorossini.org>
  32.  * @author     Graziano Liberati <http://www.liberati.org>
  33.  * @copyright  2004-2007 The ZNF Development Team
  34.  * @license    LGPL License 2.1 <http://www.gnu.org/copyleft/lesser.html>
  35.  * @version    SVN $Id: DBConfig.php 43 2007-06-26 23:36:35Z aronnax $
  36.  * @since      Release 0.7.5
  37.  * @link       http://www.zeronotice.org
  38.  */
  39. {
  40.  
  41.     /**
  42.      * Name of the configuration file.
  43.      */
  44.     const CONFIG_FILE 'znf-db-config.xml';
  45.  
  46.     /**
  47.      * The <i>ZNF_Config_DBConfig</i> object reference.
  48.      *
  49.      * @access protected
  50.      * @staticvar ZNF_Config_DBConfig $_dbConfig 
  51.      */
  52.     static protected $_dbConfig;
  53.  
  54.     /**
  55.      * The array of the configuration file.
  56.      *
  57.      * @access protected
  58.      * @var array $_config 
  59.      */
  60.     protected $_config;
  61.  
  62.     /**
  63.      * Private constructor to avoid <i>ZNF_Config_DBConfig</i> object creation.
  64.      *
  65.      * @access private
  66.      */
  67.     private function __construct()
  68.     {
  69.         $this->_parseConfiguration();
  70.     }
  71.  
  72.     /**
  73.      * Private clonator to avoid <i>ZNF_Config_DBConfig</i> object clonation.
  74.      *
  75.      * @access private
  76.      */
  77.     private function __clone()
  78.     {
  79.     }
  80.  
  81.     /**
  82.      * Returns the reference to the <i>ZNF_Config_DBConfig</i> object with a singleton pattern.
  83.      *
  84.      * This object is shared by all the classes that require the database
  85.      * configuration.
  86.      *
  87.      * @static
  88.      * @access public
  89.      * @return ZNF_Config_DBConfig 
  90.      */
  91.     static public function getInstance()
  92.     {
  93.         if (!(isset(self::$_dbConfig))) {
  94.             self::$_dbConfig new ZNF_Config_DBConfig();
  95.         }
  96.  
  97.         return self::$_dbConfig;
  98.     }
  99.  
  100.     /**
  101.      * Parses and validates the configuration file.
  102.      *
  103.      * For performance reasons implements a caching mechanism that serializes the
  104.      * configuration file in an internal structure.
  105.      *
  106.      * @access protected
  107.      */
  108.     protected function _parseConfiguration()
  109.     {
  110.         $xmlTimestamp filemtime(ZNF::CONFIG_DIR '/' self::CONFIG_FILE);
  111.         $serializedTimestamp @filemtime(ZNF::CACHE_DIR '/' self::CONFIG_FILE '.cache');
  112.  
  113.         if ($xmlTimestamp <= $serializedTimestamp{
  114.             $this->_config = unserialize(file_get_contents(ZNF::CACHE_DIR '/' self::CONFIG_FILE '.cache'));
  115.         else {
  116.             $configXml new DOMDocument('1.0','utf-8');
  117.             $configXml->load(ZNF::CONFIG_DIR '/' self::CONFIG_FILE);
  118.  
  119.             // quick hack to make ZNF work as a PEAR package, to be improved
  120.             $dirName dirname(__FILE__);
  121.             $schemaPath substr($dirName0strpos($dirName'Config')) "xsd/znf-db-config.xsd";
  122.             if (!$configXml->schemaValidate($schemaPath)) {
  123.                 $translation new ZNF_Presentation_Translation('ZNF'$_SESSION['znf']['lang']);
  124.                 throw new ZNF_Config_DBConfigException($translation->get('errorDbCfgFileNotValid'));
  125.             }
  126.  
  127.             $xpath new DOMXPath($configXml);
  128.             $xpath->registerNamespace('db''http://www.zeronotice.org/ZNF/znf-db-config');
  129.             $config array();
  130.             $default $xpath->query('/db:znf-db-config/db:db[@default="true"]');
  131.  
  132.             if ($default->length == 1{
  133.                 $config['db']['dbms']     $default->item(0)->getAttribute('dbms');
  134.                 $config['db']['username'$default->item(0)->getAttribute('username');
  135.                 $config['db']['password'$default->item(0)->getAttribute('password');
  136.                 $config['db']['hostname'$default->item(0)->getAttribute('hostname');
  137.                 $config['db']['dbname']   $default->item(0)->getAttribute('dbname');
  138.                 $config['db']['tableprefix']   $default->item(0)->getAttribute('tableprefix');
  139.             // check if only one default action is specified
  140.             elseif ($default->length 1{
  141.                 $translation new ZNF_Presentation_Translation('ZNF'$_SESSION['znf']['lang']);
  142.                 throw new ZNF_Config_DBConfigException($translation->get('errorDbCfgOneDbDefault'));
  143.             // check if at least one default action is specified
  144.             else {
  145.                 $translation new ZNF_Presentation_Translation('ZNF'$_SESSION['znf']['lang']);
  146.                 throw new ZNF_Config_DBConfigException($translation->get('errorDbCfgAtLeastOneDbDefault'));
  147.             }
  148.  
  149.             file_put_contents(ZNF::CACHE_DIR '/' self::CONFIG_FILE '.cache'serialize($config));
  150.             $this->_config = $config;
  151.         }
  152.     }
  153.  
  154.     /**
  155.      * Returns the database DBMS.
  156.      *
  157.      * @access public
  158.      * @return string 
  159.      */
  160.     public function getDbms()
  161.     {
  162.         return $this->_config['db']['dbms'];
  163.     }
  164.  
  165.     /**
  166.      * Returns the database username.
  167.      *
  168.      * @access public
  169.      * @return string 
  170.      */
  171.     public function getUsername()
  172.     {
  173.         return $this->_config['db']['username'];
  174.     }
  175.  
  176.     /**
  177.      * Returns the database password.
  178.      *
  179.      * @access public
  180.      * @return string 
  181.      */
  182.     public function getPassword()
  183.     {
  184.         return $this->_config['db']['password'];
  185.     }
  186.  
  187.     /**
  188.      * Returns the database hostname.
  189.      *
  190.      * @access public
  191.      * @return string 
  192.      */
  193.     public function getHostname()
  194.     {
  195.         return $this->_config['db']['hostname'];
  196.     }
  197.  
  198.     /**
  199.      * Returns the database dbname.
  200.      *
  201.      * @access public
  202.      * @return string 
  203.      */
  204.     public function getDbname()
  205.     {
  206.         return $this->_config['db']['dbname'];
  207.     }
  208.  
  209.     /**
  210.      * Returns the database table prefix.
  211.      *
  212.      * @access public
  213.      * @return string 
  214.      */
  215.     public function getTablePrefix()
  216.     {
  217.         return $this->_config['db']['tableprefix'];
  218.     }
  219.  
  220.     /**
  221.      * Destroys the <i>ZNF_Config_DBConfig</i> object.
  222.      *
  223.      * @access public
  224.      */
  225.     public function __destruct()
  226.     {
  227.     }
  228.  
  229. }
  230.  
  231. /**
  232.  * <i>ZNF_Config_DBConfigException</i> is the exception type for the <i>ZNF_Config_DBConfig</i> class.
  233.  *
  234.  * <i>ZNF_Config_DBConfigException</i> extends the <i>Exception</i> class of PHP5.
  235.  *
  236.  * @access     public
  237.  * @package    ZNF
  238.  * @subpackage Config
  239.  * @author     Alessandro Rossini <http://www.alessandrorossini.org>
  240.  * @author     Graziano Liberati <http://www.liberati.org>
  241.  * @copyright  2004-2007 The ZNF Development Team
  242.  * @license    LGPL License 2.1 <http://www.gnu.org/copyleft/lesser.html>
  243.  * @version    SVN $Id: DBConfig.php 43 2007-06-26 23:36:35Z aronnax $
  244.  * @since      Release 0.7.5
  245.  * @link       http://www.zeronotice.org
  246.  */
  247. class ZNF_Config_DBConfigException extends Exception
  248. {
  249. }
  250.  
  251. ?>

Documentation generated on Wed, 14 Nov 2007 23:47:36 +0100 by phpDocumentor 1.4.0