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

Source for file GenericConfigurator.php

Documentation is available at GenericConfigurator.php

  1. <?php
  2. /**
  3.  * @copyright Copyright 2005-2010 RedIRIS, http://www.rediris.es/
  4.  *
  5.  *  This file is part of phpPoA2.
  6.  *
  7.  *  phpPoA2 is free software: you can redistribute it and/or modify
  8.  *  it under the terms of the GNU General Public License as published by
  9.  *  the Free Software Foundation, either version 3 of the License, or
  10.  *  (at your option) any later version.
  11.  *
  12.  *  phpPoA2 is distributed in the hope that it will be useful,
  13.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  *  GNU General Public License for more details.
  16.  *
  17.  *  You should have received a copy of the GNU General Public License
  18.  *  along with phpPoA2. If not, see <http://www.gnu.org/licenses/>.
  19.  *
  20.  * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
  21.  * @version 2.0
  22.  * @author Jaime Perez <jaime.perez@rediris.es>
  23.  * @filesource
  24.  */
  25.  
  26. /**
  27.  * Generic configuration class.
  28.  * @abstract
  29.  * @package phpPoA2
  30.  */
  31. abstract class GenericConfigurator {
  32.  
  33.     protected $cfg = array();
  34.     protected $mandatory_options = array();
  35.  
  36.     /**
  37.      * Main constructor.
  38.      * @param file The file which stores the configuration.
  39.      * @param site The section of the configuration that applies for the current site.
  40.      * @throws PoAException if any error occurs.
  41.      */
  42.     public function __construct($file$site{
  43.         // configure
  44.         if (is_readable($file)) {
  45.             $this->configure($file$site);
  46.         else {
  47.             throw new PoAException('config-not-found'CONFIG_ERRarray($file));
  48.         }
  49.         // check configuration
  50.         $this->validate();
  51.     }
  52.  
  53.     /**
  54.      * Read the configuration from the specified file and section.
  55.      * @param file The file which stores the configuration.
  56.      * @param site The section of the configuration that applies for the current site.
  57.      * @throws PoAException if any error occurs.
  58.      */
  59.     protected function configure($file$site{
  60.         // read config from file
  61.         include $file;
  62.  
  63.         // determine the configuration variable name by means of this class name
  64.         $name strtolower(str_replace("Configurator"""get_class($this)))."_cfg";
  65.         $set = eval("return isset(\$".$name.");");
  66.         if (!$set{
  67.             throw new PoAException('config-err-php'CONFIG_ERRarray($name$site));
  68.         }
  69.  
  70.         // merge configurations
  71.         $ev = eval("return array_merge(\$this->cfg, $".$name.");");
  72.         if (is_array($ev)) $this->cfg = $ev;
  73.         $ev = eval("return @array_merge(\$this->cfg, $".$name."[\"".$site."\"]);");
  74.         if (is_array($ev)) $this->cfg = $ev;
  75.         unset($this->cfg[$site]);
  76.     }
  77.  
  78.     /**
  79.      * Check all mandatory attributes are set.
  80.      * @param mandatory Optional. An array of mandatory attributes that should be searched within the current configuration.
  81.      * @return boolean true if success.
  82.      * @throws PoAException if any error occurs.
  83.      */
  84.     public function validate($mandatory null{
  85.         $options $this->mandatory_options;
  86.         if ($mandatory$options $mandatory;
  87.  
  88.         // validate mandatory params
  89.         foreach ($options as $option{
  90.             if (!isset($this->cfg[$option])) {
  91.                 throw new PoAException('config-param-err'CONFIG_ERRarray($option));
  92.             }
  93.         }
  94.         return true;
  95.     }
  96.  
  97. }
  98.  
  99. ?>

Documentation generated on Tue, 25 Jan 2011 11:24:29 +0100 by phpDocumentor 1.4.3