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

Source for file GenericEngine.php

Documentation is available at GenericEngine.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.  * A generic engine class.
  28.  * @abstract
  29.  * @package phpPoA2
  30.  */
  31. abstract class GenericEngine {
  32.  
  33.     protected $cfg;
  34.     protected $hooks = array();
  35.     protected $valid_hooks = array();
  36.     protected $engine_type;
  37.  
  38.     /**
  39.      * Main constructor for the engine.
  40.      * @param file The path to the configuration file. Can be in the include path.
  41.      * @param section The section of the configuration file, if any.
  42.      */
  43.     public function __construct($file$site{
  44.         $this->configure($file$site);
  45.     }
  46.  
  47.     /**
  48.      * Configure the engine.
  49.      * @param file The path to the configuration file. Can be in the include path.
  50.      * @param section The section of the configuration file, if any.
  51.      * @return void 
  52.      */
  53.     public function configure($file,$section{
  54.         // search and initialize configurator
  55.         $configurator str_replace($this->engine_type."Engine"""get_class($this))."Configurator";
  56.         $this->cfg = new $configurator($file,$section);
  57.  
  58.          // initialize hooks
  59.         foreach ($this->valid_hooks as $hook$this->hooks[$hookarray();
  60.     }
  61.  
  62.     /**
  63.      * Adds a function to the specified hook, which will be executed at some point of the code.
  64.      * @param name The name of the hook.
  65.      * @param hook A mixed object. Can be the name of a function (string) or
  66.      *  an array with two elements: the former, the name of a class or an object,
  67.      *  and the latter the name of the method.
  68.      * @return boolean true if successful, false in any other case.
  69.      */
  70.     public function addHook($name$hook{
  71.          // check if the hook exists
  72.          if (!in_array($name$this->valid_hooks)) return false;
  73.  
  74.          // check if its a hook
  75.          if (!($hook instanceof Hook)) return false;
  76.  
  77.          // check if the hook is registered
  78.          if (in_array($hook$this->hooks[$name])) return false;
  79.  
  80.          trigger_error(msg('add-hook'array($hook->getName()$name)));
  81.          $this->hooks[$name][$hook;
  82.     }
  83.  
  84.     /**
  85.      * Removes a function fromt he specified hook.
  86.      * @param name The name of the hook.
  87.      * @param hook A mixed object. Can be the name of a function (string) or
  88.      *  an array with two elements: the former, the name of a class or an object,
  89.      *  and the latter the name of the method.
  90.      * @return boolean true if successful, false in any other case.
  91.      */
  92.     public function removeHook($name$hook{
  93.          // check if the hook exists
  94.          if (!in_array($name$this->valid_hooks)) return false;
  95.  
  96.          // check if the hook is registered
  97.          if (!in_array($hook$this->hooks[$name])) return false;
  98.  
  99.          // search and remove
  100.          $new array();
  101.          foreach ($this->hooks[$nameas $item{
  102.              if ($item != $hook{
  103.                  $new[$item;
  104.              }
  105.          }
  106.          $this->hooks[$name$new;
  107.  
  108.          trigger_error(msg('remove-hook'array($hook->getName()$name)));
  109.          return true;
  110.     }
  111.  
  112.     /**
  113.      * Run all hooks attached to an specific action.
  114.      * @param hook The name of the hook.
  115.      * @param params An array with all params (in order) that must be passed to the function.
  116.      */
  117.     protected function runHooks($hook&$params{
  118.         // check if the hook exists
  119.         if (!in_array($hook$this->valid_hooks)) return false;
  120.  
  121.         foreach ($this->hooks[$hookas $h{
  122.             trigger_error(msg('running-hook'array($h->getName()$hook)));
  123.             if ($h->run($params)) break;
  124.         }
  125.     }
  126.  
  127. }
  128.  
  129. ?>

Documentation generated on Thu, 26 Aug 2010 13:38:48 +0200 by phpDocumentor 1.4.3