Skip to content

Instantly share code, notes, and snippets.

@ruthlessfish
Created November 12, 2011 03:30
Show Gist options
  • Save ruthlessfish/1359992 to your computer and use it in GitHub Desktop.
Save ruthlessfish/1359992 to your computer and use it in GitHub Desktop.
Custom hooks with arguments
<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
* MY_Hooks Class
*
* Extends Hooks by allowing you to pass parameters on the fly
* with custom hook calls. Much of the logic is borrowed from the
* core CI_Hooks class.
*
* Ex:
*
* If you have a custom hook set up like this:
*
* $hook['custom_hook'] = array('class' => 'MyClass',
* 'function' => 'Myfunction',
* 'filename' => 'Myclass.php',
* 'filepath' => 'hooks');
*
* And the function is defined like this:
*
* function Myfunction($foo='', $bar='', $baz='') { ... }
*
* You can call the hook like this:
*
* $this->hooks->_call_hook('custom_hook', array('foo', 'bar', 'baz'));
*
*/
class MY_Hooks extends CI_Hooks {
/**
* Call Hook
*
* Calls a particular hook
*
* @access private
* @param string the hook name
* @return mixed
*/
function _call_hook($which = '', $params = array())
{
if ( ! $this->enabled OR ! isset($this->hooks[$which]))
{
return FALSE;
}
if (isset($this->hooks[$which][0]) AND is_array($this->hooks[$which][0]))
{
$_hook = array();
foreach ($this->hooks[$which] as $val)
{
$_hook[] = $this->_run_hook($val, $params);
}
return $_hook;
}
return $this->_run_hook($this->hooks[$which], $params);
}
// --------------------------------------------------------------------
/**
* Run Hook
*
* Runs a particular hook
*
* @access private
* @param array the hook details
* @return bool
*/
function _run_hook($data, $params = '')
{
if ( ! is_array($data))
{
return FALSE;
}
// -----------------------------------
// Safety - Prevents run-away loops
// -----------------------------------
// If the script being called happens to have the same
// hook call within it a loop can happen
if ($this->in_progress == TRUE)
{
return;
}
// -----------------------------------
// Set file path
// -----------------------------------
if ( ! isset($data['filepath']) OR ! isset($data['filename']))
{
return FALSE;
}
$filepath = APPPATH.$data['filepath'].'/'.$data['filename'];
if ( ! file_exists($filepath))
{
return FALSE;
}
// -----------------------------------
// Set class/function name
// -----------------------------------
$class = FALSE;
$function = FALSE;
if (isset($data['class']) AND $data['class'] != '')
{
$class = $data['class'];
}
if (isset($data['function']))
{
$function = $data['function'];
}
if (empty($params) && isset($data['params']))
{
$params = $data['params'];
}
if ($class === FALSE AND $function === FALSE)
{
return FALSE;
}
// -----------------------------------
// Set the in_progress flag
// -----------------------------------
$this->in_progress = TRUE;
// -----------------------------------
// Call the requested class and/or function
// -----------------------------------
$hook_output = FALSE;
if ($class !== FALSE)
{
if ( ! class_exists($class))
{
require($filepath);
}
$HOOK = new $class;
$hook_output = call_user_func_array(array($HOOK, $function), $params);
}
else
{
if ( ! function_exists($function))
{
require($filepath);
}
$hook_output = call_user_func_array($function, $params);
}
$this->in_progress = FALSE;
return $hook_output;
}
}
// END MY_Hooks class
/* End of file MY_Hooks.php */
/* Location: ./application/core/MY_Hooks.php */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment