Created
February 3, 2011 00:18
-
-
Save emad-elsaid/808803 to your computer and use it in GitHub Desktop.
this is a straight implementation of SignalSlot pattern in PHP
This file contains hidden or 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 | |
/* | |
* @Script: Signal Slot | |
* @Version: 0.1 | |
* @Author: Emad Elsaid | |
* @Email: [email protected] | |
* @Website: http://blazeeboy.blogspot.com | |
* @What is that ?: | |
* this is an implemntation of Signal/Slot pattern in PHP | |
* you can bind group of functions to one or more signals | |
* then you can send signals , it'll execute the group of | |
* functions in the same order you bind them. | |
* | |
* function pp() | |
* { | |
* echo "print print"; | |
* } | |
* | |
* SignalSlot::bind('print', 'pp' ); | |
* SignalSlot::send( 'print' ); | |
* | |
* | |
* Note: this class in is a Singleton, and it'll initialise itself | |
* on first use of any static method. | |
*/ | |
// check if the class previously declared. | |
if( ! class_exists('SignalSlot') ) | |
{ | |
class SignalSlot | |
{ | |
// the singleton instance of that class | |
static private $instance = NULL; | |
// an associative array where slot as Key and functions as value array. | |
private $signals = array(); | |
// constructor must be private in order to prevent making other instances | |
private function __construct(){} | |
// bind function to some signal. | |
static public function bind( $signal, $function ) | |
{ | |
// signal and function must be strings | |
if( !is_string($signal) or !is_string($function) ) | |
{ | |
throw new Exception( 'Signal and Function must be String' ); | |
return FALSE; | |
} | |
// function must exists | |
if( !function_exists( $function ) ) | |
{ | |
throw new Exception( 'Function not exist' ); | |
return FALSE; | |
} | |
// initialise singleton if not initialised | |
if( !is_object(SignalSlot::$instance) ) | |
SignalSlot::$instance = new SignalSlot(); | |
// initialise key with empty function if not exists | |
if( !array_key_exists( $signal, SignalSlot::$instance->signals ) ) | |
SignalSlot::$instance->signals[$signal] = array(); | |
// add function to the signals array if not exists in the signal key. | |
if( !in_array( $function, SignalSlot::$instance->signals[$signal] ) ) | |
SignalSlot::$instance->signals[$signal][] = $function; | |
return TRUE; | |
} | |
// unlink function from signal | |
static public function unbind( $signal, $function ) | |
{ | |
// signal and function must be strings | |
if( !is_string($signal) or !is_string($function) ) | |
{ | |
throw new Exception( 'Signal and Function must be String' ); | |
return FALSE; | |
} | |
// initialise singleton if not initialised | |
if( !is_object(SignalSlot::$instance) ) | |
SignalSlot::$instance = new SignalSlot(); | |
// if key exists and function position in the signals array not false | |
// then unset it from that key's array | |
if( array_key_exists( $signal, SignalSlot::$instance->signals ) | |
and array_search($function,SignalSlot::$instance->signals[$signal])!==FALSE ) | |
{ | |
unset(SignalSlot::$instance->signals[$signal] | |
[array_search($function,SignalSlot::$instance->signals[$signal])]); | |
return TRUE; | |
} | |
else | |
{ | |
return FALSE; | |
} | |
} | |
// send signal to execute all linked functions. | |
static public function send( $signal ) | |
{ | |
// initialise singleton if not initialised | |
if( !is_object(SignalSlot::$instance) ) | |
SignalSlot::$instance = new SignalSlot(); | |
// if that signal key not with us then return false | |
if( !array_key_exists( $signal, SignalSlot::$instance->signals ) ) | |
return FALSE; | |
// execute all functions to that key | |
foreach( SignalSlot::$instance->signals[$signal] as $function ) | |
$function(); | |
return TRUE; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment