45 $this->_mObject =& $obj;
66define(
'XCUBE_DELEGATE_PRIORITY_1', 10 );
67define(
'XCUBE_DELEGATE_PRIORITY_2', 20 );
68define(
'XCUBE_DELEGATE_PRIORITY_3', 30 );
69define(
'XCUBE_DELEGATE_PRIORITY_4', 40 );
70define(
'XCUBE_DELEGATE_PRIORITY_5', 50 );
71define(
'XCUBE_DELEGATE_PRIORITY_6', 60 );
72define(
'XCUBE_DELEGATE_PRIORITY_7', 70 );
73define(
'XCUBE_DELEGATE_PRIORITY_8', 80 );
74define(
'XCUBE_DELEGATE_PRIORITY_9', 90 );
75define(
'XCUBE_DELEGATE_PRIORITY_10', 100 );
77define(
'XCUBE_DELEGATE_PRIORITY_FIRST', XCUBE_DELEGATE_PRIORITY_1 );
78define(
'XCUBE_DELEGATE_PRIORITY_NORMAL', XCUBE_DELEGATE_PRIORITY_5 );
79define(
'XCUBE_DELEGATE_PRIORITY_FINAL', XCUBE_DELEGATE_PRIORITY_10 );
81define(
'XCUBE_DELEGATE_CHAIN_BREAK', - 1 );
129 public $_mIsLazyRegister =
false;
155 if ( func_num_args() ) {
158 $this->_mUniqueID = uniqid( random_int(0, mt_getrandmax()),
true );
172 $this->_mSignatures =& $args;
173 foreach ( $args as $i => $iValue ) {
175 $idx = strpos( $arg,
' &' );
176 if (
false !== $idx ) {
177 $args[ $i ] = substr( $arg, 0, $idx );
180 $this->_mHasCheckSignatures =
true;
191 public function register( $delegateName ) {
192 $root =& XCube_Root::getSingleton();
193 if (
null !== $root->mDelegateManager ) {
194 $this->_mIsLazyRegister =
false;
195 $this->_mLazyRegisterName =
null;
197 return $root->mDelegateManager->register( $delegateName, $this );
200 $this->_mIsLazyRegister =
true;
201 $this->_mLazyRegisterName = $delegateName;
224 public function add( $callback, $param2 =
null, $param3 =
null ) {
225 $priority = XCUBE_DELEGATE_PRIORITY_NORMAL;
229 if ( ! is_array( $callback ) && strpos( $callback,
'::' ) !==
false && 2 === count( $tmp = explode(
'::', $callback ) ) ) {
233 if (
null !== $param2 ) {
234 if ( is_int( $param2 ) ) {
236 $filepath = (
null !== $param3 && is_string( $param3 ) ) ? $param3 :
null;
237 } elseif ( is_string( $param2 ) ) {
242 $this->_mCallbacks[ $priority ][] = [ $callback, $filepath ];
243 ksort( $this->_mCallbacks );
254 public function delete( $delcallback ) {
255 foreach ( array_keys( $this->_mCallbacks ) as $priority ) {
256 foreach ( array_keys( $this->_mCallbacks[ $priority ] ) as $idx ) {
257 $callback = $this->_mCallbacks[ $priority ][ $idx ][0];
259 unset( $this->_mCallbacks[ $priority ][ $idx ] );
261 if ( 0 === (is_countable($this->_mCallbacks[ $priority ]) ? count( $this->_mCallbacks[ $priority ] ) : 0) ) {
262 unset( $this->_mCallbacks[ $priority ] );
276 unset( $this->_mCallbacks );
277 $this->_mCallbacks = [];
286 $args = func_get_args();
287 $num = func_num_args();
289 if ( $this->_mIsLazyRegister ) {
293 if ( ( $hasSig = $this->_mHasCheckSignatures ) && (is_countable($mSigs = &$this->_mSignatures) ? count( $mSigs = &$this->_mSignatures ) : 0) !== $num ) {
297 for ( $i = 0; $i < $num; $i ++ ) {
300 $args[ $i ] =& $arg->getObject();
304 if ( ! isset( $mSigs[ $i ] ) ) {
307 switch ( $mSigs[ $i ] ) {
312 if ( ! empty( $arg ) ) {
313 $args[ $i ] = $arg ? true :
false;
319 if ( ! empty( $arg ) ) {
320 $args[ $i ] = (int) $arg;
325 if ( ! empty( $arg ) ) {
326 $args[ $i ] = (float) $arg;
331 if ( ! empty( $arg ) && ! is_string( $arg ) ) {
337 if ( ! is_a( $arg, $mSigs[ $i ] ) ) {
344 foreach ( $this->_mCallbacks as $callback_arrays ) {
345 foreach ( $callback_arrays as $callback_array ) {
346 [$callback, $file] = $callback_array;
351 if ( is_callable( $callback ) && XCUBE_DELEGATE_CHAIN_BREAK === call_user_func_array( $callback, $args ) ) {
364 return ( 0 === count( $this->_mCallbacks ) );
374 public function getID() {
375 return $this->_mUniqueID;
429 public function register( $name, &$delegate ) {
430 $mDelegate =& $this->_mDelegates[ $name ];
431 if ( isset( $mDelegate[ $id = $delegate->getID() ] ) ) {
435 $mDelegate[ $id ] =& $delegate;
437 $mcb = &$this->_mCallbacks[ $name ];
438 if ( isset( $mcb ) && (is_countable($mcb) ? count( $mcb ) : 0) > 0 ) {
439 foreach ( $mcb as $key => $func ) {
440 [$a, $b] = $this->_mCallbackParameters[ $name ][ $key ];
441 $delegate->add( $func, $a, $b );
464 public function add(
string $name, $callback, $param3 =
null, $param4 =
null ) {
465 if ( isset( $this->_mDelegates[ $name ] ) ) {
466 foreach ( $this->_mDelegates[ $name ] as $func ) {
467 $func->add( $callback, $param3, $param4 );
470 $this->_mCallbacks[ $name ][] = $callback;
471 $this->_mCallbackParameters[ $name ][] = [
'0' => $param3,
'1' => $param4 ];
483 public function delete(
string $name, $delcallback ) {
484 if ( isset( $this->_mDelegates[ $name ] ) ) {
485 foreach ( array_keys( $this->_mDelegates[ $name ] ) as $key ) {
486 $this->_mDelegates[ $name ][ $key ]->delete( $delcallback );
489 if ( isset( $this->_mCallbacks[ $name ] ) ) {
490 foreach ( array_keys( $this->_mCallbacks[ $name ] ) as $key ) {
491 $callback = $this->_mCallbacks[ $name ][ $key ];
493 unset( $this->_mCallbacks[ $name ][ $key ], $this->_mCallbackParameters[ $name ][ $key ] );
507 public function reset(
string $name ) {
508 if ( isset( $this->_mDelegates[ $name ] ) ) {
509 foreach ( array_keys( $this->_mDelegates[ $name ] ) as $key ) {
510 $this->_mDelegates[ $name ][ $key ]->reset();
513 if ( isset( $this->_mCallbacks[ $name ] ) ) {
514 unset( $this->_mCallbacks[ $name ], $this->_mCallbackParameters[ $name ] );
527 if ( isset( $this->_mDelegates[ $name ] ) ) {
528 return $this->_mDelegates[ $name ]->isEmpty();
531 return isset( $this->_mCallbacks[ $name ] ) ? ( 0 === (is_countable($this->_mCallbacks[ $name ]) ? count( $this->_mCallbacks[ $name ] ) : 0) ) :
false;
559 public static function call() {
560 $args = func_get_args();
561 $num = func_num_args();
563 $delegateName = $args[0];
565 $delegateName = array_shift( $args );
569 $m =& XCube_Root::getSingleton()->mDelegateManager;
571 $delegates = $m->getDelegates();
572 if ( isset( $delegates[ $delegateName ] ) ) {
573 $delegates = &$delegates[ $delegateName ];
574 [$key] = array_keys( $delegates );
575 $delegate =& $delegates[ $key ];
577 $delegate =
new XCube_Delegate();
578 $m->register( $delegateName, $delegate );
582 return call_user_func_array( [ &$delegate,
'call' ], $args );
640 if ( func_num_args() ) {
641 $args = func_get_args();
643 return call_user_func_array( [
'XCube_DelegateUtils',
'call' ], $args );
662 $args = func_get_args();
663 $num = func_num_args();
665 $delegateName = $args[0];
667 if ( ! empty( $string ) && is_string( $string ) ) {
671 call_user_func_array( [
'XCube_DelegateUtils',
'call' ], $args );
693 if ( ! is_array( $callback1 ) && ! is_array( $callback2 ) && ( $callback1 === $callback2 ) ) {
696 if ( is_array( $callback1 ) && is_array( $callback2 ) && ( gettype( $callback1[0] ) === gettype( $callback2[0] ) )
697 && ( $callback1[1] === $callback2[1] ) ) {
698 if ( ! is_object( $callback1[0] ) && ( $callback1[0] === $callback2[0] ) ) {
701 if ( is_object( $callback1[0] ) && ( get_class( $callback1[0] ) === get_class( $callback2[0] ) ) ) {
[Final] Used for the simple mechanism for common delegation in XCube.
__construct()
Constructor.
$_mHasCheckSignatures
bool
reset()
Resets all delegate functions from this object.
_setSignatures( $args)
Set signatures for this delegate.
$_mLazyRegisterName
string - the registry name for lazy registration.
add( $callback, $param2=null, $param3=null)
[Overload] Connects functions to this object as callback functions
$_mCallbacks
Complex Array - This is Array for callback type data.
isEmpty()
Gets a value indicating whether this object has callback functions.
call()
Calls connected functions of this object.
$_mSignatures
Vector Array - The list of type of parameters.
__construct()
Constructor.
add(string $name, $callback, $param3=null, $param4=null)
Connects functions to the delegate that have the specified name.
isEmpty(string $name)
Gets a value indicating whether the specified delegate has callback functions.
$_mDelegates
Map Array - std::map<string, XCube_Delegate*>
reset(string $name)
Resets all functions off the delegate that have the specified name.
$_mCallbacks
Complex Array.
$_mCallbackParameters
Complex Array.
Utility class which collects utility functions for delegates.
static _compareCallback( $callback1, $callback2)
__construct()
Private Construct. In other words, it's possible to create an instance of this class.
static applyStringFilter()
static raiseEvent()
[Static] Utility method for calling event-delegates.