Welcome Guest, Not a member yet? Register   Sign In
Static Hooks
#1

[eluser]Clifford James[/eluser]
I just ran into a small problem with my user class and ORM, because I couldn't use a static hook.

Here's my solution, hopefully it saves some time for somebody.

Hooks.php:
Code:
$hook['post_controller_constructor'][] = array(
    'class'    => 'User',
    'function' => 'is_loggedin',
    'filename' => 'User.php',
    'filepath' => 'models',
    'static'   => TRUE,
    'params'   => array(TRUE)
);

MY_Hooks.php:
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter
*
* An open source application development framework for PHP 5.1.6 or newer
*
* @package        CodeIgniter
* @author        ExpressionEngine Dev Team
* @copyright    Copyright (c) 2008 - 2011, EllisLab, Inc.
* @license        http://ellislab.com/codeigniter/user-guide/license.html
* @link        http://codeigniter.com
* @since        Version 1.0
* @filesource
*/

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

/**
* CodeIgniter Hooks Class
*
* Provides a mechanism to extend the base system without hacking.
*
* @package        CodeIgniter
* @subpackage    Libraries
* @category    Libraries
* @author        ExpressionEngine Dev Team
* @link        http://ellislab.com/codeigniter/user-guide/libraries/encryption.html
*/
class MY_Hooks extends CI_Hooks {

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

    /**
     * Run Hook
     *
     * Runs a particular hook
     *
     * @access    private
     * @param    array    the hook details
     * @return    bool
     */
    function _run_hook($data)
    {
        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;
        $static        = FALSE;
        $function    = FALSE;
        $params        = '';

        if (isset($data['class']) AND $data['class'] != '')
        {
            $class = $data['class'];
        }

        if (isset($data['function']))
        {
            $function = $data['function'];
        }

        if (isset($data['params']))
        {
            $params = $data['params'];
        }
        
        if (isset($data['static']))
        {
            $static = $data['static'];
        }

        if ($class === FALSE AND $function === FALSE)
        {
            return FALSE;
        }

        // -----------------------------------
        // Set the in_progress flag
        // -----------------------------------

        $this->in_progress = TRUE;

        // -----------------------------------
        // Call the requested class and/or function
        // -----------------------------------

        if ($class !== FALSE)
        {
            if ( ! class_exists($class))
            {
                require($filepath);
            }

            if ($static)
            {
                $class::$function($params);
            }
            else
            {
                $HOOK = new $class;
                $HOOK->$function($params);
            }
        }
        else
        {
            if ( ! function_exists($function))
            {
                require($filepath);
            }

            $function($params);
        }

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

}

// END MY_Hooks class

/* End of file MY_Hooks.php */
/* Location: ./applycation/core/MY_Hooks.php */




Theme © iAndrew 2016 - Forum software by © MyBB