[eluser]simshaun[/eluser]
I've wrote up this plan for my plugins.
What are your guys opinions of this?
Code:
<?php
// hooks.php - controller file
class Homepage extends Controller {
function Homepage()
{
parent::Controller();
$this->load->library('MyHooks');
}
function index()
{
$data['body_content'] = 'This sentence & that sentence.';
$this->myhooks->apply_filters('content_filter_collection', $data['body_content']);
$this->load->view('page_view', $data);
}
}
Code:
// my_plugin.php - ensure this file is loaded before any controllers are loaded
/*
* I'm thinking that all plugins in a directory should be "included" by an auto-loaded library "PluginLoader".
* By doing so, I can have
* $CI =& get_instance();
* $CI->load->library('Hooks');
* before the plugins are included, that way plugins have access to $CI->hooks->...
*/
function my_content_filter(&$input)
{
$input = htmlentities($input);
}
function my_content_filter2(&$input)
{
$input = stripslashes($input);
}
$CI->hooks->register_filter('my_content_filter', 'content_filter_collection', 100);
$CI->hooks->register_filter('my_content_filter2', 'content_filter_collection', 50);
Code:
// How It Works / What Needs Done
/*
1. I need to create a key in the config where the different collections are defined (ex: "content_filter_collection".)
custom_config.php
$config['my_hooks'] = array(
'content_filter_collection' => array();
,'input_filter_collection' => array();
);
custom_config.php - After plugins are loaded, it should theoretically look like this:
$config['my_hooks'] = array(
'content_filter_collection' => array(100 => 'my_content_filter', 50 => 'my_content_filter2')
,'input_filter_collection' => array();
);
2. In the controller, $this->myhooks->apply_filters('content_filter_collection', $data['content']) is called.
*/
Code:
// MyHooks library
class MyHooks {
var $CI;
function MyHooks()
{
$this->_load_ci();
}
function apply_filters($filter_collection, &$input)
{
// Get the my_hooks array from the config.
$my_hooks = $this->CI->config->item('my_hooks');
// If the my_hooks array did not exist or the filter_collection does not exist in the array, stop.
## TODO: Log an error.
if ($my_hooks === FALSE OR !array_key_exists($filter_collection, $my_hooks)){
return FALSE;
}
// We only want to work with a specific collection of filters.
$filter_collection = $my_hooks[$filter_collection];
$num_filters = count($filter_collection);
// Stop if there are no filters in the collection.
if (!$num_filters){
return FALSE;
}
// The elements in the collection have a number for a key.
// This number is the order of importance that filters should be run, so lets start with the lowest.
ksort($filter_collection);
// Call each registered filter on $input.
// I need to investigate and see if call_user_func passes the var by reference like I want.
// If not, then I just need to have the methods return something and have $input = call_user_func($method, $input) below.
foreach ($filter_collection AS $method){
call_user_func($method, $input);
}
}
function _load_ci()
{
$this->CI =& get_instance();
}
}