CodeIgniter框架源码学习之钩子类--Hooks.php

CI源码 专栏收录该内容
20 篇文章 0 订阅
文件位置:./system/core/Hooks.php
<?php
/**
* CodeIgniter
*
* An open source application development framework for PHP
*
* This content is released under the MIT License (MIT)
*
* Copyright (c) 2014 - 2017, 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 - 2017, British Columbia Institute of Technology (http://bcit.ca/)
* @license http://opensource.org/licenses/MIT MIT License
* @link https://codeigniter.com
* @since Version 1.0.0
* @filesource
*/
defined('BASEPATH') OR exit('No direct script access allowed');

/**
* Hooks Class
*
* 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
*/
class CI_Hooks {

/**
* Determines whether hooks are enabled
*
* @var bool
*/
public $enabled = FALSE;

/**
* List of all hooks set in config/hooks.php
* 决定钩子是否能用
* @var array
*/
public $hooks = array();

/**
* Array with class objects to use hooks methods
*可以使用钩子方法的类对象的数组
* @var array
*/
protected $_objects = array();

/**
* In progress flag
*
* Determines whether hook is in progress, used to prevent infinte loops
* 决定钩子是否在进行中,通常阻止无限循环
* @var bool
*/
protected $_in_progress = FALSE;

/**
* Class constructor
*
* @return void
*/
public function __construct()
{
$CFG =& load_class('Config', 'core');
log_message('info', 'Hooks Class Initialized');

// If hooks are not enabled in the config file
// there is nothing else to do
// 如果在config文件中设置钩子不能用,这里就不会执行任何操作
if ($CFG->item('enable_hooks') === FALSE)
{
return;
}

// Grab the "hooks" definition file. 如果APPPATH.'config/hooks.php或者APPPATH.'config/'.ENVIRONMENT.'/hooks.php'存在则加载hooks.php
if (file_exists(APPPATH.'config/hooks.php'))
{
include(APPPATH.'config/hooks.php');
}

if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/hooks.php'))
{
include(APPPATH.'config/'.ENVIRONMENT.'/hooks.php');
}

// If there are no hooks, we're done. 如果没有钩子就结束
if ( ! isset($hook) OR ! is_array($hook))
{
return;
}

$this->hooks =& $hook;
$this->enabled = TRUE;
}

// --------------------------------------------------------------------

/**
* Call Hook
*
* Calls a particular hook. Called by CodeIgniter.php.
* 在CodeIgniter.php文件中调用指定的钩子
* @uses CI_Hooks::_run_hook()
*
* @param string $which Hook name
* @return bool TRUE on success or FALSE on failure
*/
public function call_hook($which = '')
{
if ( ! $this->enabled OR ! isset($this->hooks[$which]))
{
return FALSE;
}

if (is_array($this->hooks[$which]) && ! isset($this->hooks[$which]['function']))
{
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
*/
protected function _run_hook($data)
{
// 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))
{
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'], $data['filename']))
{
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'] : '';

if (empty($function))
{
return FALSE;
}

// Set the _in_progress flag
$this->_in_progress = TRUE;

// Call the requested class and/or function
if ($class !== FALSE)
{
// The object is stored?
if (isset($this->_objects[$class]))
{
if (method_exists($this->_objects[$class], $function))
{
$this->_objects[$class]->$function($params);
}
else
{
return $this->_in_progress = FALSE;
}
}
else
{
class_exists($class, FALSE) OR require_once($filepath);

if ( ! class_exists($class, FALSE) OR ! method_exists($class, $function))
{
return $this->_in_progress = FALSE;
}

// Store the object and execute the method
$this->_objects[$class] = new $class();
$this->_objects[$class]->$function($params);
}
}
else
{
function_exists($function) OR require_once($filepath);

if ( ! function_exists($function))
{
return $this->_in_progress = FALSE;
}

$function($params);
}

$this->_in_progress = FALSE;
return TRUE;
}

}

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页

打赏作者

HarcourtFun

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值