Skip to content

Instantly share code, notes, and snippets.

@Toyz
Created December 28, 2015 02:57
Show Gist options
  • Save Toyz/652bb1a0699d0e28ccf0 to your computer and use it in GitHub Desktop.
Save Toyz/652bb1a0699d0e28ccf0 to your computer and use it in GitHub Desktop.
<?php
namespace Controllers\EventEngine;
class EventHandler
{
private static $EventPool = [];
public static function CreatePool($poolName){
if(array_key_exists($poolName, self::$EventPool))
throw new Exception("$poolName is already a eventPool", 1);
self::$EventPool[$poolName] = [];
}
public static function RemovePool($poolName){
if(!array_key_exists($poolName, self::$EventPool))
throw new Exception("$poolName is not in eventPool", 1);
unset(self::$EventPool[$poolName]);
}
public static function Call($poolName, $params){
if(array_key_exists($poolName, self::$EventPool))
throw new Exception("$poolName is not in eventPool", 1);
for(self::$EventPool[$poolName] as $key=>$value){
$value["function"]($params);
}
}
public static function Register($poolName, $eventName, $function){
if(!array_key_exists($poolName, self::$EventPool))
throw new Exception("$poolName is not in eventPool", 1);
if(array_key_exists(self::$EventPool[$poolName], $eventName))
throw new Exception("$eventName is already in EventPool $poolName", 1);
self::$EventPool[$poolName][$eventName] = [
"function" => $function
];
}
public static function Unregister($poolName, $eventName){
if(!array_key_exists($poolName, self::$EventPool))
throw new Exception("$poolName is not in eventPool", 1);
if(!array_key_exists(self::$EventPool[$poolName], $eventName))
throw new Exception("$eventName is not in EventPool $poolName", 1);
unset(self::$EventPool[$poolName][$eventName]);
}
public static function GetPool(){
return self::$EventPool;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment