tims-todo/system/core/Hooks.php

267 lines
6.1 KiB
PHP
Raw Permalink Normal View History

<?php
2014-08-08 15:41:59 -04:00
/**
* CodeIgniter
*
* An open source application development framework for PHP
2014-08-08 15:41:59 -04:00
*
* This content is released under the MIT License (MIT)
*
* Copyright (c) 2014 - 2019, British Columbia Institute of Technology
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @package CodeIgniter
* @author EllisLab Dev Team
* @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
* @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
* @license https://opensource.org/licenses/MIT MIT License
* @link https://codeigniter.com
* @since Version 1.0.0
2014-08-08 15:41:59 -04:00
* @filesource
*/
defined('BASEPATH') OR exit('No direct script access allowed');
2014-08-08 15:41:59 -04:00
/**
* Hooks Class
2014-08-08 15:41:59 -04:00
*
* Provides a mechanism to extend the base system without hacking.
*
* @package CodeIgniter
* @subpackage Libraries
* @category Libraries
* @author EllisLab Dev Team
* @link https://codeigniter.com/user_guide/general/hooks.html
2014-08-08 15:41:59 -04:00
*/
class CI_Hooks {
/**
* Determines whether hooks are enabled
2014-08-08 15:41:59 -04:00
*
* @var bool
2014-08-08 15:41:59 -04:00
*/
public $enabled = FALSE;
2014-08-08 15:41:59 -04:00
/**
* List of all hooks set in config/hooks.php
*
* @var array
2014-08-08 15:41:59 -04:00
*/
public $hooks = array();
2014-08-08 15:41:59 -04:00
/**
* Array with class objects to use hooks methods
2014-08-08 15:41:59 -04:00
*
* @var array
2014-08-08 15:41:59 -04:00
*/
protected $_objects = array();
2014-08-08 15:41:59 -04:00
/**
* In progress flag
2014-08-08 15:41:59 -04:00
*
* Determines whether hook is in progress, used to prevent infinte loops
*
* @var bool
2014-08-08 15:41:59 -04:00
*/
protected $_in_progress = FALSE;
2014-08-08 15:41:59 -04:00
/**
* Class constructor
2014-08-08 15:41:59 -04:00
*
* @return void
*/
public function __construct()
2014-08-08 15:41:59 -04:00
{
$CFG =& load_class('Config', 'core');
log_message('info', 'Hooks Class Initialized');
2014-08-08 15:41:59 -04:00
// If hooks are not enabled in the config file
// there is nothing else to do
if ($CFG->item('enable_hooks') === FALSE)
2014-08-08 15:41:59 -04:00
{
return;
}
// Grab the "hooks" definition file.
if (file_exists(APPPATH.'config/hooks.php'))
2014-08-08 15:41:59 -04:00
{
include(APPPATH.'config/hooks.php');
}
if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/hooks.php'))
{
include(APPPATH.'config/'.ENVIRONMENT.'/hooks.php');
}
2014-08-08 15:41:59 -04:00
// If there are no hooks, we're done.
2014-08-08 15:41:59 -04:00
if ( ! isset($hook) OR ! is_array($hook))
{
return;
}
$this->hooks =& $hook;
$this->enabled = TRUE;
}
// --------------------------------------------------------------------
/**
* Call Hook
*
* Calls a particular hook. Called by CodeIgniter.php.
2014-08-08 15:41:59 -04:00
*
* @uses CI_Hooks::_run_hook()
*
* @param string $which Hook name
* @return bool TRUE on success or FALSE on failure
2014-08-08 15:41:59 -04:00
*/
public function call_hook($which = '')
2014-08-08 15:41:59 -04:00
{
if ( ! $this->enabled OR ! isset($this->hooks[$which]))
{
return FALSE;
}
if (is_array($this->hooks[$which]) && ! isset($this->hooks[$which]['function']))
2014-08-08 15:41:59 -04:00
{
foreach ($this->hooks[$which] as $val)
{
$this->_run_hook($val);
}
}
else
{
$this->_run_hook($this->hooks[$which]);
}
return TRUE;
}
// --------------------------------------------------------------------
/**
* Run Hook
*
* Runs a particular hook
*
* @param array $data Hook details
* @return bool TRUE on success or FALSE on failure
2014-08-08 15:41:59 -04:00
*/
protected function _run_hook($data)
2014-08-08 15:41:59 -04:00
{
// Closures/lambda functions and array($object, 'method') callables
if (is_callable($data))
{
is_array($data)
? $data[0]->{$data[1]}()
: $data();
return TRUE;
}
elseif ( ! is_array($data))
2014-08-08 15:41:59 -04:00
{
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)
2014-08-08 15:41:59 -04:00
{
return;
}
// -----------------------------------
// Set file path
// -----------------------------------
if ( ! isset($data['filepath'], $data['filename']))
2014-08-08 15:41:59 -04:00
{
return FALSE;
}
$filepath = APPPATH.$data['filepath'].'/'.$data['filename'];
if ( ! file_exists($filepath))
{
return FALSE;
}
// Determine and class and/or function names
$class = empty($data['class']) ? FALSE : $data['class'];
$function = empty($data['function']) ? FALSE : $data['function'];
$params = isset($data['params']) ? $data['params'] : '';
2014-08-08 15:41:59 -04:00
if (empty($function))
2014-08-08 15:41:59 -04:00
{
return FALSE;
}
// Set the _in_progress flag
$this->_in_progress = TRUE;
2014-08-08 15:41:59 -04:00
// Call the requested class and/or function
if ($class !== FALSE)
{
// The object is stored?
if (isset($this->_objects[$class]))
2014-08-08 15:41:59 -04:00
{
if (method_exists($this->_objects[$class], $function))
{
$this->_objects[$class]->$function($params);
}
else
{
return $this->_in_progress = FALSE;
}
2014-08-08 15:41:59 -04:00
}
else
{
class_exists($class, FALSE) OR require_once($filepath);
if ( ! class_exists($class, FALSE) OR ! method_exists($class, $function))
{
return $this->_in_progress = FALSE;
}
2014-08-08 15:41:59 -04:00
// Store the object and execute the method
$this->_objects[$class] = new $class();
$this->_objects[$class]->$function($params);
}
2014-08-08 15:41:59 -04:00
}
else
{
function_exists($function) OR require_once($filepath);
2014-08-08 15:41:59 -04:00
if ( ! function_exists($function))
{
return $this->_in_progress = FALSE;
2014-08-08 15:41:59 -04:00
}
$function($params);
}
$this->_in_progress = FALSE;
2014-08-08 15:41:59 -04:00
return TRUE;
}
}