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

Source for file Hook.php

Documentation is available at Hook.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.  * Hook class that allows to run a function or a method specified by their name.
  28.  * @package phpPoA2
  29.  */
  30. class Hook {
  31.  
  32.     protected $_hook;
  33.     protected $_callable_name;
  34.     protected $_name;
  35.  
  36.     /**
  37.      * Build a new hook.
  38.      * @param hook The name of a function or an array specifying a class and its method.
  39.      * @return boolean true if everything is ok.
  40.      * @throws PoAException if something goes wrong.
  41.      */
  42.     public function __construct($hook{
  43.         $printable str_replace(array("\n""\r""\t"" ")""print_r($hooktrue));
  44.  
  45.         // perform some sanity checks
  46.         // empty function
  47.         if (empty($hook))
  48.             throw new PoAException('hook-error'E_USER_ERRORarray($printable));
  49.  
  50.         // if array, check no. of elements
  51.         if (is_array($hook&& (count($hook!= 2)) {
  52.             throw new PoAException('hook-error'E_USER_ERRORarray($printable));
  53.  
  54.         // if array and no. of elements is ok, check it 
  55.         else if (is_array($hook)) {
  56.             $class $hook[0];
  57.             $method $hook[1];
  58.  
  59.             // not an object and the class does not exist
  60.             if (!is_object($class&& !class_exists($class)) {
  61.                 throw new PoAException('hook-error'E_USER_ERRORarray($printable));
  62.  
  63.             // not an object, but the class exists
  64.             else if (!is_object($class)) {
  65.                 $object new $class();
  66.  
  67.                 // is there such method in the class?
  68.                 if (!is_callable(array($object$method)))
  69.                     throw new PoAException('hook-error'E_USER_ERRORarray($printable));
  70.  
  71.                 $this->_hook = array($object$method);
  72.                 $this->_name = $class."::".$method;
  73.  
  74.             // an object, is there such method in the class?
  75.             else if (!is_callable(array($class$method))) {
  76.                 throw new PoAException('hook-error'E_USER_ERRORarray($printable));
  77.  
  78.             // an object, everything ok
  79.             else {
  80.                 $this->_hook = array($class$method);
  81.                 $this->_name = get_class($class)."::".$method;
  82.             }
  83.             $this->_callable_name = '$this->_hook[0]->'.$method;
  84.  
  85.         // a function, check if we can call it
  86.         else if (!is_callable($hook)) {
  87.             throw new PoAException('hook-error'E_USER_ERRORarray($printable));
  88.         else {
  89.             $this->_hook = $hook;
  90.             $this->_callable_name = $hook;
  91.             $this->_name = $hook;
  92.         }
  93.  
  94.         return true;
  95.     }
  96.  
  97.     /**
  98.      * Run the hook with the specified params.
  99.      * @param args An array with all the params the function receives.
  100.      * @return boolean The return value of the function. Please, bear in mind that the function
  101.      *  must return true if hooks proccessing should stop, false in any other case.
  102.      */
  103.     public function run(&$args{
  104.         // build params
  105.         $i 0;
  106.         $input "";
  107.         while ($i count($args)) {
  108.             $input .= "\$args[".$i."],";
  109.             $i++;
  110.         }
  111.         $input trim($input",");
  112.         $call "return ".$this->_callable_name."(".$input.");";
  113.         return eval($call);
  114.     }
  115.  
  116.     /**
  117.      * Get the name of the hook.
  118.      * @return string The name.
  119.      */
  120.     public function getName({
  121.         return $this->_name;
  122.     }
  123.  
  124. }
  125.  
  126. ?>

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