Skip to content

Commit

Permalink
Added events/hooks system
Browse files Browse the repository at this point in the history
  • Loading branch information
ericlbarnes committed Dec 11, 2010
1 parent e94ee06 commit e43d4e6
Show file tree
Hide file tree
Showing 4 changed files with 252 additions and 2 deletions.
16 changes: 16 additions & 0 deletions addons/modules/contact/events.php
@@ -0,0 +1,16 @@
<?php defined('BASEPATH') or exit('No direct script access allowed');

class Events_Contact {

public function __construct()
{
Events::register('public_controller', array($this, 'method_name'));
}

public function method_name()
{
echo 'it works';
}
}
/* End of file events.php */
/* Location: ./pyrocms/addons/modules/contact/events.php */
4 changes: 2 additions & 2 deletions system/pyrocms/config/autoload.php
Expand Up @@ -39,7 +39,7 @@
| $autoload['libraries'] = array('database', 'session', 'xmlrpc');
*/

$autoload['libraries'] = array('asset', 'database', 'session', 'cache', 'template', 'parser', 'settings/settings', 'users/ion_auth');
$autoload['libraries'] = array('events', 'asset', 'database', 'session', 'cache', 'template', 'parser', 'settings/settings', 'users/ion_auth');


/*
Expand Down Expand Up @@ -81,7 +81,7 @@
|
| $autoload['language'] = array('lang1', 'lang2');
|
| NOTE: Do not include the "_lang" part of your file. For example
| NOTE: Do not include the "_lang" part of your file. For example
| "codeigniter_lang.php" would be referenced as array('codeigniter');
|
*/
Expand Down
2 changes: 2 additions & 0 deletions system/pyrocms/core/Public_Controller.php
Expand Up @@ -9,6 +9,8 @@ function Public_Controller()

$this->benchmark->mark('public_controller_start');

Events::trigger('public_controller');

// Check the frontend hasnt been disabled by an admin
if ( ! $this->settings->frontend_enabled && (empty($this->user) OR $this->user->group != 'admin'))
{
Expand Down
232 changes: 232 additions & 0 deletions system/pyrocms/libraries/Events.php
@@ -0,0 +1,232 @@
<?php defined('BASEPATH') or exit('No direct script access allowed');
/**
* Events
*
* A simple events system for CodeIgniter.
*
* @package CodeIgniter
* @subpackage Events
* @version 1.0
* @author Dan Horrigan <http://dhorrigan.com>
* @author Eric Barnes <http://ericlbarnes.com>
* @license Apache License v2.0
* @copyright 2010 Dan Horrigan
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Events Library
*/
class Events {

/**
* @var array An array of listeners
*/
protected static $_listeners = array();

public function __construct()
{
self::_load_modules();
}

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

/**
* Load Modules
*
* Loads all active modules
*
*/
private static function _load_modules()
{
$_ci = get_instance();

$is_core = TRUE;

$_ci->load->model('modules/module_m');

if ( ! $results = $_ci->module_m->get_all())
{
return FALSE;
}

foreach (array(APPPATH, ADDONPATH) as $directory)
{
foreach (glob($directory.'modules/*', GLOB_ONLYDIR) as $module_name)
{
$slug = basename($module_name);

// This doesnt have a valid details.php file! :o
if ( ! $details_class = self::_spawn_class($slug, $is_core))
{
continue;
}
}

// Going back around, 2nd time is addons
$is_core = FALSE;
}

return TRUE;
}

/**
* Spawn Class
*
* Checks to see if a details.php exists and returns a class
*
* @param string $slug The folder name of the module
* @access private
* @return array
*/
private static function _spawn_class($slug, $is_core = FALSE)
{
$path = $is_core ? APPPATH : ADDONPATH;

// Before we can install anything we need to know some details about the module
$details_file = $path . 'modules/' . $slug . '/events'.EXT;

// Check the details file exists
if ( ! is_file($details_file))
{
return FALSE;
}

// Sweet, include the file
include_once $details_file;

// Now call the details class
$class = 'Events_'.ucfirst(strtolower($slug));

// Now we need to talk to it
return class_exists($class) ? new $class : FALSE;
}

/**
* Register
*
* Registers a Callback for a given event
*
* @access public
* @param string The name of the event
* @param array The callback for the Event
* @return void
*/
public static function register($event, array $callback)
{
$key = get_class($callback[0]).'::'.$callback[1];
self::$_listeners[$event][$key] = $callback;
log_message('debug', 'Events::register() - Registered "'.$key.' with event "'.$event.'"');
}

/**
* Trigger
*
* Triggers an event and returns the results. The results can be returned
* in the following formats:
*
* 'array'
* 'json'
* 'serialized'
* 'string'
*
* @access public
* @param string The name of the event
* @param mixed Any data that is to be passed to the listener
* @param string The return type
* @return mixed The return of the listeners, in the return type
*/
public static function trigger($event, $data = '', $return_type = 'string')
{
log_message('debug', 'Events::trigger() - Triggering event "'.$event.'"');

$calls = array();

if (self::has_listeners($event))
{
foreach (self::$_listeners[$event] as $listener)
{
if (is_callable($listener))
{
$calls[] = call_user_func($listener, $data);
}
}
}

return self::_format_return($calls, $return_type);
}

/**
* Format Return
*
* Formats the return in the given type
*
* @access protected
* @param array The array of returns
* @param string The return type
* @return mixed The formatted return
*/
protected static function _format_return(array $calls, $return_type)
{
log_message('debug', 'Events::_format_return() - Formating calls in type "'.$return_type.'"');

switch ($return_type)
{
case 'array':
return $calls;
break;
case 'json':
return json_encode($calls);
break;
case 'serialized':
return serialize($calls);
break;
case 'string':
$str = '';
foreach ($calls as $call)
{
$str .= $call;
}
return $str;
break;
default:
return $calls;
break;
}

return FALSE;
}

/**
* Has Listeners
*
* Checks if the event has listeners
*
* @access public
* @param string The name of the event
* @return bool Whether the event has listeners
*/
public static function has_listeners($event)
{
log_message('debug', 'Events::has_listeners() - Checking if event "'.$event.'" has listeners.');

if (isset(self::$_listeners[$event]) AND count(self::$_listeners[$event]) > 0)
{
return TRUE;
}
return FALSE;
}
}

/* End of file Events.php */

0 comments on commit e43d4e6

Please sign in to comment.