18 public static function getEvent( $name = null ){
return static::getEvents( $name ); }
22 public static function register( )
24 $namespace = @strtoupper( @str_replace(
'\\' ,
'_' , __NAMESPACE__ ) ) .
'_';
25 if ( !defined(
'_PTCEVENT_' . $namespace ) )
27 @define(
'_PTCEVENT_' . $namespace , get_called_class( ) );
37 public static function listen( $event , $callback , $priority = 0 )
39 static::register( $class = null );
40 $event = explode(
'.' , $event );
41 if (
sizeof( $event ) < 1 )
43 trigger_error(
'Invalid format, all event names must use "."!' , E_USER_ERROR );
46 if ( $callback instanceof Closure || is_callable( $callback ) ){ $call = $callback; }
49 $try = explode(
'@' , $callback );
50 if ( @class_exists( $try[ 0 ] ) )
52 $method = (
sizeof( $try ) > 1 ) ? $try[ 1 ] :
'handle';
53 $call = array(
new $try[ 0 ] , $method );
57 trigger_error( $callback .
' is not a valid callback parameter!' , E_USER_ERROR );
61 if ( $event[ 1 ] ===
'*' )
63 $debug =
' wildcard to ';
64 if ( !array_key_exists( $event[ 0 ] , static::$_wildCards ) )
66 static::$_wildCards[ $event[ 0 ] ] = array( );
67 $debug =
' new wildcard ';
69 static::$_wildCards[ $event[ 0 ] ][ ] = $call;
70 static::_debug( array( $call ) ,
'added ' . $debug .
71 '<b>"' . $event[ 0 ] .
'.*"</b>' ,
'Event Manager' );
74 static::_addEvent( $event , $call , $priority );
83 if ( $name && !array_key_exists( $name , static::$_events ) ){
return false; }
84 return ( $name ) ? static::$_events[ $name ] : static::$_events;
91 public static function remove( $event , $key = null )
93 $event = explode(
'.' , $event );
94 if ( array_key_exists( 1 , $event ) )
96 if ( is_numeric( $key ) )
98 if ( !array_key_exists( $key , static::$_events[ $event[ 0 ] ][ $event[ 1 ] ] ) )
100 trigger_error( $key .
' not found in <b>' . $event[ 0 ] .
101 '.' . $event[ 1 ] .
'</b>!' , E_USER_WARNING );
104 static::_debug( static::$_events[ $event[ 0 ] ][ $event[ 1 ] ][ $key ] ,
105 'removing event <b>' . $event[ 0 ] .
'.' . $event[ 1 ] .
106 '[ ' . $key .
' ]</b>' ,
'Event Manager' );
107 unset( static::$_events[ $event[ 0 ] ][ $event[ 1 ] ][ $key ] );
111 $debug = array_pop( static::$_events[ $event[ 0 ] ][ $event[ 1 ] ] );
112 static::_debug( $debug ,
'removing last event from <b>' .
113 $event[ 0 ] .
'.' . $event[ 1 ] .
'</b>' ,
'Event Manager' );
118 if ( @empty( static::$_events[ $event[ 0 ] ][ $event[ 1 ] ] ) )
120 unset( static::$_events[ $event[ 0 ] ][ $event[ 1 ] ] );
122 if ( @empty( static::$_events[ $event[ 0 ] ] ) ){ unset( static::$_events[ $event[ 0 ] ] ); }
129 public static function fire( $event , $data )
133 $event = explode(
'.' , $event );
134 if ( !array_key_exists( $event[ 0 ] , static::$_events ) ||
sizeof( $event ) < 1 ||
135 !array_key_exists( $event[ 1 ] , static::$_events[ $event[ 0 ] ] ) )
137 trigger_error(
'No listeners defined named "' . $main .
'"!' , E_USER_WARNING );
140 $events = static::$_events[ $event[ 0 ] ][ $event[ 1 ] ];
141 $events = array_reverse( $events );
142 uasort( $events ,
function ( $a , $b ){
return $b[
'priority' ] - $a[
'priority' ]; } );
143 $events = array_map(
function ( $i ){
return $i[
'callback' ]; }, $events );
144 if ( array_key_exists( $event[ 0 ] , static::$_wildCards ) )
147 foreach ( static::$_wildCards[ $event[ 0 ] ] as $wildcard )
149 $data = ( is_array( $data ) ) ? $data : array( $data );
150 static::_debug( array(
'callback' => $wildcard ,
'data' => array( $data , $main ) ) ,
151 'firing wildcard <b>' . $event[ 0 ] .
'.' . $event[ 1 ] .
'[ ' . $a .
' ]</b>' ,
'Event Manager' );
153 if (
false === static::_run( $wildcard , array( $data , $main ) ) ){
return; }
157 foreach ( $events as $sub_event )
159 static::_debug( array(
'callback' => $sub_event ,
'data' => $data ) ,
160 'firing event <b>' . $event[ 0 ] .
'.' . $event[ 1 ] .
'[ ' . $a .
' ]</b>' ,
'Event Manager' );
162 if (
false === static::_run( $sub_event , $data ) ){
return; }
180 protected static function _addEvent( $event , $call , $priority = 0 )
182 $debug =
'1 more event to ';
183 if ( !array_key_exists( $event[ 0 ] , static::$_events ) )
185 static::$_events[ $event[ 0 ] ] = array( );
186 $debug =
'new event ';
188 if ( !array_key_exists( $event[ 1 ] , static::$_events[ $event[ 0 ] ] ) )
190 static::$_events[ $event[ 0 ] ][ $event[ 1 ] ] = array( );
191 $debug =
'new event ';
193 static::$_events[ $event[ 0 ] ][ $event[ 1 ] ][ ] =
194 array(
'priority' => $priority ,
'callback' => $call );
195 static::_debug(
@end( array_values( static::$_events[ $event[ 0 ] ][ $event[ 1 ] ] ) ) ,
196 'added ' . $debug .
'<b>"' . $event[ 0 ] .
'.' . $event[ 1 ] .
'"</b>' ,
'Event Manager' );
203 protected static function _run( $event , $data )
205 $data = ( is_array( $data ) ) ? $data : array( $data );
206 return call_user_func_array( $event , $data );
214 protected static function _debug( $string , $statement = null , $category = null )
216 if ( !defined(
'_PTCDEBUG_NAMESPACE_' ) ) {
return false; }
217 return @call_user_func_array( array(
'\\' . _PTCDEBUG_NAMESPACE_ ,
'bufferLog' ) ,
218 array( $string , $statement , $category ) );
static _addEvent($event, $call, $priority=0)
Adds events to the class.
static $_events
Property that holds the events.
static $_wildCards
Property that holds the wildcards.
static getEvent($name=null)
Alias of PtcEvent::getEvents( ).
static fire($event, $data)
Fires an event.
static listen($event, $callback, $priority=0)
Adds a listener to an event.
static getEvents($name=null)
Returs the current events.
static _run($event, $data)
Runs an event with call_user_func_array.
static _debug($string, $statement=null, $category=null)
Send messsages to the PtcDebug class if present.