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.     protected $handler;
  38.  
  39.     /**
  40.      * Main constructor for the engine.
  41.      * @param file The path to the configuration file. Can be in the include path.
  42.      * @param section The section of the configuration file, if any.
  43.      */
  44.     public function __construct($file$site{
  45.         $this->configure($file$site);
  46.     }
  47.  
  48.     /**
  49.      * Configure the engine.
  50.      * @param file The path to the configuration file. Can be in the include path.
  51.      * @param section The section of the configuration file, if any.
  52.      * @return void 
  53.      */
  54.     public function configure($file,$section{
  55.         // search and initialize configurator
  56.         $configurator str_replace($this->engine_type."Engine"""get_class($this))."Configurator";
  57.         $this->cfg = new $configurator($file,$section);
  58.  
  59.          // initialize hooks
  60.         foreach ($this->valid_hooks as $hook$this->hooks[$hookarray();
  61.     }
  62.  
  63.     /**
  64.      * Adds a function to the specified hook, which will be executed at some point of the code.
  65.      * @param name The name of the hook.
  66.      * @param hook A mixed object. Can be the name of a function (string) or
  67.      *  an array with two elements: the former, the name of a class or an object,
  68.      *  and the latter the name of the method.
  69.      * @return boolean true if successful, false in any other case.
  70.      */
  71.     public function addHook($name$hook{
  72.          // check if the hook exists
  73.          if (!in_array($name$this->valid_hooks)) return false;
  74.  
  75.          // check if its a hook
  76.          if (!($hook instanceof Hook)) return false;
  77.  
  78.          // check if the hook is registered
  79.          if (in_array($hook$this->hooks[$name])) return false;
  80.  
  81.          trigger_error(PoAUtils::msg('add-hook'array($hook->getName()$name)));
  82.          $this->hooks[$name][$hook;
  83.     }
  84.  
  85.     /**
  86.      * Removes a function fromt he specified hook.
  87.      * @param name The name of the hook.
  88.      * @param hook A mixed object. Can be the name of a function (string) or
  89.      *  an array with two elements: the former, the name of a class or an object,
  90.      *  and the latter the name of the method.
  91.      * @return boolean true if successful, false in any other case.
  92.      */
  93.     public function removeHook($name$hook{
  94.          // check if the hook exists
  95.          if (!in_array($name$this->valid_hooks)) return false;
  96.  
  97.          // check if the hook is registered
  98.          if (!in_array($hook$this->hooks[$name])) return false;
  99.  
  100.          // search and remove
  101.          $new array();
  102.          foreach ($this->hooks[$nameas $item{
  103.              if ($item != $hook{
  104.                  $new[$item;
  105.              }
  106.          }
  107.          $this->hooks[$name$new;
  108.  
  109.          trigger_error(PoAUtils::msg('remove-hook'array($hook->getName()$name)));
  110.          return true;
  111.     }
  112.  
  113.     /**
  114.      * Run all hooks attached to an specific action.
  115.      * @param hook The name of the hook.
  116.      * @param params An array with all params (in order) that must be passed to the function.
  117.      */
  118.     protected function runHooks($hook&$params{
  119.         // check if the hook exists
  120.         if (!in_array($hook$this->valid_hooks)) return false;
  121.  
  122.         foreach ($this->hooks[$hookas $h{
  123.             trigger_error(PoAUtils::msg('running-hook'array($h->getName()$hook)));
  124.             if ($h->run($params)) break;
  125.         }
  126.     }
  127.  
  128.     /**
  129.      * Set the event handler to the one specified.
  130.      * @param handler The event handler to use.
  131.      */
  132.     public function setHandler($handler{
  133.         $this->handler = $handler;
  134.     }
  135.  
  136.     /**
  137.      * Register error and exception handlers for logging. Use it only for methods not declared
  138.      * in the interface that could trigger errors.
  139.      */
  140.     protected function registerHandler({
  141.         // register autoload function
  142.         set_exception_handler(array($this->handler"exceptionHandler"));
  143.         set_error_handler(array($this->handler"errorHandler"));
  144.     }
  145.  
  146.     /**
  147.      * Unregister error and exception handlers. Use it only for methods not declared in the
  148.      * interface that previously called registerHandler() method.
  149.      */
  150.     protected function clean({
  151.         // clean
  152.         restore_exception_handler();
  153.         restore_error_handler();
  154.     }
  155.  
  156. }
  157.  
  158. ?>

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