Skip to content

Instantly share code, notes, and snippets.

@dduleone
Last active August 29, 2015 14:01
Show Gist options
  • Select an option

  • Save dduleone/452cc67715b343556954 to your computer and use it in GitHub Desktop.

Select an option

Save dduleone/452cc67715b343556954 to your computer and use it in GitHub Desktop.
PubSub PoC
<?php
$_NL2BR = TRUE;
// more: http://baylorrae.com/php-pubsub/
class PubSub {
private static $events = array(); // all subscriptions
// Don't allow PubSub to be initialized outside this class
private function __construct () { }
private function __clone () { }
/**
* Adds a subscription to the stack
*
* @param string $name
* @param function $callback needs to be callable with call_user_func()
* @return void
* @author Baylor Rae'
*/
public static function subscribe ($name, $callback) {
// Make sure the subscription isn't null
if (empty(self::$events[$name])) {
self::$events[$name] = array();
}
// push the $callback onto the subscription stack
array_push(self::$events[$name], $callback);
}
/**
* Calls the last subscription in the stack
*
* @param string $name
* @param string $params
* @return false if fails
* @author Baylor Rae'
*/
public static function publish ($name, $params = '') {
// Check to see if the subscribe isn't null
if (empty(self::$events[$name])) {
return false;
}
// Gets all parameters passed to the function
// and removes the first, which is $name
$params = func_get_args();
array_shift($params);
$results = array();
// Loop through all the events and call them
foreach (self::$events[$name] as $event) {
if (is_callable($event)) {
$results[$event] = call_user_func_array($event, $params);
} elseif (is_callable($event. "::fire")) {
$results[$event] = call_user_func_array($event . "::fire", $params);
}
}
return $results;
}
public static function unsubscribe($name) {
if (!empty(self::$events[$name])) {
unset(self::$events[$name]);
}
}
}
class Event {
public static $init;
public static function init ($subscriptions = "") {
$class = get_called_class();
if (!is_array(self::$init)) {
self::$init = array();
}
if (!isset(self::$init[$class])) {
self::$init[$class] = FALSE;
}
if (self::$init[$class] == TRUE) {
return;
}
self::$init[$class] = TRUE;
if (!empty($subscriptions)) {
foreach ($subscriptions as $sub) {
PubSub::subscribe($sub->event, $sub->routine);
}
}
}
public static function makeHandler ($eventName, $routine) {
$event = new stdClass();
$event->event = $eventName;
$event->routine = $routine;
return $event;
}
}
class beforeEventEvent extends Event {
public static function fire () {
echo get_class() . "::fire\n";
return 'fire';
}
public static function firealt () {
echo get_class() . "::firealt\n";
return 'firealt';
}
public static function init ($subscriptions = "") {
$events = array();
$events[] = Event::makeHandler('beforeEvent', 'beforeEventEvent::firealt');
$events[] = Event::makeHandler('beforeEvent', 'beforeEventEvent');
if (!empty($subscriptions)) {
foreach ($subscriptions as $sub) {
$events[] = $sub;
}
}
parent::init($events);
}
}
class afterEventEvent extends Event {
public static function fire () {
echo get_class() . "::fire\n";
return 'firealt';
}
public static function init ($subscriptions = "") {
$events = array();
$events[] = Event::makeHandler('afterEvent', 'afterEventEvent');
if (!empty($subscriptions)) {
foreach ($subscriptions as $sub) {
$events[] = $sub;
}
}
parent::init($events);
}
}
beforeEventEvent::init();
afterEventEvent::init();
ob_start();
// Basic usage
$results = array();
$results['beforeEvent'] = PubSub::publish('beforeEvent');
echo "...Event...\n";
$results['afterEvent'] = PubSub::publish('afterEvent');
echo "\tRound 1:\n";
print_r($results);
PubSub::unsubscribe('beforeEvent');
PubSub::unsubscribe('afterEvent');
$results = array();
$results['beforeEvent'] = PubSub::publish('beforeEvent');
echo "...Event...\n";
$results['afterEvent'] = PubSub::publish('afterEvent');
echo "\tRound 2:\n";
print_r($results);
$ob = ob_get_clean();
if ($_NL2BR) {
$buffer = str_replace("\n", "<br />\n", $ob);
} else {
$buffer = $ob;
}
echo $buffer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment