Skip to content

Instantly share code, notes, and snippets.

@BaylorRae
Created August 31, 2012 21:56
Show Gist options
  • Save BaylorRae/3559713 to your computer and use it in GitHub Desktop.
Save BaylorRae/3559713 to your computer and use it in GitHub Desktop.
<?php
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);
}
}
// Basic usage
PubSub::subscribe('beforeSave', function() {
echo 'PubSub::beforeSave';
});
PubSub::subscribe('afterSave', function() {
echo 'PubSub::afterSave';
});
?>
<?php
/*
Somewhere in the core files
*/
PubSub::subscribe('beforeSaveRecord', function($record_data) {
# code...
});
PubSub::subscribe('afterSaveRecord', function($record_data) {
# code...
});
function save_record($record_data) {
$record_data = PubSub::publish('beforeSaveRecord', $record_data);
# code ...
PubSub::publish('after_save_record', $record_data);
}
// my code
PubSub::subscribe('beforeSaveRecord', function($record_data) {
# do stuff with $record_data
});
save_record(array(
'username' => 'BaylorRae',
'first_name' => 'Baylor',
'last_name' => 'Rae\''
));
?>
<?php
function before_save_record($record_data) {
# code...
}
function after_save_record($record_data) {
# code...
}
function save_record($record_data) {
$record_data = before_save_record($record_data);
# code ...
after_save_record($record_data);
}
save_record(array(
'username' => 'BaylorRae',
'first_name' => 'Baylor',
'last_name' => 'Rae\''
));
?>
<?php
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);
// If there's only one event, then call it and return the value
if( count(self::$events[$name]) === 1 ) {
if( is_callable(self::$events[$name][0]) )
return call_user_func_array(self::$events[$name][0], $params);
else
return false;
}
// Loop through all the events and call them
foreach( self::$events[$name] as $event ) {
if( is_callable($event) )
call_user_func_array($event, $params);
}
}
}
// Basic usage
PubSub::subscribe('beforeSave', function() {
echo 'PubSub::beforeSave';
});
PubSub::subscribe('afterSave', function() {
echo 'PubSub::afterSave';
});
PubSub::publish('beforeSave');
?>
<?php
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);
// If there's only one event, then call it and return the value
if( count(self::$events[$name]) === 1 ) {
if( is_callable(self::$events[$name][0]) )
return call_user_func_array(self::$events[$name][0], $params);
else
return false;
}
// Loop through all the events and call them
foreach( self::$events[$name] as $event ) {
if( is_callable($event) )
call_user_func_array($event, $params);
}
}
public static function unsubscribe($name) {
if( !empty(self::$events[$name]) )
unset(self::$events[$name]);
}
}
// Basic usage
PubSub::subscribe('beforeSave', function() {
echo 'PubSub::beforeSave';
});
PubSub::subscribe('afterSave', function() {
echo 'PubSub::afterSave';
});
PubSub::unsubscribe('beforeSave');
PubSub::publish('beforeSave');
?>
<?php
// user_manager.php from xxx.com
PubSub::subscribe('saveLogin', function($user_token) {
$_SESSION['user_token'] = $user_token;
});
// my_file.php
PubSub::subcribe('saveLogin', function($user_token) {
Cookie::set('user_token', '1 year', $user_token);
});
?>
Array
(
[beforeSave] => Array
(
[0] => Closure Object
(
)
)
[afterSave] => Array
(
[0] => Closure Object
(
)
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment