CI_Hooks::_run_hook PHP Method

_run_hook() protected method

Runs a particular hook
protected _run_hook ( array $data ) : boolean
$data array Hook details
return boolean 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;
    }

Usage Example

Example #1
0
 function _run_hook($data)
 {
     if (is_callable($data)) {
         call_user_func_array($data, array());
     } else {
         return parent::_run_hook($data);
     }
 }
All Usage Examples Of CI_Hooks::_run_hook