Created
October 27, 2016 14:24
-
-
Save hexblot/2843fdbe85e6c83e5409cfe0bca42eac to your computer and use it in GitHub Desktop.
A very simple event system for PHP Projects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* A class to implement a simple event system in any PHP project. | |
* | |
* @author Nick Andriopoulos <[email protected]> | |
* @license https://www.apache.org/licenses/LICENSE-2.0 | |
*/ | |
/** | |
* A simple event system for any PHP Project | |
*/ | |
class EventSystem | |
{ | |
static $eventmap; | |
/** | |
* Register an event before it becoming available for use | |
* | |
* @param string $eventname | |
*/ | |
public static function registerEvent($eventname) { | |
if(!isset(self::$eventmap[$eventname])) { | |
self::$eventmap[$eventname] = array(); | |
} | |
} | |
/** | |
* Subscribe to a defined event. | |
* | |
* @param string $eventname | |
* @param string $callback | |
* @param int $weight | |
*/ | |
public static function subscribeToEvent($eventname, $callback, $weight=0) { | |
if(!self::validEvent($eventname)) { return false; } | |
self::$eventmap[$eventname][$weight][] = $callback; | |
} | |
/** | |
* Trigger an event, and call all subscribers, giving an array of params. | |
* | |
* @param string $eventname | |
* @param array $params | |
*/ | |
public static function triggerEvent($eventname, $params) { | |
if(!self::validEvent($eventname)) { return false; } | |
foreach(self::$eventmap[$eventname] as $key => $weight) { | |
foreach($weight as $callback) { | |
call_user_func_array($callback, $params); | |
} | |
} | |
} | |
/** | |
* Check that an event is valid before interacting with it. | |
* | |
* @internal | |
* | |
* @param string $eventname | |
*/ | |
public static function validEvent($eventname) { | |
if(!isset(self::$eventmap[$eventname]) || !is_array(self::$eventmap[$eventname])) { return false; } | |
return true; | |
} | |
} | |
/* Simple Example */ | |
/* Delete this line to enable testprint! | |
// On website boot, create your event.... | |
# include('EventSystem.php'); // If the class were in a different file, include it. | |
EventSystem::registerEvent('prelogin'); | |
// Module 1 needs to do stuff then ( anywhere in the project ) | |
EventSystem::subscribeToEvent('prelogin', 'myprelogin'); | |
// Module 2 too! ( anywhere in the project ) | |
EventSystem::subscribeToEvent('prelogin', 'anotherprelogin'); | |
// The actual website will then trigger the event when it's time, | |
// with the params expected by the implementors | |
EventSystem::triggerEvent('prelogin', array('nick', 'asdf')); | |
// Sample module 1 function: | |
function myprelogin($username, $password) { | |
print 'My Username is: '.$username."\n"; | |
print 'My Password is: '.$password."\n"; | |
} | |
// Sample module 2 function: | |
function anotherprelogin($username, $password) { | |
print 'Just wanted to join in the fun!'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment