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

Source for file ModulesConfig.php

Documentation is available at ModulesConfig.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: ModulesConfig.php 43 2007-06-26 23:36:35Z aronnax $
  18.  * @since      Release 0.6.1
  19.  * @link       http://www.zeronotice.org
  20.  */
  21.  
  22. /**
  23.  * <i>ZNF_Config_ModulesConfig</i> contains the collection of static module configuration informations.
  24.  *
  25.  * Returns the <i>ZNF_Config_ModulesConfig</i> object reference of a specified
  26.  * module with a singleton 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: ModulesConfig.php 43 2007-06-26 23:36:35Z aronnax $
  36.  * @since      Release 0.6.1
  37.  * @link       http://www.zeronotice.org
  38.  */
  39. {
  40.  
  41.     /**
  42.      * The array of <i>ZNF_Config_ModulesConfig</i> object references.
  43.      *
  44.      * @access protected
  45.      * @staticvar array $_modulesConfig 
  46.      */
  47.     static protected $_modulesConfig;
  48.  
  49.     /**
  50.      * The array of the configuration file.
  51.      *
  52.      * @access protected
  53.      * @var array $_config 
  54.      */
  55.     protected $_config;
  56.  
  57.     /**
  58.      * Private constructor to avoid <i>ZNF_Config_ModulesConfig</i> object creation.
  59.      *
  60.      * @access private
  61.      * @param array $appModuleConfig The module's application configuration
  62.      */
  63.     private function __construct($appModuleConfig)
  64.     {
  65.         $this->_parseConfiguration($appModuleConfig);
  66.     }
  67.  
  68.     /**
  69.      * Private clonator to avoid <i>ZNF_Config_ModulesConfig</i> object clonation.
  70.      *
  71.      * @access private
  72.      */
  73.     private function __clone()
  74.     {
  75.     }
  76.  
  77.     /**
  78.      * Returns the reference to the <i>ZNF_Config_ModulesConfig</i> object with a singleton pattern.
  79.      *
  80.      * This object is shared by all the classes that require the module
  81.      * configuration.
  82.      *
  83.      * @static
  84.      * @access public
  85.      * @param array $appModuleConfig 
  86.      * @return ZNF_Config_ModulesConfig 
  87.      */
  88.     static public function getInstance($appModuleConfig)
  89.     {
  90.         if (!(isset(self::$_modulesConfig[$appModuleConfig['name']]))) {
  91.             self::$_modulesConfig[$appModuleConfig['name']] new ZNF_Config_ModulesConfig($appModuleConfig);
  92.         }
  93.  
  94.         return self::$_modulesConfig[$appModuleConfig['name']];
  95.     }
  96.  
  97.     /**
  98.      * Parses and validates the configuration file.
  99.      *
  100.      * For performance reasons implements a caching mechanism that serializes the
  101.      * configuration file in an internal structure.
  102.      *
  103.      * @access protected
  104.      * @param array $appModuleConfig 
  105.      */
  106.     protected function _parseConfiguration($appModuleConfig)
  107.     {
  108.         $serializedTimestamp @filemtime(ZNF::CACHE_DIR "/{$appModuleConfig['config']}.cache");
  109.         $xmlTimestamp filemtime(ZNF::CONFIG_DIR "/{$appModuleConfig['config']}");
  110.  
  111.         if ($xmlTimestamp <= $serializedTimestamp{
  112.             $this->_config = unserialize(file_get_contents(ZNF::CACHE_DIR "/{$appModuleConfig['config']}.cache"));
  113.         else {
  114.             $configXml new DOMDocument('1.0','utf-8');
  115.             $configXml->load(ZNF::CONFIG_DIR "/{$appModuleConfig['config']}");
  116.  
  117.             // quick hack to make ZNF work as a PEAR package, to be improved
  118.             $dirName dirname(__FILE__);
  119.             $schemaPath substr($dirName0strpos($dirName'Config')) "xsd/znf-module-config.xsd";
  120.             if (!$configXml->schemaValidate($schemaPath)) {
  121.                 $translation new ZNF_Presentation_Translation('ZNF'$_SESSION['znf']['lang']);
  122.                 throw new ZNF_Config_ModulesConfigException($translation->get('errorModCfgFileNotValid'));
  123.             }
  124.  
  125.             $xpath new DOMXPath($configXml);
  126.             $xpath->registerNamespace('mod''http://www.zeronotice.org/ZNF/znf-module-config');
  127.             $config array();
  128.  
  129.             // get default action
  130.             $default $xpath->query('/mod:znf-module-config/mod:action-mappings/mod:action[@default="true"]');
  131.  
  132.             if ($default->length == 1{
  133.                 $forward $default->item(0)->getAttribute('forward');
  134.                 $type    $default->item(0)->getAttribute('type');
  135.  
  136.                 // check if only one of forward or type attribute is defined
  137.                 if ($forward && $type{
  138.                     $translation new ZNF_Presentation_Translation('ZNF'$_SESSION['znf']['lang']);
  139.                     throw new ZNF_Config_ModulesConfigException($translation->get('errorModCfgForwardTypeOr'));
  140.                 }
  141.  
  142.                 // check if forward is a valid action
  143.                 if ($forward && (!ereg('/'$forward|| ereg('//'$forward))) {
  144.  
  145.                     // TODO: add the checking of valid action in other module
  146.                     if (!ereg('//'$forward)) {
  147.                         $forwardAction $xpath->query('/mod:znf-module-config/mod:action-mappings/mod:action[@path="' $forward '"]');
  148.  
  149.                         if (!$forwardAction->length{
  150.                             $translation new ZNF_Presentation_Translation('ZNF'$_SESSION['znf']['lang']);
  151.                             throw new ZNF_Config_ModulesConfigException($translation->get('errorModCfgForwardAttributeValidAction'));
  152.                         }
  153.                     }
  154.                 }
  155.  
  156.                 $config['action']['default']['forward']  $forward;
  157.                 $config['action']['default']['include']  $default->item(0)->getAttribute('include');
  158.                 $scope $default->item(0)->getAttribute('scope');
  159.                 $name $default->item(0)->getAttribute('name');
  160.  
  161.                 // check if name is defined when scope is defined
  162.                 if ($scope && !$name{
  163.                     $translation new ZNF_Presentation_Translation('ZNF'$_SESSION['znf']['lang']);
  164.                     throw new ZNF_Config_ModulesConfigException($translation->get('errorModCfgScopeDefinedNameValidFormBean'));
  165.                 }
  166.  
  167.                 // check if name refers to a valid form-bean
  168.                 if ($name{
  169.                     $form $xpath->query('/mod:znf-module-config/mod:form-beans/mod:form-bean[@name="' $name '"]');
  170.  
  171.                     if (!$form->length{
  172.                         $translation new ZNF_Presentation_Translation('ZNF'$_SESSION['znf']['lang']);
  173.                         throw new ZNF_Config_ModulesConfigException($translation->get('errorModCfgNameValidFormBean'));
  174.                     }
  175.  
  176.                 }
  177.  
  178.                 $parameter $default->item(0)->getAttribute('parameter');
  179.                 $parameterValue $default->item(0)->getAttribute('parameterValue');
  180.  
  181.                 // check if only one of forward or type attribute is defined
  182.                 if ($parameter && $parameterValue{
  183.                     $translation new ZNF_Presentation_Translation('ZNF'$_SESSION['znf']['lang']);
  184.                     throw new ZNF_Config_ModulesConfigException($translation->get('errorModCfgParameterParameterValueOr'));
  185.                 }
  186.  
  187.                 $config['action']['default']['parameter'$parameter;
  188.                 $config['action']['default']['parameterValue'$parameterValue;
  189.  
  190.                 $config['action']['default']['scope']     $scope;
  191.                 $config['action']['default']['name']      $name;
  192.                 $config['action']['default']['nextPath']  $default->item(0)->getAttribute('nextPath');
  193.                 $config['action']['default']['path']      $default->item(0)->getAttribute('path');
  194.                 $input $default->item(0)->getAttribute('input');
  195.                 $validate $default->item(0)->getAttribute('validate');
  196.  
  197.                 // check if validate and input are both defined
  198.                 if ($input && !$validate{
  199.                     $translation new ZNF_Presentation_Translation('ZNF'$_SESSION['znf']['lang']);
  200.                     throw new ZNF_Config_ModulesConfigException($translation->get('errorModCfgInputValidateBoth'));
  201.                 elseif (!$input && $validate{
  202.                     $translation new ZNF_Presentation_Translation('ZNF'$_SESSION['znf']['lang']);
  203.                     throw new ZNF_Config_ModulesConfigException($translation->get('errorModCfgInputValidateBoth'));
  204.                 }
  205.  
  206.                 $config['action']['default']['input']    $input;
  207.                 $config['action']['default']['validate'$validate;
  208.                 $config['action']['default']['roles']    $default->item(0)->getAttribute('roles');
  209.                 $config['action']['default']['type']     $type;
  210.  
  211.                 // get forward elements associated with the action
  212.                 $forwards $xpath->query('/mod:znf-module-config/mod:action-mappings/mod:action[@path="' $default->item(0)->getAttribute('path''"]/mod:forward');
  213.  
  214.                 foreach ($forwards as $forward{
  215.                     $forwardPath $forward->getAttribute('path');
  216.                     $forwardRedirect $forward->getAttribute('redirect');
  217.  
  218.                     // check if redirect is required and if the path is an action
  219.                     if ($forwardRedirect == 'true'{
  220.  
  221.                         if ($forwardPath && ereg('/'$forwardPath&& !ereg('//'$forwardPath)) {
  222.                             $translation new ZNF_Presentation_Translation('ZNF'$_SESSION['znf']['lang']);
  223.                             throw new ZNF_Config_ModulesConfigException($translation->get('errorModCfgForwardElementRedirectTrueValidAction'));
  224.                         }
  225.  
  226.                     }
  227.  
  228.                     // check if the path is a valid action
  229.                     if ($forwardPath && (!ereg('/'$forwardPath|| ereg('//'$forwardPath))) {
  230.  
  231.                         // TODO: add the checking of valid action in other module
  232.                         if (!ereg('//'$forwardPath)) {
  233.                             $forwardAction $xpath->query('/mod:znf-module-config/mod:action-mappings/mod:action[@path="' $forwardPath '"]');
  234.  
  235.                             if (!$forwardAction->length{
  236.                                 $translation new ZNF_Presentation_Translation('ZNF'$_SESSION['znf']['lang']);
  237.                                 throw new ZNF_Config_ModulesConfigException($translation->get('errorModCfgForwardElementValidAction'));
  238.                             }
  239.                         }
  240.                     }
  241.  
  242.                     $forwardName $forward->getAttribute('name');
  243.                     $config['action']['default']['forwards-config'][$forwardName]['name']     $forwardName;
  244.                     $config['action']['default']['forwards-config'][$forwardName]['path']     $forwardPath;
  245.                     $config['action']['default']['forwards-config'][$forwardName]['redirect'$forwardRedirect;
  246.                 }
  247.  
  248.             // check if only one default action is defined
  249.             elseif ($default->length 1{
  250.                 $translation new ZNF_Presentation_Translation('ZNF'$_SESSION['znf']['lang']);
  251.                 throw new ZNF_Config_ModulesConfigException($translation->get('errorModCfgOneActionDefault'));
  252.  
  253.             // check if at least one default action is defined
  254.             else {
  255.                 $translation new ZNF_Presentation_Translation('ZNF'$_SESSION['znf']['lang']);
  256.                 throw new ZNF_Config_ModulesConfigException($translation->get('errorModCfgAtLeastOneActionDefault'));
  257.             }
  258.  
  259.             // get global forwards
  260.             $forwards $xpath->query('/mod:znf-module-config/mod:global-forwards/mod:forward');
  261.  
  262.             foreach ($forwards as $forward{
  263.                 $forwardPath $forward->getAttribute('path');
  264.                 $forwardRedirect $forward->getAttribute('redirect');
  265.  
  266.                 // check if redirect is required and if the path is an action
  267.                 if ($forwardRedirect == 'true'{
  268.  
  269.                     if ($forwardPath && ereg('/'$forwardPath&& !ereg('//'$forwardPath)) {
  270.                         $translation new ZNF_Presentation_Translation('ZNF'$_SESSION['znf']['lang']);
  271.                         throw new ZNF_Config_ModulesConfigException($translation->get('errorModCfgForwardElementRedirectTrueValidAction'));
  272.                     }
  273.  
  274.                 }
  275.  
  276.                 // check if the path is a valid action
  277.                 if ($forwardPath && (!ereg('/'$forwardPath|| ereg('//'$forwardPath))) {
  278.  
  279.                     // TODO: add the checking of valid action in other module
  280.                     if (!ereg('//'$forwardPath)) {
  281.                         $forwardAction $xpath->query('/mod:znf-module-config/mod:action-mappings/mod:action[@path="' $forwardPath '"]');
  282.  
  283.                         if (!$forwardAction->length{
  284.                             $translation new ZNF_Presentation_Translation('ZNF'$_SESSION['znf']['lang']);
  285.                             throw new ZNF_Config_ModulesConfigException($translation->get('errorModCfgForwardElementValidAction'));
  286.                         }
  287.                     }
  288.                 }
  289.  
  290.                 $forwardName $forward->getAttribute('name');
  291.                 $config['global-forwards'][$forwardName]['name']     $forwardName;
  292.                 $config['global-forwards'][$forwardName]['path']     $forward->getAttribute('path');
  293.                 $config['global-forwards'][$forwardName]['redirect'$forward->getAttribute('redirect');
  294.             }
  295.  
  296.             // get all actions
  297.             $actions $xpath->query('/mod:znf-module-config/mod:action-mappings/mod:action');
  298.  
  299.             foreach ($actions as $action{
  300.                 $path    $action->getAttribute('path');
  301.                 $forward $action->getAttribute('forward');
  302.                 $type    $action->getAttribute('type');
  303.  
  304.                 // check if only one of forward or type attribute is defined
  305.                 if ($forward && $type{
  306.                     $translation new ZNF_Presentation_Translation('ZNF'$_SESSION['znf']['lang']);
  307.                     throw new ZNF_Config_ModulesConfigException($translation->get('errorModCfgForwardTypeOr'));
  308.                 }
  309.  
  310.                 // check if forward is a valid action
  311.                 if ($forward && (!ereg('/'$forward|| ereg('//'$forward))) {
  312.  
  313.                     // TODO: add the checking of valid action in other module
  314.                     if (!ereg('//'$forward)) {
  315.                         $forwardAction $xpath->query('/mod:znf-module-config/mod:action-mappings/mod:action[@path="' $forward '"]');
  316.  
  317.                         if (!$forwardAction->length{
  318.                             $translation new ZNF_Presentation_Translation('ZNF'$_SESSION['znf']['lang']);
  319.                             throw new ZNF_Config_ModulesConfigException($translation->get('errorModCfgForwardAttributeValidAction'));
  320.                         }
  321.                     }
  322.                 }
  323.  
  324.                 $config['action'][$path]['forward'$forward;
  325.                 $config['action'][$path]['include'$action->getAttribute('include');
  326.                 $scope $action->getAttribute('scope');
  327.                 $name $action->getAttribute('name');
  328.  
  329.                 // check if name is defined when scope is defined
  330.                 if ($scope && !$name{
  331.                     $translation new ZNF_Presentation_Translation('ZNF'$_SESSION['znf']['lang']);
  332.                     throw new ZNF_Config_ModulesConfigException($translation->get('errorModCfgScopeDefinedNameValidFormBean'));
  333.                 }
  334.  
  335.                 // check if name refers to a valid form-bean
  336.                 if ($name{
  337.                     $form $xpath->query('/mod:znf-module-config/mod:form-beans/mod:form-bean[@name="' $name '"]');
  338.  
  339.                     if (!$form->length{
  340.                         $translation new ZNF_Presentation_Translation('ZNF'$_SESSION['znf']['lang']);
  341.                         throw new ZNF_Config_ModulesConfigException($translation->get('errorModCfgNameValidFormBean'));
  342.                     }
  343.  
  344.                 }
  345.  
  346.                 $parameter $action->getAttribute('parameter');
  347.                 $parameterValue $action->getAttribute('parameterValue');
  348.  
  349.                 // check if only one of forward or type attribute is defined
  350.                 if ($parameter && $parameterValue{
  351.                     $translation new ZNF_Presentation_Translation('ZNF'$_SESSION['znf']['lang']);
  352.                     throw new ZNF_Config_ModulesConfigException($translation->get('errorModCfgParameterParameterValueOr'));
  353.                 }
  354.  
  355.                 $config['action'][$path]['parameter'$parameter;
  356.                 $config['action'][$path]['parameterValue'$parameterValue;
  357.  
  358.                 $config['action'][$path]['scope']     $scope;
  359.                 $config['action'][$path]['name']      $name;
  360.                 $config['action'][$path]['nextPath']  $action->getAttribute('nextPath');
  361.                 $config['action'][$path]['path']      $action->getAttribute('path');
  362.                 $input $action->getAttribute('input');
  363.                 $validate $action->getAttribute('validate');
  364.  
  365.                 // check if validate and input are both defined
  366.                 if ($input && !$validate{
  367.                     $translation new ZNF_Presentation_Translation('ZNF'$_SESSION['znf']['lang']);
  368.                     throw new ZNF_Config_ModulesConfigException($translation->get('errorModCfgInputValidateBoth'));
  369.                 elseif (!$input && $validate{
  370.                     $translation new ZNF_Presentation_Translation('ZNF'$_SESSION['znf']['lang']);
  371.                     throw new ZNF_Config_ModulesConfigException($translation->get('errorModCfgInputValidateBoth'));
  372.                 }
  373.  
  374.                 $config['action'][$path]['input']    $input;
  375.                 $config['action'][$path]['validate'$validate;
  376.                 $config['action'][$path]['roles']    $action->getAttribute('roles');
  377.                 $config['action'][$path]['type']     $type;
  378.  
  379.                 // get forward elements associated with the action
  380.                 $forwards $xpath->query('/mod:znf-module-config/mod:action-mappings/mod:action[@path="' $path '"]/mod:forward');
  381.  
  382.                 foreach ($forwards as $forward{
  383.                     $forwardPath $forward->getAttribute('path');
  384.                     $forwardRedirect $forward->getAttribute('redirect');
  385.  
  386.                     // check if redirect is required and if the path is an action
  387.                     if ($forwardRedirect == 'true'{
  388.  
  389.                         if ($forwardPath && ereg('/'$forwardPath&& !ereg('//'$forwardPath)) {
  390.                             $translation new ZNF_Presentation_Translation('ZNF'$_SESSION['znf']['lang']);
  391.                             throw new ZNF_Config_ModulesConfigException($translation->get('errorModCfgForwardElementRedirectTrueValidAction'));
  392.                         }
  393.  
  394.                     }
  395.  
  396.                     // check if the path is a valid action
  397.                     if ($forwardPath && (!ereg('/'$forwardPath|| ereg('//'$forwardPath))) {
  398.  
  399.                         // TODO: add the checking of valid action in other module
  400.                         if (!ereg('//'$forwardPath)) {
  401.                             $forwardAction $xpath->query('/mod:znf-module-config/mod:action-mappings/mod:action[@path="' $forwardPath '"]');
  402.  
  403.                             if (!$forwardAction->length{
  404.                                 $translation new ZNF_Presentation_Translation('ZNF'$_SESSION['znf']['lang']);
  405.                                 throw new ZNF_Config_ModulesConfigException($translation->get('errorModCfgForwardElementValidAction'));
  406.                             }
  407.                         }
  408.                     }
  409.  
  410.                     $forwardName $forward->getAttribute('name');
  411.                     $config['action'][$path]['forwards-config'][$forwardName]['name']     $forwardName;
  412.                     $config['action'][$path]['forwards-config'][$forwardName]['path']     $forwardPath;
  413.                     $config['action'][$path]['forwards-config'][$forwardName]['redirect'$forwardRedirect;
  414.                 }
  415.             }
  416.  
  417.             // get form beans
  418.             $forms $xpath->query('/mod:znf-module-config/mod:form-beans/mod:form-bean');
  419.  
  420.             foreach ($forms as $form{
  421.                 $formName $form->getAttribute('name');
  422.                 $config['form-bean'][$formName]['name'$formName;
  423.                 $config['form-bean'][$formName]['type'$form->getAttribute('type');
  424.             }
  425.  
  426.             file_put_contents(ZNF::CACHE_DIR "/{$appModuleConfig['config']}.cache"serialize($config));
  427.             $this->_config = $config;
  428.         }
  429.     }
  430.  
  431.     /**
  432.      * Returns the global forwards configuration.
  433.      *
  434.      * @access public
  435.      * @return array 
  436.      */
  437.     public function getGlobalForwardsConfig()
  438.     {
  439.         if (array_key_exists('global-forwards'$this->_config)) {
  440.             return $this->_config['global-forwards'];
  441.         }
  442.  
  443.         return null;
  444.     }
  445.  
  446.     /**
  447.      * Returns the action configuration for the defined <i>path</i>.
  448.      *
  449.      * @access public
  450.      * @param string $path 
  451.      * @return array 
  452.      */
  453.     public function findActionConfig($path)
  454.     {
  455.         if ($path && array_key_exists($path$this->_config['action'])) {
  456.             // extracts the configuration of the action requested
  457.             return $this->_config['action'][$path];
  458.         }
  459.  
  460.         return null;
  461.     }
  462.  
  463.     /**
  464.      * Returns the form bean configuration for the defined <i>name</i>.
  465.      *
  466.      * @access public
  467.      * @param string $name 
  468.      * @return array 
  469.      */
  470.     public function findFormBeanConfig($name)
  471.     {
  472.         if ($name && array_key_exists($name$this->_config['form-bean'])) {
  473.             // extracts the configuration of the action requested
  474.             return $this->_config['form-bean'][$name];
  475.         }
  476.  
  477.         return null;
  478.     }
  479.  
  480.     /**
  481.      * Destroys the <i>ZNF_Config_ModulesConfig</i> object.
  482.      *
  483.      * @access public
  484.      */
  485.     public function __destruct()
  486.     {
  487.     }
  488.  
  489. }
  490.  
  491. /**
  492.  * <i>ZNF_Config_ModulesConfigException</i> is the exception type for the <i>ZNF_Config_ModulesConfig</i> class.
  493.  *
  494.  * <i>ZNF_Config_ModulesConfigException</i> extends the <i>Exception</i> class of PHP5.
  495.  *
  496.  * @access     public
  497.  * @package    ZNF
  498.  * @subpackage Config
  499.  * @author     Alessandro Rossini <http://www.alessandrorossini.org>
  500.  * @author     Graziano Liberati <http://www.liberati.org>
  501.  * @copyright  2004-2007 The ZNF Development Team
  502.  * @license    LGPL License 2.1 <http://www.gnu.org/copyleft/lesser.html>
  503.  * @version    SVN $Id: ModulesConfig.php 43 2007-06-26 23:36:35Z aronnax $
  504.  * @since      Release 0.6.1
  505.  * @link       http://www.zeronotice.org
  506.  */
  507. class ZNF_Config_ModulesConfigException extends Exception
  508. {
  509. }
  510.  
  511. ?>

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