Created
February 26, 2014 17:51
-
-
Save nash-ye/9234674 to your computer and use it in GitHub Desktop.
This file contains 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 | |
/** | |
* Event manager | |
* | |
* @since 1.3 | |
*/ | |
class EventManager { | |
/*** Properties ***********************************************************/ | |
/** | |
* Registered events list. | |
* | |
* @var array | |
* @since 1.3 | |
*/ | |
protected $events = array(); | |
/** Methods ***************************************************************/ | |
/** | |
* Trigger all listeners for a given event | |
* | |
* @return void | |
* @since 1.3 | |
*/ | |
public function trigger( $event, $arg = '' ) { | |
if ( is_array( $event ) ) { | |
foreach( $event as $name ) { | |
$this->trigger( $name ); | |
} | |
} elseif ( isset( $this->events[ $event ] ) ) { | |
$args = array(); | |
for ( $i = 1; $i < func_num_args(); $i++ ) { | |
$args[] = func_get_arg( $i ); | |
} | |
foreach( $this->events[ $event ] as $listener ) { | |
call_user_func_array( $listener, $args ); | |
} | |
} | |
} | |
/** | |
* Attach a listener to an event | |
* | |
* @return void | |
* @since 1.3 | |
*/ | |
public function attach( $event, $listener, $priority = 10 ) { | |
if ( ! is_callable( $listener ) ) { | |
throw new Exception( 'The event listener is not a valid callback.' ); | |
} | |
if ( is_array( $event ) ) { | |
foreach( $event as $name ) { | |
$this->attach( $name, $listener, $priority ); | |
} | |
} else { | |
if ( ! isset( $this->events[ $event ] ) ) { | |
$this->events[ $event ] = new PriorityArray(); | |
} | |
$this->events[ $event ]->offsetSet( $this->build_listener_id( $listener ), $listener, $priority ); | |
} | |
} | |
/** | |
* Unsubscribe a listener from an event | |
* | |
* @return void | |
* @since 1.3 | |
*/ | |
public function detach( $event, $listener ) { | |
if ( ! is_callable( $listener ) ) { | |
throw new Exception( 'The event listener is not a valid callback.' ); | |
} | |
if ( is_array( $event ) ) { | |
foreach( $event as $name ) { | |
$this->detach( $name, $listener ); | |
} | |
} elseif ( isset( $this->events[ $event ] ) ) { | |
$this->events[ $event ]->offsetUnset( $this->build_listener_id( $listener ) ); | |
} | |
} | |
/** | |
* Build Unique ID for listeners callbacks. | |
* | |
* @access private | |
* @return string | |
* @since 1.3 | |
*/ | |
protected function build_listener_id( $listener ) { | |
if ( is_string( $listener ) ) { | |
return $listener; | |
} | |
return spl_object_hash( (object) $listener ); | |
} | |
/** | |
* Clear all listeners for a given event | |
* | |
* @return void | |
* @since 1.3 | |
*/ | |
public function clear_listeners( $event ) { | |
unset( $this->events[ $event ] ); | |
} | |
/** | |
* Gel all registered events. | |
* | |
* @return array | |
* @since 1.3 | |
*/ | |
public function get_events() { | |
return array_keys( $this->events ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
PriorityArray: https://gist.github.com/nash-ye/9230544