Skip to content

Commit

Permalink
Add extension to the core to allow library like loading of event hand…
Browse files Browse the repository at this point in the history
…lers.
  • Loading branch information
Nathan Macnamara committed Jun 1, 2014
1 parent 178037f commit 17a105d
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 5 deletions.
170 changes: 170 additions & 0 deletions core/MY_Loader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
<?php

(defined('BASEPATH')) OR exit('No direct script access allowed');

class MY_Loader extends CI_Loader {

protected $_ci_events_paths = array();

public function __construct() {
parent::__construct();

$this->_ci_events_paths = array(APPPATH);
log_message('debug', "MY_Loader Class Initialized");
}

/** Load a events module * */
public function event($event = '', $params = NULL, $object_name = NULL) {

if (is_array($event))
return $this->libraries($event);

$class = strtolower(basename($event));

if (isset($this->_ci_classes[$class]) AND $_alias = $this->_ci_classes[$class])
return CI::$APP->$_alias;

($_alias = strtolower($object_name)) OR $_alias = $class;

list($path, $_event) = Modules::find($event, $this->_module, 'events/');

/* load library config file as params */
if ($params == NULL) {
list($path2, $file) = Modules::find($_alias, $this->_module, 'config/');
($path2) AND $params = Modules::load_file($file, $path2, 'config');
}

if ($path === FALSE) {

$this->_ci_events_class($event, $params, $object_name);
$_alias = $this->_ci_classes[$class];
} else {

Modules::load_file($_event, $path);

$event = ucfirst($_event);
CI::$APP->$_alias = new $event($params);

$this->_ci_classes[$class] = $_alias;
}

return CI::$APP->$_alias;
}

/** Load an array of events * */
public function events($events) {
foreach ($events as $_event)
$this->event($_event);
}

/**
* Load Events
*
* This function loads the requested class.
*
* @param string the item that is being loaded
* @param mixed any additional parameters
* @param string an optional object name
* @return void
*/
protected function _ci_events_class($class, $params = NULL, $object_name = NULL) {
// Get the class name, and while we're at it trim any slashes.
// The directory path can be included as part of the class name,
// but we don't want a leading slash
$class = str_replace('.php', '', trim($class, '/'));

// Was the path included with the class name?
// We look for a slash to determine this
$subdir = '';
if (($last_slash = strrpos($class, '/')) !== FALSE) {
// Extract the path
$subdir = substr($class, 0, $last_slash + 1);

// Get the filename from the path
$class = substr($class, $last_slash + 1);
}

// We'll test for both lowercase and capitalized versions of the file name
foreach (array(ucfirst($class), strtolower($class)) as $class) {
$subclass = APPPATH . 'events/' . $subdir . config_item('subclass_prefix') . $class . '.php';

// Is this a class extension request?
if (file_exists($subclass)) {
$baseclass = BASEPATH . 'events/' . ucfirst($class) . '.php';

if (!file_exists($baseclass)) {
log_message('error', "Unable to load the requested class: " . $class);
show_error("Unable to load the requested class: " . $class);
}

// Safety: Was the class already loaded by a previous call?
if (in_array($subclass, $this->_ci_loaded_files)) {
// Before we deem this to be a duplicate request, let's see
// if a custom object name is being supplied. If so, we'll
// return a new instance of the object
if (!is_null($object_name)) {
$CI = & get_instance();
if (!isset($CI->$object_name)) {
return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
}
}

$is_duplicate = TRUE;
log_message('debug', $class . " class already loaded. Second attempt ignored.");
return;
}

include_once($baseclass);
include_once($subclass);
$this->_ci_loaded_files[] = $subclass;

return $this->_ci_init_class($class, config_item('subclass_prefix'), $params, $object_name);
}

// Lets search for the requested library file and load it.
$is_duplicate = FALSE;
foreach ($this->_ci_events_paths as $path) {
$filepath = $path . 'events/' . $subdir . $class . '.php';

// Does the file exist? No? Bummer...
if (!file_exists($filepath)) {
continue;
}

// Safety: Was the class already loaded by a previous call?
if (in_array($filepath, $this->_ci_loaded_files)) {
// Before we deem this to be a duplicate request, let's see
// if a custom object name is being supplied. If so, we'll
// return a new instance of the object
if (!is_null($object_name)) {
$CI = & get_instance();
if (!isset($CI->$object_name)) {
return $this->_ci_init_class($class, '', $params, $object_name);
}
}

$is_duplicate = TRUE;
log_message('debug', $class . " class already loaded. Second attempt ignored.");
return;
}

include_once($filepath);
$this->_ci_loaded_files[] = $filepath;
return $this->_ci_init_class($class, '', $params, $object_name);
}
} // END FOREACH
// One last attempt. Maybe the library is in a subdirectory, but it wasn't specified?
if ($subdir == '') {
$path = strtolower($class) . '/' . $class;
return $this->_ci_load_class($path, $params);
}

// If we got this far we were unable to find the requested class.
// We do not issue errors if the load call failed due to a duplicate request
if ($is_duplicate == FALSE) {
log_message('error', "Unable to load the requested class: " . $class);
show_error("Unable to load the requested class: " . $class);
}
}

}
14 changes: 9 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ Version 1.0.0
* Author: [Eric Barnes](http://ericlbarnes.com/ "Eric Barnes")
* Author: [Dan Horrigan](http://dhorrigan.com/ "Dan Horrigan")

##### Additions (Core Extention)

* Author: [Nathan Macnamara](https://github.com/nathanmac "Nathan Macnamara")


## Public Methods

### register
Expand Down Expand Up @@ -59,7 +64,7 @@ The 3rd parameter is the type of data you wish trigger() to return. Your option
Because events need to be registered before being used it is a good idea to have a system
in place to load any of these before you trigger any events.

Here is an example using a third party library to register the event.
Here is an example loading the event handler to register the event.

<pre>
// Example Welcome Controller
Expand All @@ -71,9 +76,8 @@ class Welcome extends CI_Controller {
parent::__construct();
// Load Library
$this->load->library('events');
// Load our class that registers an event. See class Test below.
$this->load->add_package_path(APPPATH.'third_party/test/');
$this->load->library('test');

$this->load->event('test');
}

public function index()
Expand All @@ -82,7 +86,7 @@ class Welcome extends CI_Controller {
}
}

// Example third_party/test/libaries/test.php
// Example events/test.php

class Test {

Expand Down

0 comments on commit 17a105d

Please sign in to comment.