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

Source for file AppConfig.php

Documentation is available at AppConfig.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.  * @author     Tomasz Kuter <evolic@interia.pl>
  16.  * @copyright  2004-2007 The ZNF Development Team
  17.  * @license    LGPL License 2.1 <http://www.gnu.org/copyleft/lesser.html>
  18.  * @version    SVN $Id: AppConfig.php 44 2007-07-22 23:55:30Z evolic $
  19.  * @since      Release 0.7.5
  20.  * @link       http://www.zeronotice.org
  21.  */
  22.  
  23. /**
  24.  * <i>ZNF_Config_AppConfig</i> contains the collection of static application configuration informations.
  25.  *
  26.  * Returns the <i>ZNF_Config_AppConfig</i> object reference with a singleton
  27.  * pattern.
  28.  *
  29.  * @access     public
  30.  * @package    ZNF
  31.  * @subpackage Config
  32.  * @author     Alessandro Rossini <http://www.alessandrorossini.org>
  33.  * @author     Graziano Liberati <http://www.liberati.org>
  34.  * @author     Tomasz Kuter <evolic@interia.pl>
  35.  * @copyright  2004-2007 The ZNF Development Team
  36.  * @license    LGPL License 2.1 <http://www.gnu.org/copyleft/lesser.html>
  37.  * @version    SVN $Id: AppConfig.php 44 2007-07-22 23:55:30Z evolic $
  38.  * @since      Release 0.7.5
  39.  * @link       http://www.zeronotice.org
  40.  */
  41. {
  42.  
  43.     /**
  44.      * Name of the configuration file.
  45.      */
  46.     const CONFIG_FILE 'znf-app-config.xml';
  47.  
  48.     /**
  49.      * The <i>ZNF_Config_AppConfig</i> object reference.
  50.      *
  51.      * @access protected
  52.      * @staticvar ZNF_Config_AppConfig $_appConfig 
  53.      */
  54.     static protected $_appConfig;
  55.  
  56.     /**
  57.      * The array of the configuration file.
  58.      *
  59.      * @access protected
  60.      * @var array $_config 
  61.      */
  62.     protected $_config;
  63.  
  64.     /**
  65.      * Private constructor to avoid <i>ZNF_Config_AppConfig</i> object creation.
  66.      *
  67.      * @access private
  68.      */
  69.     private function __construct()
  70.     {
  71.         $this->_parseConfiguration();
  72.     }
  73.  
  74.     /**
  75.      * Private clonator to avoid <i>ZNF_Config_AppConfig</i> object clonation.
  76.      *
  77.      * @access private
  78.      */
  79.     private function __clone()
  80.     {
  81.     }
  82.  
  83.     /**
  84.      * Returns the reference to the <i>ZNF_Config_AppConfig</i> object with a singleton pattern.
  85.      *
  86.      * This object is shared by all the classes that require the application
  87.      * configuration.
  88.      *
  89.      * @static
  90.      * @access public
  91.      * @return ZNF_Config_AppConfig 
  92.      */
  93.     static public function getInstance()
  94.     {
  95.         if (!(isset(self::$_appConfig))) {
  96.             self::$_appConfig new ZNF_Config_AppConfig();
  97.         }
  98.  
  99.         return self::$_appConfig;
  100.     }
  101.  
  102.     /**
  103.      * Parses and validates the configuration file.
  104.      *
  105.      * For performance reasons implements a caching mechanism that serializes the
  106.      * configuration file in an internal structure.
  107.      *
  108.      * @access protected
  109.      */
  110.     protected function _parseConfiguration()
  111.     {
  112.         $xmlTimestamp filemtime(ZNF::CONFIG_DIR '/' self::CONFIG_FILE);
  113.         $serializedTimestamp @filemtime(ZNF::CACHE_DIR ZNF::CACHE_DIR '/' self::CONFIG_FILE '.cache');
  114.  
  115.         if ($xmlTimestamp <= $serializedTimestamp{
  116.             $this->_config = unserialize(file_get_contents(ZNF::CACHE_DIR '/' self::CONFIG_FILE '.cache'));
  117.         else {
  118.             $configXml new DOMDocument('1.0','utf-8');
  119.             $configXml->load(ZNF::CONFIG_DIR '/' self::CONFIG_FILE);
  120.  
  121.             if (!$configXml{
  122.                 throw new ZNF_Config_AppConfigException('Error loading the application configuration file, file is not well formed');
  123.             }
  124.  
  125.             // quick hack to make ZNF work as a PEAR package, to be improved
  126.             $dirName dirname(__FILE__);
  127.             $schemaPath substr($dirName0strpos($dirName'Config')) "xsd/znf-app-config.xsd";
  128.             if (!$configXml->schemaValidate($schemaPath)) {
  129.                 throw new ZNF_Config_AppConfigException('Error loading the application configuration file, file is not valid');
  130.             }
  131.  
  132.             $xpath new DOMXPath($configXml);
  133.             $xpath->registerNamespace('app''http://www.zeronotice.org/ZNF/znf-app-config');
  134.             $config array();
  135.  
  136.             //get app settings
  137.             $appDefaults $xpath->query('/app:znf-app-config/app:app-settings');
  138.             $config['app-settings']['theme']      $appDefaults->item(0)->getAttribute('theme');
  139.  
  140.             if (!file_exists(ZNF::THEME_DIR "/{$config['app-settings']['theme']}")) {
  141.                 throw new ZNF_Config_AppConfigException('Error loading the application configuration file, the selected theme is not present');
  142.             }
  143.  
  144.             $config['app-settings']['lang']       $appDefaults->item(0)->getAttribute('lang');
  145.  
  146.             if (!file_exists(ZNF::THEME_DIR "/{$config['app-settings']['theme']}/{$config['app-settings']['lang']}")) {
  147.                 throw new ZNF_Config_AppConfigException('Error loading the application configuration file, the selected language is not present');
  148.             }
  149.  
  150.             $config['app-settings']['locale']     $appDefaults->item(0)->getAttribute('locale');
  151.             $config['app-settings']['langAutodetect'$appDefaults->item(0)->getAttribute('langAutodetect');
  152.             $config['app-settings']['sessionId']  $appDefaults->item(0)->getAttribute('sessionId');
  153.  
  154.             // get default module
  155.             $default $xpath->query('/app:znf-app-config/app:modules/app:module[@default="true"]');
  156.  
  157.             if ($default->length == 1{
  158.                 $config['modules']['default']['name']      $default->item(0)->getAttribute('name');
  159.                 $config['modules']['default']['config']    $default->item(0)->getAttribute('config');
  160.                 $config['modules']['default']['processor'$default->item(0)->getAttribute('processor');
  161.  
  162.             // check if only one default module is defined
  163.             elseif ($default->length 1{
  164.                 throw new ZNF_Config_ModulesConfigException('Error loading the module configuration file, only one module can be default');
  165.  
  166.             // check if at least one default module is defined
  167.             else {
  168.                 throw new ZNF_Config_ModulesConfigException('Error loading the module configuration file, at least one module must be default');
  169.             }
  170.  
  171.             // get modules
  172.             $modules $xpath->query('/app:znf-app-config/app:modules/app:module');
  173.  
  174.             foreach ($modules as $module{
  175.                 $moduleName $module->getAttribute('name');
  176.                 $config['modules'][$moduleName]['name']      $moduleName;
  177.                 $config['modules'][$moduleName]['config']    $module->getAttribute('config');
  178.                 $config['modules'][$moduleName]['processor'$module->getAttribute('processor');
  179.             }
  180.  
  181.             // get authentication configuration
  182.             $authConfig $xpath->query('/app:znf-app-config/app:auth-config');
  183.  
  184.             if ($authConfig->length 0{
  185.                 $config['authConfig']['path'$authConfig->item(0)->getAttribute('path');
  186.             }
  187.  
  188.             // get wrong authorization configuration
  189.             $wrongAuthConfig $xpath->query('/app:znf-app-config/app:wrong-auth-config');
  190.  
  191.             if ($wrongAuthConfig->length 0{
  192.                 $config['wrongAuthConfig']['path'$wrongAuthConfig->item(0)->getAttribute('path');
  193.             }
  194.  
  195.             file_put_contents(ZNF::CACHE_DIR '/' self::CONFIG_FILE '.cache'serialize($config));
  196.             $this->_config = $config;
  197.         }
  198.     }
  199.  
  200.     /**
  201.      * Returns the application lang.
  202.      *
  203.      * @access public
  204.      * @return string 
  205.      */
  206.     public function getLang()
  207.     {
  208.         return $this->_config['app-settings']['lang'];
  209.     }
  210.  
  211.     /**
  212.      * Returns the application locale.
  213.      *
  214.      * @access public
  215.      * @return string 
  216.      */
  217.     public function getLocale()
  218.     {
  219.         return $this->_config['app-settings']['locale'];
  220.     }
  221.  
  222.     /**
  223.      * Returns the application autodetected language.
  224.      *
  225.      * @access public
  226.      * @return string 
  227.      */
  228.     public function getLangAutodetect()
  229.     {
  230.         return $this->_config['app-settings']['langAutodetect'];
  231.     }
  232.  
  233.     /**
  234.      * Returns the application session ID.
  235.      *
  236.      * @access public
  237.      * @return string 
  238.      */
  239.     public function getSessionId()
  240.     {
  241.         return $this->_config['app-settings']['sessionId'];
  242.     }
  243.  
  244.     /**
  245.      * Returns the application theme.
  246.      *
  247.      * @access public
  248.      * @return string 
  249.      */
  250.     public function getTheme()
  251.     {
  252.         return $this->_config['app-settings']['theme'];
  253.     }
  254.  
  255.     /**
  256.      * Returns the module for the defined <i>name</i>.
  257.      *
  258.      * @access public
  259.      * @param string $module 
  260.      * @return array 
  261.      */
  262.     public function findModule($module)
  263.     {
  264.         if ($module && array_key_exists($module$this->_config['modules'])) {
  265.  
  266.             // extracts the configuration of the module requested
  267.             return $this->_config['modules'][$module];
  268.         else {
  269.  
  270.             // extracts the configuration of the default module
  271.             return $this->_config['modules']['default'];
  272.         }
  273.     }
  274.  
  275.     /**
  276.      * Returns the authentication configuration.
  277.      *
  278.      * @access public
  279.      * @return array 
  280.      */
  281.     public function findAuthConfig()
  282.     {
  283.         if (isset($this->_config['authConfig'])) {
  284.  
  285.             // extracts the authentication configuration
  286.             return $this->_config['authConfig'];
  287.         }
  288.  
  289.         // authentication configuration not found
  290.         return null;
  291.     }
  292.  
  293.     /**
  294.      * Returns the wrong authentication configuration.
  295.      *
  296.      * @access public
  297.      * @return array 
  298.      */
  299.     public function findWrongAuthConfig()
  300.     {
  301.         if (isset($this->_config['wrongAuthConfig'])) {
  302.  
  303.             // extracts the authentication configuration
  304.             return $this->_config['wrongAuthConfig'];
  305.         }
  306.  
  307.         // authentication configuration not found
  308.         return null;
  309.     }
  310.  
  311.     /**
  312.      * Destroys the <i>ZNF_Config_AppConfig</i> object.
  313.      *
  314.      * @access public
  315.      */
  316.     public function __destruct()
  317.     {
  318.     }
  319.  
  320. }
  321.  
  322. /**
  323.  * <i>ZNF_Config_AppConfigException</i> is the exception type for the <i>ZNF_Config_AppConfig</i> class.
  324.  *
  325.  * <i>ZNF_Config_AppConfigException</i> extends the <i>Exception</i> class of PHP5.
  326.  *
  327.  * @access     public
  328.  * @package    ZNF
  329.  * @subpackage Config
  330.  * @author     Alessandro Rossini <http://www.alessandrorossini.org>
  331.  * @author     Graziano Liberati <http://www.liberati.org>
  332.  * @copyright  2004-2007 The ZNF Development Team
  333.  * @license    LGPL License 2.1 <http://www.gnu.org/copyleft/lesser.html>
  334.  * @version    SVN $Id: AppConfig.php 44 2007-07-22 23:55:30Z evolic $
  335.  * @since      Release 0.7.5
  336.  * @link       http://www.zeronotice.org
  337.  */
  338. class ZNF_Config_AppConfigException extends Exception
  339. {
  340. }
  341.  
  342. ?>

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