Hooks.php

Go to the documentation of this file.
00001 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
00002 /**
00003  * CodeIgniter
00004  *
00005  * An open source application development framework for PHP 4.3.2 or newer
00006  *
00007  * @package             CodeIgniter
00008  * @author              ExpressionEngine Dev Team
00009  * @copyright   Copyright (c) 2008, EllisLab, Inc.
00010  * @license             http://codeigniter.com/user_guide/license.html
00011  * @link                http://codeigniter.com
00012  * @since               Version 1.0
00013  * @filesource
00014  */
00015 
00016 // ------------------------------------------------------------------------
00017 
00018 /**
00019  * CodeIgniter Hooks Class
00020  *
00021  * Provides a mechanism to extend the base system without hacking.  Most of
00022  * this class is borrowed from Paul's Extension class in ExpressionEngine.
00023  *
00024  * @package             CodeIgniter
00025  * @subpackage  Libraries
00026  * @category    Libraries
00027  * @author              ExpressionEngine Dev Team
00028  * @link                http://codeigniter.com/user_guide/libraries/encryption.html
00029  */
00030 class CI_Hooks {
00031         
00032         var $enabled            = FALSE;
00033         var $hooks              = array();
00034         var $in_progress        = FALSE;
00035         
00036         /**
00037          * Constructor
00038          *
00039          */
00040         function CI_Hooks()
00041         {
00042                 $this->_initialize();   
00043                 log_message('debug', "Hooks Class Initialized");
00044         }
00045         
00046         // --------------------------------------------------------------------
00047 
00048         /**
00049          * Initialize the Hooks Preferences
00050          *
00051          * @access      private
00052          * @return      void
00053          */     
00054         function _initialize()
00055         {
00056                 $CFG =& load_class('Config');
00057                 
00058                 // If hooks are not enabled in the config file
00059                 // there is nothing else to do
00060                 
00061                 if ($CFG->item('enable_hooks') == FALSE)
00062                 {
00063                         return;
00064                 }
00065                 
00066                 // Grab the "hooks" definition file.
00067                 // If there are no hooks, we're done.
00068                 
00069                 @include(APPPATH.'config/hooks'.EXT);
00070                 
00071                 if ( ! isset($hook) OR ! is_array($hook))
00072                 {
00073                         return;
00074                 }
00075 
00076                 $this->hooks =& $hook;
00077                 $this->enabled = TRUE;
00078         }
00079         
00080         // --------------------------------------------------------------------
00081 
00082         /**
00083          * Call Hook
00084          *
00085          * Calls a particular hook
00086          *
00087          * @access      private
00088          * @param       string  the hook name
00089          * @return      mixed
00090          */
00091         function _call_hook($which = '')
00092         {
00093                 if ( ! $this->enabled OR ! isset($this->hooks[$which]))
00094                 {
00095                         return FALSE;
00096                 }
00097         
00098                 if (isset($this->hooks[$which][0]) AND is_array($this->hooks[$which][0]))
00099                 {
00100                         foreach ($this->hooks[$which] as $val)
00101                         {
00102                                 $this->_run_hook($val);
00103                         }
00104                 }
00105                 else
00106                 {
00107                         $this->_run_hook($this->hooks[$which]);
00108                 }
00109                 
00110                 return TRUE;
00111         }
00112 
00113         // --------------------------------------------------------------------
00114 
00115         /**
00116          * Run Hook
00117          *
00118          * Runs a particular hook
00119          *
00120          * @access      private
00121          * @param       array   the hook details
00122          * @return      bool
00123          */
00124         function _run_hook($data)
00125         {
00126                 if ( ! is_array($data))
00127                 {
00128                         return FALSE;
00129                 }
00130                 
00131                 // -----------------------------------
00132                 // Safety - Prevents run-away loops
00133                 // -----------------------------------
00134         
00135                 // If the script being called happens to have the same
00136                 // hook call within it a loop can happen
00137                 
00138                 if ($this->in_progress == TRUE)
00139                 {
00140                         return;
00141                 }
00142 
00143                 // -----------------------------------
00144                 // Set file path
00145                 // -----------------------------------
00146                 
00147                 if ( ! isset($data['filepath']) OR ! isset($data['filename']))
00148                 {
00149                         return FALSE;
00150                 }
00151                 
00152                 $filepath = APPPATH.$data['filepath'].'/'.$data['filename'];
00153         
00154                 if ( ! file_exists($filepath))
00155                 {
00156                         return FALSE;
00157                 }
00158                 
00159                 // -----------------------------------
00160                 // Set class/function name
00161                 // -----------------------------------
00162                 
00163                 $class          = FALSE;
00164                 $function       = FALSE;
00165                 $params         = '';
00166                 
00167                 if (isset($data['class']) AND $data['class'] != '')
00168                 {
00169                         $class = $data['class'];
00170                 }
00171 
00172                 if (isset($data['function']))
00173                 {
00174                         $function = $data['function'];
00175                 }
00176 
00177                 if (isset($data['params']))
00178                 {
00179                         $params = $data['params'];
00180                 }
00181                 
00182                 if ($class === FALSE AND $function === FALSE)
00183                 {
00184                         return FALSE;
00185                 }
00186                 
00187                 // -----------------------------------
00188                 // Set the in_progress flag
00189                 // -----------------------------------
00190 
00191                 $this->in_progress = TRUE;
00192                 
00193                 // -----------------------------------
00194                 // Call the requested class and/or function
00195                 // -----------------------------------
00196                 
00197                 if ($class !== FALSE)
00198                 {
00199                         if ( ! class_exists($class))
00200                         {
00201                                 require($filepath);
00202                         }
00203                 
00204                         $HOOK = new $class;
00205                         $HOOK->$function($params);
00206                 }
00207                 else
00208                 {
00209                         if ( ! function_exists($function))
00210                         {
00211                                 require($filepath);
00212                         }
00213                 
00214                         $function($params);
00215                 }
00216         
00217                 $this->in_progress = FALSE;
00218                 return TRUE;
00219         }
00220 
00221 }
00222 
00223 // END CI_Hooks class
00224 
00225 /* End of file Hooks.php */
00226 /* Location: ./system/libraries/Hooks.php */