XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
XCube_Delegate.class.php
1<?php
31
32class XCube_Ref {
37 public $_mObject;
38
44 public function __construct( &$obj ) {
45 $this->_mObject =& $obj;
46 }
47
56 public function &getObject() {
57 return $this->_mObject;
58 }
59}
60
61//
62// Constants for delegate priority.
63// But, developers should use {first,normal,firnal} basically.
64//
65
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 );
76
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 );
80
81define( 'XCUBE_DELEGATE_CHAIN_BREAK', - 1 );
82
106 public $_mSignatures = [];
107
112 public $_mCallbacks = [];
113
118 public $_mHasCheckSignatures = false;
119
129 public $_mIsLazyRegister = false;
130
136
140 public $_mUniqueID;
141
154 public function __construct() {
155 if ( func_num_args() ) {
156 $this->_setSignatures( func_get_args() );
157 }
158 $this->_mUniqueID = uniqid( random_int(0, mt_getrandmax()), true );
159 }
160
171 public function _setSignatures( $args ) {
172 $this->_mSignatures =& $args;
173 foreach ( $args as $i => $iValue ) {
174 $arg = $iValue;
175 $idx = strpos( $arg, ' &' );
176 if ( false !== $idx ) {
177 $args[ $i ] = substr( $arg, 0, $idx );
178 }
179 }
180 $this->_mHasCheckSignatures = true;
181 }
182
191 public function register( $delegateName ) {
192 $root =& XCube_Root::getSingleton();
193 if ( null !== $root->mDelegateManager ) {
194 $this->_mIsLazyRegister = false;
195 $this->_mLazyRegisterName = null;
196
197 return $root->mDelegateManager->register( $delegateName, $this );
198 }
199
200 $this->_mIsLazyRegister = true;
201 $this->_mLazyRegisterName = $delegateName;
202
203 return false;
204 }
205
224 public function add( $callback, $param2 = null, $param3 = null ) {
225 $priority = XCUBE_DELEGATE_PRIORITY_NORMAL;
226 $filepath = null;
227
228 //@gigamaster fixed to save memory
229 if ( ! is_array( $callback ) && strpos( $callback, '::' ) !== false && 2 === count( $tmp = explode( '::', $callback ) ) ) {
230 $callback = $tmp;
231 }
232
233 if ( null !== $param2 ) {
234 if ( is_int( $param2 ) ) {
235 $priority = $param2;
236 $filepath = ( null !== $param3 && is_string( $param3 ) ) ? $param3 : null;
237 } elseif ( is_string( $param2 ) ) {
238 $filepath = $param2;
239 }
240 }
241
242 $this->_mCallbacks[ $priority ][] = [ $callback, $filepath ];
243 ksort( $this->_mCallbacks );
244 }
245
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];
258 if ( XCube_DelegateUtils::_compareCallback( $callback, $delcallback ) ) {
259 unset( $this->_mCallbacks[ $priority ][ $idx ] );
260 }
261 if ( 0 === (is_countable($this->_mCallbacks[ $priority ]) ? count( $this->_mCallbacks[ $priority ] ) : 0) ) {
262 unset( $this->_mCallbacks[ $priority ] );
263 }
264 }
265 }
266 }
267
275 public function reset() {
276 unset( $this->_mCallbacks );
277 $this->_mCallbacks = [];
278 }
279
284 public function call() {
285 $mSigs = [];
286 $args = func_get_args();
287 $num = func_num_args();
288
289 if ( $this->_mIsLazyRegister ) {
290 $this->register( $this->_mLazyRegisterName );
291 }
292
293 if ( ( $hasSig = $this->_mHasCheckSignatures ) && (is_countable($mSigs = &$this->_mSignatures) ? count( $mSigs = &$this->_mSignatures ) : 0) !== $num ) {
294 return false;
295 }
296
297 for ( $i = 0; $i < $num; $i ++ ) {
298 $arg = &$args[ $i ];
299 if ( $arg instanceof XCube_Ref ) {
300 $args[ $i ] =& $arg->getObject();
301 }
302
303 if ( $hasSig ) {
304 if ( ! isset( $mSigs[ $i ] ) ) {
305 return false;
306 }
307 switch ( $mSigs[ $i ] ) {
308 case 'void':
309 break;
310
311 case 'bool':
312 if ( ! empty( $arg ) ) {
313 $args[ $i ] = $arg ? true : false;
314 // $args[ $i ] = (bool)$arg; // Test replace with (bool)
315 }
316 break;
317
318 case 'int':
319 if ( ! empty( $arg ) ) {
320 $args[ $i ] = (int) $arg;
321 }
322 break;
323
324 case 'float':
325 if ( ! empty( $arg ) ) {
326 $args[ $i ] = (float) $arg;
327 }
328 break;
329
330 case 'string':
331 if ( ! empty( $arg ) && ! is_string( $arg ) ) {
332 return false;
333 }
334 break;
335
336 default:
337 if ( ! is_a( $arg, $mSigs[ $i ] ) ) {
338 return false;
339 }
340 }
341 }
342 }
343
344 foreach ( $this->_mCallbacks as $callback_arrays ) {
345 foreach ( $callback_arrays as $callback_array ) {
346 [$callback, $file] = $callback_array;
347
348 if ( $file ) {
349 require_once $file;
350 }
351 if ( is_callable( $callback ) && XCUBE_DELEGATE_CHAIN_BREAK === call_user_func_array( $callback, $args ) ) {
352 break 2;
353 }
354 }
355 }
356 }
357
363 public function isEmpty() {
364 return ( 0 === count( $this->_mCallbacks ) );
365 }
366
374 public function getID() {
375 return $this->_mUniqueID;
376 }
377}
378
393 public $_mCallbacks = [];
394
400
405 public $_mDelegates = [];
406
411 public function __construct() {
412 }
413
429 public function register( $name, &$delegate ) {
430 $mDelegate =& $this->_mDelegates[ $name ];
431 if ( isset( $mDelegate[ $id = $delegate->getID() ] ) ) {
432 return false;
433 }
434
435 $mDelegate[ $id ] =& $delegate;
436
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 );
442 }
443 }
444
445 return true;
446 }
447
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 );
468 }
469 }
470 $this->_mCallbacks[ $name ][] = $callback;
471 $this->_mCallbackParameters[ $name ][] = [ '0' => $param3, '1' => $param4 ];
472 }
473
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 );
487 }
488 }
489 if ( isset( $this->_mCallbacks[ $name ] ) ) {
490 foreach ( array_keys( $this->_mCallbacks[ $name ] ) as $key ) {
491 $callback = $this->_mCallbacks[ $name ][ $key ];
492 if ( XCube_DelegateUtils::_compareCallback( $callback, $delcallback ) ) {
493 unset( $this->_mCallbacks[ $name ][ $key ], $this->_mCallbackParameters[ $name ][ $key ] );
494 }
495 }
496 }
497 }
498
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();
511 }
512 }
513 if ( isset( $this->_mCallbacks[ $name ] ) ) {
514 unset( $this->_mCallbacks[ $name ], $this->_mCallbackParameters[ $name ] );
515 }
516 }
517
526 public function isEmpty(string $name ) {
527 if ( isset( $this->_mDelegates[ $name ] ) ) {
528 return $this->_mDelegates[ $name ]->isEmpty();
529 }
530
531 return isset( $this->_mCallbacks[ $name ] ) ? ( 0 === (is_countable($this->_mCallbacks[ $name ]) ? count( $this->_mCallbacks[ $name ] ) : 0) ) : false;
532 }
533
538 public function getDelegates() {
539 return $this->_mDelegates;
540 }
541}
542
556 public function __construct() {
557 }
558
559 public static function call() {
560 $args = func_get_args();
561 $num = func_num_args();
562 if ( 1 === $num ) {
563 $delegateName = $args[0];
564 } elseif ( $num ) {
565 $delegateName = array_shift( $args );
566 } else {
567 return false;
568 }
569 $m =& XCube_Root::getSingleton()->mDelegateManager;
570 if ( $m ) {
571 $delegates = $m->getDelegates();
572 if ( isset( $delegates[ $delegateName ] ) ) {
573 $delegates = &$delegates[ $delegateName ];
574 [$key] = array_keys( $delegates );
575 $delegate =& $delegates[ $key ];
576 } else {
577 $delegate = new XCube_Delegate();
578 $m->register( $delegateName, $delegate );
579 }
580 }
581
582 return call_user_func_array( [ &$delegate, 'call' ], $args );
583 }
584
639 public static function raiseEvent() {
640 if ( func_num_args() ) {
641 $args = func_get_args();
642
643 return call_user_func_array( [ 'XCube_DelegateUtils', 'call' ], $args );
644 }
645 }
646
661 public static function applyStringFilter() {
662 $args = func_get_args();
663 $num = func_num_args();
664 if ( $num > 1 ) {
665 $delegateName = $args[0];
666 $string = $args[1];
667 if ( ! empty( $string ) && is_string( $string ) ) {
668 return '';
669 }
670 $args[1] = new XCube_Ref( $string );
671 call_user_func_array( [ 'XCube_DelegateUtils', 'call' ], $args );
672
673 return $string;
674 }
675
676 return '';
677 }
678
692 public static function _compareCallback( $callback1, $callback2 ) {
693 if ( ! is_array( $callback1 ) && ! is_array( $callback2 ) && ( $callback1 === $callback2 ) ) {
694 return true;
695 }
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] ) ) {
699 return true;
700 }
701 if ( is_object( $callback1[0] ) && ( get_class( $callback1[0] ) === get_class( $callback2[0] ) ) ) {
702 return true;
703 }
704 }
705
706 return false;
707 }
708}
[Final] Used for the simple mechanism for common delegation in XCube.
__construct()
Constructor.
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.
Manager for delegates.
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.
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 raiseEvent()
[Static] Utility method for calling event-delegates.