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

Documentation generated on Wed, 13 Oct 2010 15:06:17 +0200 by phpDocumentor 1.4.3