Total Complexity | 379 |
Total Lines | 1829 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Complex classes like TComponent often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TComponent, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
377 | class TComponent |
||
378 | { |
||
379 | /** |
||
380 | * @var array event handler lists |
||
381 | */ |
||
382 | protected $_e = []; |
||
383 | |||
384 | /** |
||
385 | * @var bool if listening is enabled. Automatically turned on or off in |
||
386 | * constructor according to {@see getAutoGlobalListen}. Default false, off |
||
387 | */ |
||
388 | protected $_listeningenabled = false; |
||
389 | |||
390 | /** |
||
391 | * @var array static registered global event handler lists |
||
392 | */ |
||
393 | private static $_ue = []; |
||
394 | |||
395 | /** |
||
396 | * @var bool if object behaviors are on or off. default true, on |
||
397 | */ |
||
398 | protected $_behaviorsenabled = true; |
||
399 | |||
400 | /** |
||
401 | * @var TPriorityMap list of object behaviors |
||
402 | */ |
||
403 | protected $_m; |
||
404 | |||
405 | /** |
||
406 | * @var array static global class behaviors, these behaviors are added upon instantiation of a class |
||
407 | */ |
||
408 | private static $_um = []; |
||
409 | |||
410 | |||
411 | /** |
||
412 | * @const string the name of the global {@see raiseEvent} listener |
||
413 | */ |
||
414 | public const GLOBAL_RAISE_EVENT_LISTENER = 'fxGlobalListener'; |
||
415 | |||
416 | |||
417 | /** |
||
418 | * The common __construct. |
||
419 | * If desired by the new object, this will auto install and listen to global event functions |
||
420 | * as defined by the object via 'fx' methods. This also attaches any predefined behaviors. |
||
421 | * This function installs all class behaviors in a class hierarchy from the deepest subclass |
||
422 | * through each parent to the top most class, TComponent. |
||
423 | */ |
||
424 | public function __construct() |
||
425 | { |
||
426 | if ($this->getAutoGlobalListen()) { |
||
427 | $this->listen(); |
||
428 | } |
||
429 | |||
430 | $classes = $this->getClassHierarchy(true); |
||
431 | array_pop($classes); |
||
432 | foreach ($classes as $class) { |
||
433 | if (isset(self::$_um[$class])) { |
||
434 | $this->attachBehaviors(self::$_um[$class], true); |
||
435 | } |
||
436 | } |
||
437 | } |
||
438 | |||
439 | /** |
||
440 | * The common __clone magic method from PHP's "clone". |
||
441 | * This reattaches the behaviors to the cloned object. |
||
442 | * IBehavior objects are cloned, IClassBehaviors are not. |
||
443 | * Clone object events are scrubbed of the old object behaviors' events. |
||
444 | * To finalize the behaviors, dyClone is raised. |
||
445 | * @since 4.3.0 |
||
446 | */ |
||
447 | public function __clone() |
||
448 | { |
||
449 | foreach ($this->_e as $event => $handlers) { |
||
450 | $this->_e[$event] = clone $handlers; |
||
451 | } |
||
452 | $behaviorArray = array_values(($this->_m !== null) ? $this->_m->toArray() : []); |
||
453 | if (count($behaviorArray) && count($this->_e)) { |
||
454 | $behaviorArray = array_combine(array_map('spl_object_id', $behaviorArray), $behaviorArray); |
||
455 | foreach ($this->_e as $event => $handlers) { |
||
456 | foreach ($handlers->toArray() as $handler) { |
||
457 | $a = is_array($handler); |
||
458 | if ($a && array_key_exists(spl_object_id($handler[0]), $behaviorArray) || !$a && array_key_exists(spl_object_id($handler), $behaviorArray)) { |
||
|
|||
459 | $handlers->remove($handler); |
||
460 | } |
||
461 | } |
||
462 | } |
||
463 | } |
||
464 | if ($this->_m !== null) { |
||
465 | $behaviors = $this->_m; |
||
466 | $this->_m = new TPriorityMap(); |
||
467 | foreach ($behaviors->getPriorities() as $priority) { |
||
468 | foreach ($behaviors->itemsAtPriority($priority) as $name => $behavior) { |
||
469 | if ($behavior instanceof IBehavior) { |
||
470 | $behavior = clone $behavior; |
||
471 | } |
||
472 | $this->attachBehavior($name, $behavior, $priority); |
||
473 | } |
||
474 | } |
||
475 | } |
||
476 | $this->callBehaviorsMethod('dyClone', $return); |
||
477 | } |
||
478 | |||
479 | /** |
||
480 | * The common __wakeup magic method from PHP's "unserialize". |
||
481 | * This reattaches the behaviors to the reconstructed object. |
||
482 | * Any global class behaviors are used rather than their unserialized copy. |
||
483 | * Any global behaviors not found in the object will be added. |
||
484 | * To finalize the behaviors, dyWakeUp is raised. |
||
485 | * If a TModule needs to add events to an object during unserialization, |
||
486 | * the module can use a small IClassBehavior [implementing dyWakeUp] |
||
487 | * (adding the event[s]) attached to the class with {@see |
||
488 | * attachClassBehavior} prior to unserialization. |
||
489 | * @since 4.3.0 |
||
490 | */ |
||
491 | public function __wakeup() |
||
492 | { |
||
493 | $classes = $this->getClassHierarchy(true); |
||
494 | array_pop($classes); |
||
495 | $classBehaviors = []; |
||
496 | if ($this->_m !== null) { |
||
497 | $behaviors = $this->_m; |
||
498 | $this->_m = new TPriorityMap(); |
||
499 | foreach ($behaviors->getPriorities() as $priority) { |
||
500 | foreach ($behaviors->itemsAtPriority($priority) as $name => $behavior) { |
||
501 | if ($behavior instanceof IClassBehavior && !is_numeric($name)) { |
||
502 | //Replace class behaviors with their current instances, if they exist. |
||
503 | foreach ($classes as $class) { |
||
504 | if (isset(self::$_um[$class]) && array_key_exists($name, self::$_um[$class])) { |
||
505 | $behavior = self::$_um[$class][$name]->getBehavior(); |
||
506 | break; |
||
507 | } |
||
508 | } |
||
509 | } |
||
510 | $classBehaviors[$name] = $name; |
||
511 | $this->attachBehavior($name, $behavior, $priority); |
||
512 | } |
||
513 | } |
||
514 | } |
||
515 | foreach ($classes as $class) { |
||
516 | if (isset(self::$_um[$class])) { |
||
517 | foreach (self::$_um[$class] as $name => $behavior) { |
||
518 | if (is_numeric($name)) { |
||
519 | continue; |
||
520 | } |
||
521 | if (!array_key_exists($name, $classBehaviors)) { |
||
522 | $this->attachBehaviors([$name => $behavior], true); |
||
523 | } |
||
524 | } |
||
525 | } |
||
526 | } |
||
527 | $this->callBehaviorsMethod('dyWakeUp', $return); |
||
528 | } |
||
529 | |||
530 | |||
531 | /** |
||
532 | * Tells TComponent whether or not to automatically listen to global events. |
||
533 | * Defaults to false because PHP variable cleanup is affected if this is true. |
||
534 | * When unsetting a variable that is listening to global events, {@see unlisten} |
||
535 | * must explicitly be called when cleaning variables allocation or else the global |
||
536 | * event registry will contain references to the old object. This is true for PHP 5.4 |
||
537 | * |
||
538 | * Override this method by a subclass to change the setting. When set to true, this |
||
539 | * will enable {@see __construct} to call {@see listen}. |
||
540 | * |
||
541 | * @return bool whether or not to auto listen to global events during {@see __construct}, default false |
||
542 | */ |
||
543 | public function getAutoGlobalListen() |
||
544 | { |
||
545 | return false; |
||
546 | } |
||
547 | |||
548 | |||
549 | /** |
||
550 | * The common __destruct |
||
551 | * When listening, this unlistens from the global event routines. It also detaches |
||
552 | * all the behaviors so they can clean up, eg remove handlers. |
||
553 | * |
||
554 | * Prior to PHP 7.4, when listening, unlisten must be manually called for objects |
||
555 | * to destruct because circular references will prevent the __destruct process. |
||
556 | */ |
||
557 | public function __destruct() |
||
558 | { |
||
559 | $this->clearBehaviors(); |
||
560 | if ($this->_listeningenabled) { |
||
561 | $this->unlisten(); |
||
562 | } |
||
563 | } |
||
564 | |||
565 | |||
566 | /** |
||
567 | * This utility function is a private array filter method. The array values |
||
568 | * that start with 'fx' are filtered in. |
||
569 | * @param mixed $name |
||
570 | */ |
||
571 | private function filter_prado_fx($name) |
||
572 | { |
||
573 | return strncasecmp($name, 'fx', 2) === 0; |
||
574 | } |
||
575 | |||
576 | |||
577 | /** |
||
578 | * This returns an array of the class name and the names of all its parents. The base object last, |
||
579 | * {@see \Prado\TComponent}, and the deepest subclass is first. |
||
580 | * @param bool $lowercase optional should the names be all lowercase true/false |
||
581 | * @return string[] array of strings being the class hierarchy of $this. |
||
582 | */ |
||
583 | public function getClassHierarchy($lowercase = false) |
||
603 | } |
||
604 | |||
605 | /** |
||
606 | * This caches the 'fx' events for classes. |
||
607 | * @param object $class |
||
608 | * @return string[] fx events from a specific class |
||
609 | */ |
||
610 | protected function getClassFxEvents($class) |
||
611 | { |
||
612 | static $_classfx = []; |
||
613 | $className = $class::class; |
||
614 | if (isset($_classfx[$className])) { |
||
615 | return $_classfx[$className]; |
||
616 | } |
||
617 | $fx = array_filter(get_class_methods($class), [$this, 'filter_prado_fx']); |
||
618 | $_classfx[$className] = $fx; |
||
619 | return $fx; |
||
620 | } |
||
621 | |||
622 | /** |
||
623 | * This adds an object's fx event handlers into the global broadcaster to listen into any |
||
624 | * broadcast global events called through {@see raiseEvent} |
||
625 | * |
||
626 | * Behaviors may implement the function: |
||
627 | * ```php |
||
628 | * public function dyListen($globalEvents[, ?TCallChain $chain = null]) { |
||
629 | * $this->listen($globalEvents); //eg |
||
630 | * if ($chain) |
||
631 | * $chain->dyUnlisten($globalEvents); |
||
632 | * } |
||
633 | * ``` |
||
634 | * to be executed when listen is called. All attached behaviors are notified through dyListen. |
||
635 | * |
||
636 | * @return numeric the number of global events that were registered to the global event registry |
||
637 | */ |
||
638 | public function listen() |
||
639 | { |
||
640 | if ($this->_listeningenabled) { |
||
641 | return; |
||
642 | } |
||
643 | |||
644 | $fx = $this->getClassFxEvents($this); |
||
645 | |||
646 | foreach ($fx as $func) { |
||
647 | $this->getEventHandlers($func)->add([$this, $func]); |
||
648 | } |
||
649 | |||
650 | if (is_a($this, IDynamicMethods::class)) { |
||
651 | $this->attachEventHandler(TComponent::GLOBAL_RAISE_EVENT_LISTENER, [$this, '__dycall']); |
||
652 | array_push($fx, TComponent::GLOBAL_RAISE_EVENT_LISTENER); |
||
653 | } |
||
654 | |||
655 | $this->_listeningenabled = true; |
||
656 | |||
657 | $this->callBehaviorsMethod('dyListen', $return, $fx); |
||
658 | |||
659 | return count($fx); |
||
660 | } |
||
661 | |||
662 | /** |
||
663 | * this removes an object's fx events from the global broadcaster |
||
664 | * |
||
665 | * Behaviors may implement the function: |
||
666 | * ```php |
||
667 | * public function dyUnlisten($globalEvents[, ?TCallChain $chain = null]) { |
||
668 | * $this->behaviorUnlisten(); //eg |
||
669 | * if ($chain) |
||
670 | * $chain->dyUnlisten($globalEvents); |
||
671 | * } |
||
672 | * ``` |
||
673 | * to be executed when listen is called. All attached behaviors are notified through dyUnlisten. |
||
674 | * |
||
675 | * @return numeric the number of global events that were unregistered from the global event registry |
||
676 | */ |
||
677 | public function unlisten() |
||
678 | { |
||
679 | if (!$this->_listeningenabled) { |
||
680 | return; |
||
681 | } |
||
682 | |||
683 | $fx = $this->getClassFxEvents($this); |
||
684 | |||
685 | foreach ($fx as $func) { |
||
686 | $this->detachEventHandler($func, [$this, $func]); |
||
687 | } |
||
688 | |||
689 | if (is_a($this, IDynamicMethods::class)) { |
||
690 | $this->detachEventHandler(TComponent::GLOBAL_RAISE_EVENT_LISTENER, [$this, '__dycall']); |
||
691 | array_push($fx, TComponent::GLOBAL_RAISE_EVENT_LISTENER); |
||
692 | } |
||
693 | |||
694 | $this->_listeningenabled = false; |
||
695 | |||
696 | $this->callBehaviorsMethod('dyUnlisten', $return, $fx); |
||
697 | |||
698 | return count($fx); |
||
699 | } |
||
700 | |||
701 | /** |
||
702 | * Gets the state of listening to global events |
||
703 | * @return bool is Listening to global broadcast enabled |
||
704 | */ |
||
705 | public function getListeningToGlobalEvents() |
||
706 | { |
||
707 | return $this->_listeningenabled; |
||
708 | } |
||
709 | |||
710 | |||
711 | /** |
||
712 | * Calls a method. |
||
713 | * Do not call this method directly. This is a PHP magic method that we override |
||
714 | * to allow behaviors, dynamic events (intra-object/behavior events), |
||
715 | * undefined dynamic and global events, and |
||
716 | * to allow using the following syntax to call a property setter or getter. |
||
717 | * ```php |
||
718 | * $this->getPropertyName($value); // if there's a $this->getjsPropertyName() method |
||
719 | * $this->setPropertyName($value); // if there's a $this->setjsPropertyName() method |
||
720 | * ``` |
||
721 | * |
||
722 | * Additional object behaviors override class behaviors. |
||
723 | * dynamic and global events do not fail even if they aren't implemented. |
||
724 | * Any intra-object/behavior dynamic events that are not implemented by the behavior |
||
725 | * return the first function paramater or null when no parameters are specified. |
||
726 | * |
||
727 | * @param string $method method name that doesn't exist and is being called on the object |
||
728 | * @param mixed $args method parameters |
||
729 | * @throws TInvalidOperationException If the property is not defined or read-only or |
||
730 | * method is undefined |
||
731 | * @return mixed result of the method call, or false if 'fx' or 'dy' function but |
||
732 | * is not found in the class, otherwise it runs |
||
733 | */ |
||
734 | public function __call($method, $args) |
||
735 | { |
||
736 | $getset = substr($method, 0, 3); |
||
737 | if (($getset == 'get') || ($getset == 'set')) { |
||
738 | $propname = substr($method, 3); |
||
739 | $jsmethod = $getset . 'js' . $propname; |
||
740 | if (Prado::method_visible($this, $jsmethod)) { |
||
741 | if (count($args) > 0) { |
||
742 | if ($args[0] && !($args[0] instanceof TJavaScriptString)) { |
||
743 | $args[0] = new TJavaScriptString($args[0]); |
||
744 | } |
||
745 | } |
||
746 | return $this->$jsmethod(...$args); |
||
747 | } |
||
748 | |||
749 | if (($getset == 'set') && Prado::method_visible($this, 'getjs' . $propname)) { |
||
750 | throw new TInvalidOperationException('component_property_readonly', $this::class, $method); |
||
751 | } |
||
752 | } |
||
753 | if ($this->callBehaviorsMethod($method, $return, ...$args)) { |
||
754 | return $return; |
||
755 | } |
||
756 | |||
757 | // don't throw an exception for __magicMethods() or any other weird methods natively implemented by php |
||
758 | if (!method_exists($this, $method)) { |
||
759 | throw new TUnknownMethodException('component_method_undefined', $this::class, $method); |
||
760 | } |
||
761 | } |
||
762 | |||
763 | /** |
||
764 | * This is the magic method that is called when a static function is not found. |
||
765 | * It checks the class if it has an ISingleton instance, in which case its behaviors |
||
766 | * are checked for the static method. This further checks the Class-wide behaviors |
||
767 | * (added with {@see \Prado\TComponent::attachClassBehavior}) for the static method. |
||
768 | * When the static method is found (in either the ISingleton behaviors or the class-wide |
||
769 | * behaviors), the behavior's static method is called and the results returned. |
||
770 | * @param string $method The method name of the static call. |
||
771 | * @param array $args The array of arguments passed to the static call. |
||
772 | * @return mixed the result of the static call. |
||
773 | * @since 4.3.0 |
||
774 | */ |
||
775 | public static function __callStatic(string $method, array $args) |
||
776 | { |
||
777 | $checkedClasses = []; |
||
778 | if (is_a(static::class, ISingleton::class, true) && ($singleton = (static::class)::singleton(false)) && $singleton->getBehaviorsEnabled()) { |
||
779 | foreach ($singleton->getBehaviors() as $behavior) { |
||
780 | $class = $behavior::class; |
||
781 | $lclass = strtolower($class); |
||
782 | if (!isset($checkedClasses[$lclass])) { |
||
783 | if ($behavior->getEnabled() && method_exists($class, $method)) { |
||
784 | return forward_static_call_array([$class, $method], $args); |
||
785 | } |
||
786 | $checkedClasses[$lclass] = true; |
||
787 | } |
||
788 | } |
||
789 | } |
||
790 | |||
791 | if (isset(self::$_um[$lclass = strtolower(static::class)])) { |
||
792 | foreach (self::$_um[$lclass] as $pbehavior) { |
||
793 | $class = $behavior = $pbehavior->getBehavior(); |
||
794 | if (is_array($behavior)) { |
||
795 | $class = $behavior['class']; |
||
796 | } elseif (is_object($behavior)) { |
||
797 | $class = $behavior::class; |
||
798 | } |
||
799 | |||
800 | $lclass = strtolower($class); |
||
801 | if (!isset($checkedClasses[$lclass])) { |
||
802 | if ((!($behavior instanceof IBaseBehavior) || $behavior->getEnabled()) && method_exists($class, $method)) { |
||
803 | return forward_static_call_array([$class, $method], $args); |
||
804 | } |
||
805 | $checkedClasses[$lclass] = true; |
||
806 | } |
||
807 | } |
||
808 | } |
||
809 | |||
810 | // don't throw an exception for __magicMethods() or any other weird methods natively implemented by php |
||
811 | if (!method_exists(static::class, $method)) { |
||
812 | throw new TUnknownMethodException('component_method_undefined', static::class, $method); |
||
813 | } |
||
814 | } |
||
815 | |||
816 | /** |
||
817 | * Returns a property value or an event handler list by property or event name. |
||
818 | * Do not call this method. This is a PHP magic method that we override |
||
819 | * to allow using the following syntax to read a property: |
||
820 | * ```php |
||
821 | * $value = $component->PropertyName; |
||
822 | * $value = $component->jsPropertyName; // return JavaScript literal |
||
823 | * ``` |
||
824 | * and to obtain the event handler list for an event, |
||
825 | * ```php |
||
826 | * $eventHandlerList = $component->EventName; |
||
827 | * ``` |
||
828 | * This will also return the global event handler list when specifing an 'fx' |
||
829 | * event, |
||
830 | * ```php |
||
831 | * $globalEventHandlerList = $component->fxEventName; |
||
832 | * ``` |
||
833 | * When behaviors are enabled, this will return the behavior of a specific |
||
834 | * name, a property of a behavior, or an object 'on' event defined by the behavior. |
||
835 | * @param string $name the property name or the event name |
||
836 | * @throws TInvalidOperationException if the property/event is not defined. |
||
837 | * @return mixed the property value or the event handler list as {@see \Prado\Collections\TWeakCallableCollection} |
||
838 | */ |
||
839 | public function __get($name) |
||
840 | { |
||
841 | if (Prado::method_visible($this, $getter = 'get' . $name)) { |
||
842 | // getting a property |
||
843 | return $this->$getter(); |
||
844 | } elseif (Prado::method_visible($this, $jsgetter = 'getjs' . $name)) { |
||
845 | // getting a javascript property |
||
846 | return (string) $this->$jsgetter(); |
||
847 | } elseif (strncasecmp($name, 'on', 2) === 0 && method_exists($this, $name)) { |
||
848 | // getting an event (handler list) |
||
849 | $name = strtolower($name); |
||
850 | if (!isset($this->_e[$name])) { |
||
851 | $this->_e[$name] = new TWeakCallableCollection(); |
||
852 | } |
||
853 | return $this->_e[$name]; |
||
854 | } elseif (strncasecmp($name, 'fx', 2) === 0) { |
||
855 | // getting a global event (handler list) |
||
856 | $name = strtolower($name); |
||
857 | if (!isset(self::$_ue[$name])) { |
||
858 | self::$_ue[$name] = new TWeakCallableCollection(); |
||
859 | } |
||
860 | return self::$_ue[$name]; |
||
861 | } elseif ($this->getBehaviorsEnabled()) { |
||
862 | // getting a behavior property/event (handler list) |
||
863 | $name = strtolower($name); |
||
864 | if (isset($this->_m[$name])) { |
||
865 | return $this->_m[$name]; |
||
866 | } elseif ($this->_m !== null) { |
||
867 | foreach ($this->_m->toArray() as $behavior) { |
||
868 | if ($behavior->getEnabled() && (property_exists($behavior, $name) || $behavior->canGetProperty($name) || $behavior->hasEvent($name))) { |
||
869 | return $behavior->$name; |
||
870 | } |
||
871 | } |
||
872 | } |
||
873 | } |
||
874 | throw new TInvalidOperationException('component_property_undefined', $this::class, $name); |
||
875 | } |
||
876 | |||
877 | /** |
||
878 | * Sets value of a component property. |
||
879 | * Do not call this method. This is a PHP magic method that we override |
||
880 | * to allow using the following syntax to set a property or attach an event handler. |
||
881 | * ```php |
||
882 | * $this->PropertyName = $value; |
||
883 | * $this->jsPropertyName = $value; // $value will be treated as a JavaScript literal |
||
884 | * $this->EventName = $handler; |
||
885 | * $this->fxEventName = $handler; //global event listener |
||
886 | * $this->EventName = function($sender, $param) {...}; |
||
887 | * ``` |
||
888 | * When behaviors are enabled, this will also set a behaviors properties and events. |
||
889 | * @param string $name the property name or event name |
||
890 | * @param mixed $value the property value or event handler |
||
891 | * @throws TInvalidOperationException If the property is not defined or read-only. |
||
892 | */ |
||
893 | public function __set($name, $value) |
||
894 | { |
||
895 | if (Prado::method_visible($this, $setter = 'set' . $name)) { |
||
896 | if (strncasecmp($name, 'js', 2) === 0 && $value && !($value instanceof TJavaScriptLiteral)) { |
||
897 | $value = new TJavaScriptLiteral($value); |
||
898 | } |
||
899 | return $this->$setter($value); |
||
900 | } elseif (Prado::method_visible($this, $jssetter = 'setjs' . $name)) { |
||
901 | if ($value && !($value instanceof TJavaScriptString)) { |
||
902 | $value = new TJavaScriptString($value); |
||
903 | } |
||
904 | return $this->$jssetter($value); |
||
905 | } elseif ((strncasecmp($name, 'on', 2) === 0 && method_exists($this, $name)) || strncasecmp($name, 'fx', 2) === 0) { |
||
906 | return $this->attachEventHandler($name, $value); |
||
907 | } elseif ($this->_m !== null && $this->_m->getCount() > 0 && $this->getBehaviorsEnabled()) { |
||
908 | $sets = 0; |
||
909 | foreach ($this->_m->toArray() as $behavior) { |
||
910 | if ($behavior->getEnabled() && (property_exists($behavior, $name) || $behavior->canSetProperty($name) || $behavior->hasEvent($name))) { |
||
911 | $behavior->$name = $value; |
||
912 | $sets++; |
||
913 | } |
||
914 | } |
||
915 | if ($sets) { |
||
916 | return $value; |
||
917 | } |
||
918 | } |
||
919 | |||
920 | if (Prado::method_visible($this, 'get' . $name) || Prado::method_visible($this, 'getjs' . $name)) { |
||
921 | throw new TInvalidOperationException('component_property_readonly', $this::class, $name); |
||
922 | } else { |
||
923 | throw new TInvalidOperationException('component_property_undefined', $this::class, $name); |
||
924 | } |
||
925 | } |
||
926 | |||
927 | /** |
||
928 | * Checks if a property value is null, there are no events in the object |
||
929 | * event list or global event list registered under the name, and, if |
||
930 | * behaviors are enabled, |
||
931 | * Do not call this method. This is a PHP magic method that we override |
||
932 | * to allow using isset() to detect if a component property is set or not. |
||
933 | * This also works for global events. When behaviors are enabled, it |
||
934 | * will check for a behavior of the specified name, and also check |
||
935 | * the behavior for events and properties. |
||
936 | * @param string $name the property name or the event name |
||
937 | * @since 3.2.3 |
||
938 | */ |
||
939 | public function __isset($name) |
||
940 | { |
||
941 | if (Prado::method_visible($this, $getter = 'get' . $name)) { |
||
942 | return $this->$getter() !== null; |
||
943 | } elseif (Prado::method_visible($this, $jsgetter = 'getjs' . $name)) { |
||
944 | return $this->$jsgetter() !== null; |
||
945 | } elseif (strncasecmp($name, 'on', 2) === 0 && method_exists($this, $name)) { |
||
946 | $name = strtolower($name); |
||
947 | return isset($this->_e[$name]) && $this->_e[$name]->getCount(); |
||
948 | } elseif (strncasecmp($name, 'fx', 2) === 0) { |
||
949 | $name = strtolower($name); |
||
950 | return isset(self::$_ue[$name]) && self::$_ue[$name]->getCount(); |
||
951 | } elseif ($this->_m !== null && $this->_m->getCount() > 0 && $this->getBehaviorsEnabled()) { |
||
952 | $name = strtolower($name); |
||
953 | if (isset($this->_m[$name])) { |
||
954 | return true; |
||
955 | } |
||
956 | foreach ($this->_m->toArray() as $behavior) { |
||
957 | if ($behavior->getEnabled() && (property_exists($behavior, $name) || $behavior->canGetProperty($name) || $behavior->hasEvent($name))) { |
||
958 | return isset($behavior->$name); |
||
959 | } |
||
960 | } |
||
961 | } |
||
962 | return false; |
||
963 | } |
||
964 | |||
965 | /** |
||
966 | * Sets a component property to be null. Clears the object or global |
||
967 | * events. When enabled, loops through all behaviors and unsets the |
||
968 | * property or event. |
||
969 | * Do not call this method. This is a PHP magic method that we override |
||
970 | * to allow using unset() to set a component property to be null. |
||
971 | * @param string $name the property name or the event name |
||
972 | * @throws TInvalidOperationException if the property is read only. |
||
973 | * @since 3.2.3 |
||
974 | */ |
||
975 | public function __unset($name) |
||
976 | { |
||
977 | if (Prado::method_visible($this, $setter = 'set' . $name)) { |
||
978 | $this->$setter(null); |
||
979 | } elseif (Prado::method_visible($this, $jssetter = 'setjs' . $name)) { |
||
980 | $this->$jssetter(null); |
||
981 | } elseif (strncasecmp($name, 'on', 2) === 0 && method_exists($this, $name)) { |
||
982 | $this->_e[strtolower($name)]->clear(); |
||
983 | } elseif (strncasecmp($name, 'fx', 2) === 0) { |
||
984 | $this->getEventHandlers($name)->remove([$this, $name]); |
||
985 | } elseif ($this->_m !== null && $this->_m->getCount() > 0 && $this->getBehaviorsEnabled()) { |
||
986 | $name = strtolower($name); |
||
987 | if (isset($this->_m[$name])) { |
||
988 | $this->detachBehavior($name); |
||
989 | } else { |
||
990 | $unset = 0; |
||
991 | foreach ($this->_m->toArray() as $behavior) { |
||
992 | if ($behavior->getEnabled()) { |
||
993 | unset($behavior->$name); |
||
994 | $unset++; |
||
995 | } |
||
996 | } |
||
997 | if (!$unset && Prado::method_visible($this, 'get' . $name)) { |
||
998 | throw new TInvalidOperationException('component_property_readonly', $this::class, $name); |
||
999 | } |
||
1000 | } |
||
1001 | } elseif (Prado::method_visible($this, 'get' . $name)) { |
||
1002 | throw new TInvalidOperationException('component_property_readonly', $this::class, $name); |
||
1003 | } |
||
1004 | } |
||
1005 | |||
1006 | /** |
||
1007 | * Determines whether a property is defined. |
||
1008 | * A property is defined if there is a getter or setter method |
||
1009 | * defined in the class. Note, property names are case-insensitive. |
||
1010 | * @param string $name the property name |
||
1011 | * @return bool whether the property is defined |
||
1012 | */ |
||
1013 | public function hasProperty($name) |
||
1014 | { |
||
1015 | return $this->canGetProperty($name) || $this->canSetProperty($name); |
||
1016 | } |
||
1017 | |||
1018 | /** |
||
1019 | * Determines whether a property can be read. |
||
1020 | * A property can be read if the class has a getter method |
||
1021 | * for the property name. Note, property name is case-insensitive. |
||
1022 | * This also checks for getjs. When enabled, it loops through all |
||
1023 | * active behaviors for the get property when undefined by the object. |
||
1024 | * @param string $name the property name |
||
1025 | * @return bool whether the property can be read |
||
1026 | */ |
||
1027 | public function canGetProperty($name) |
||
1028 | { |
||
1029 | if (Prado::method_visible($this, 'get' . $name) || Prado::method_visible($this, 'getjs' . $name)) { |
||
1030 | return true; |
||
1031 | } elseif ($this->_m !== null && $this->getBehaviorsEnabled()) { |
||
1032 | foreach ($this->_m->toArray() as $behavior) { |
||
1033 | if ($behavior->getEnabled() && $behavior->canGetProperty($name)) { |
||
1034 | return true; |
||
1035 | } |
||
1036 | } |
||
1037 | } |
||
1038 | return false; |
||
1039 | } |
||
1040 | |||
1041 | /** |
||
1042 | * Determines whether a property can be set. |
||
1043 | * A property can be written if the class has a setter method |
||
1044 | * for the property name. Note, property name is case-insensitive. |
||
1045 | * This also checks for setjs. When enabled, it loops through all |
||
1046 | * active behaviors for the set property when undefined by the object. |
||
1047 | * @param string $name the property name |
||
1048 | * @return bool whether the property can be written |
||
1049 | */ |
||
1050 | public function canSetProperty($name) |
||
1051 | { |
||
1052 | if (Prado::method_visible($this, 'set' . $name) || Prado::method_visible($this, 'setjs' . $name)) { |
||
1053 | return true; |
||
1054 | } elseif ($this->_m !== null && $this->getBehaviorsEnabled()) { |
||
1055 | foreach ($this->_m->toArray() as $behavior) { |
||
1056 | if ($behavior->getEnabled() && $behavior->canSetProperty($name)) { |
||
1057 | return true; |
||
1058 | } |
||
1059 | } |
||
1060 | } |
||
1061 | return false; |
||
1062 | } |
||
1063 | |||
1064 | /** |
||
1065 | * Evaluates a property path. |
||
1066 | * A property path is a sequence of property names concatenated by '.' character. |
||
1067 | * For example, 'Parent.Page' refers to the 'Page' property of the component's |
||
1068 | * 'Parent' property value (which should be a component also). |
||
1069 | * When a property is not defined by an object, this also loops through all |
||
1070 | * active behaviors of the object. |
||
1071 | * @param string $path property path |
||
1072 | * @return mixed the property path value |
||
1073 | */ |
||
1074 | public function getSubProperty($path) |
||
1075 | { |
||
1076 | $object = $this; |
||
1077 | foreach (explode('.', $path) as $property) { |
||
1078 | $object = $object->$property; |
||
1079 | } |
||
1080 | return $object; |
||
1081 | } |
||
1082 | |||
1083 | /** |
||
1084 | * Sets a value to a property path. |
||
1085 | * A property path is a sequence of property names concatenated by '.' character. |
||
1086 | * For example, 'Parent.Page' refers to the 'Page' property of the component's |
||
1087 | * 'Parent' property value (which should be a component also). |
||
1088 | * When a property is not defined by an object, this also loops through all |
||
1089 | * active behaviors of the object. |
||
1090 | * @param string $path property path |
||
1091 | * @param mixed $value the property path value |
||
1092 | */ |
||
1093 | public function setSubProperty($path, $value) |
||
1094 | { |
||
1095 | $object = $this; |
||
1096 | if (($pos = strrpos($path, '.')) === false) { |
||
1097 | $property = $path; |
||
1098 | } else { |
||
1099 | $object = $this->getSubProperty(substr($path, 0, $pos)); |
||
1100 | $property = substr($path, $pos + 1); |
||
1101 | } |
||
1102 | $object->$property = $value; |
||
1103 | } |
||
1104 | |||
1105 | /** |
||
1106 | * Calls a method on a component's behaviors. When the method is a |
||
1107 | * dynamic event, it is raised with all the behaviors. When a class implements |
||
1108 | * a dynamic event (eg. for patching), the class can customize raising the |
||
1109 | * dynamic event with the classes behaviors using this method. |
||
1110 | * Dynamic [dy] and global [fx] events call {@see __dycall} when $this |
||
1111 | * implements IDynamicMethods. Finally, this catches all unexecuted |
||
1112 | * Dynamic [dy] and global [fx] events and returns the first $args parameter; |
||
1113 | * acting as a passthrough (filter) of the first $args parameter. In dy/fx methods, |
||
1114 | * there can be no $args parameters, the first parameter used as a pass through |
||
1115 | * filter, or act as a return variable with the first $args parameter being |
||
1116 | * the default return value. |
||
1117 | * @param string $method The method being called or dynamic/global event being raised. |
||
1118 | * @param mixed &$return The return value. |
||
1119 | * @param array $args The arguments to the method being called. |
||
1120 | * @return bool Was the method handled. |
||
1121 | * @since 4.3.0 |
||
1122 | */ |
||
1123 | public function callBehaviorsMethod($method, &$return, ...$args): bool |
||
1124 | { |
||
1125 | if ($this->_m !== null && $this->getBehaviorsEnabled()) { |
||
1126 | if (strncasecmp($method, 'dy', 2) === 0) { |
||
1127 | if ($callchain = $this->getCallChain($method, ...$args)) { |
||
1128 | $return = $callchain->call(...$args); |
||
1129 | return true; |
||
1130 | } |
||
1131 | } else { |
||
1132 | foreach ($this->_m->toArray() as $behavior) { |
||
1133 | if ($behavior->getEnabled() && Prado::method_visible($behavior, $method)) { |
||
1134 | if ($behavior instanceof IClassBehavior) { |
||
1135 | array_unshift($args, $this); |
||
1136 | } |
||
1137 | $return = $behavior->$method(...$args); |
||
1138 | return true; |
||
1139 | } |
||
1140 | } |
||
1141 | } |
||
1142 | } |
||
1143 | if (strncasecmp($method, 'dy', 2) === 0 || strncasecmp($method, 'fx', 2) === 0) { |
||
1144 | if ($this instanceof IDynamicMethods) { |
||
1145 | $return = $this->__dycall($method, $args); |
||
1146 | return true; |
||
1147 | } |
||
1148 | $return = $args[0] ?? null; |
||
1149 | return true; |
||
1150 | } |
||
1151 | return false; |
||
1152 | } |
||
1153 | |||
1154 | /** |
||
1155 | * This gets the chain of methods implemented by attached and enabled behaviors. |
||
1156 | * This method disregards the {behaviorsEnabled |
||
1157 | * @param string $method The name of the behaviors method being chained. |
||
1158 | * @param array $args The arguments to the behaviors method being chained. |
||
1159 | * @return ?TCallChain The chain of methods implemented by behaviors or null when |
||
1160 | * there are no methods to call. |
||
1161 | * @since 4.3.0 |
||
1162 | */ |
||
1163 | protected function getCallChain($method, ...$args): ?TCallChain |
||
1164 | { |
||
1165 | $classArgs = $callchain = null; |
||
1166 | foreach ($this->_m->toArray() as $behavior) { |
||
1167 | if ($behavior->getEnabled() && (Prado::method_visible($behavior, $method) || ($behavior instanceof IDynamicMethods))) { |
||
1168 | if ($classArgs === null) { |
||
1169 | $classArgs = $args; |
||
1170 | array_unshift($classArgs, $this); |
||
1171 | } |
||
1172 | if (!$callchain) { |
||
1173 | $callchain = new TCallChain($method); |
||
1174 | } |
||
1175 | $callchain->addCall([$behavior, $method], ($behavior instanceof IClassBehavior) ? $classArgs : $args); |
||
1176 | } |
||
1177 | } |
||
1178 | return $callchain; |
||
1179 | } |
||
1180 | |||
1181 | /** |
||
1182 | * Determines whether a method is defined. When behaviors are enabled, this |
||
1183 | * will loop through all enabled behaviors checking for the method as well. |
||
1184 | * Nested behaviors within behaviors are not supported but the nested behavior can |
||
1185 | * affect the primary behavior like any behavior affects their owner. |
||
1186 | * Note, method name are case-insensitive. |
||
1187 | * @param string $name the method name |
||
1188 | * @return bool |
||
1189 | * @since 4.2.2 |
||
1190 | */ |
||
1191 | public function hasMethod($name) |
||
1192 | { |
||
1193 | if (Prado::method_visible($this, $name) || strncasecmp($name, 'dy', 2) === 0) { |
||
1194 | return true; |
||
1195 | } elseif ($this->_m !== null && $this->getBehaviorsEnabled()) { |
||
1196 | foreach ($this->_m->toArray() as $behavior) { |
||
1197 | //Prado::method_visible($behavior, $name) rather than $behavior->hasMethod($name) b/c only one layer is supported, @4.2.2 |
||
1198 | if ($behavior->getEnabled() && Prado::method_visible($behavior, $name)) { |
||
1199 | return true; |
||
1200 | } |
||
1201 | } |
||
1202 | } |
||
1203 | return false; |
||
1204 | } |
||
1205 | |||
1206 | /** |
||
1207 | * Determines whether an event is defined. |
||
1208 | * An event is defined if the class has a method whose name is the event name |
||
1209 | * prefixed with 'on', 'fx', or 'dy'. |
||
1210 | * Every object responds to every 'fx' and 'dy' event as they are in a universally |
||
1211 | * accepted event space. 'on' event must be declared by the object. |
||
1212 | * When enabled, this will loop through all active behaviors for 'on' events |
||
1213 | * defined by the behavior. |
||
1214 | * Note, event name is case-insensitive. |
||
1215 | * @param string $name the event name |
||
1216 | * @return bool |
||
1217 | */ |
||
1218 | public function hasEvent($name) |
||
1219 | { |
||
1220 | if ((strncasecmp($name, 'on', 2) === 0 && method_exists($this, $name)) || strncasecmp($name, 'fx', 2) === 0 || strncasecmp($name, 'dy', 2) === 0) { |
||
1221 | return true; |
||
1222 | } elseif ($this->_m !== null && $this->getBehaviorsEnabled()) { |
||
1223 | foreach ($this->_m->toArray() as $behavior) { |
||
1224 | if ($behavior->getEnabled() && $behavior->hasEvent($name)) { |
||
1225 | return true; |
||
1226 | } |
||
1227 | } |
||
1228 | } |
||
1229 | return false; |
||
1230 | } |
||
1231 | |||
1232 | /** |
||
1233 | * Checks if an event has any handlers. This function also checks through all |
||
1234 | * the behaviors for 'on' events when behaviors are enabled. |
||
1235 | * 'dy' dynamic events are not handled by this function. |
||
1236 | * @param string $name the event name |
||
1237 | * @return bool whether an event has been attached one or several handlers |
||
1238 | */ |
||
1239 | public function hasEventHandler($name) |
||
1240 | { |
||
1241 | $name = strtolower($name); |
||
1242 | if (strncasecmp($name, 'fx', 2) === 0) { |
||
1243 | return isset(self::$_ue[$name]) && self::$_ue[$name]->getCount() > 0; |
||
1244 | } else { |
||
1245 | if (isset($this->_e[$name]) && $this->_e[$name]->getCount() > 0) { |
||
1246 | return true; |
||
1247 | } elseif ($this->_m !== null && $this->getBehaviorsEnabled()) { |
||
1248 | foreach ($this->_m->toArray() as $behavior) { |
||
1249 | if ($behavior->getEnabled() && $behavior->hasEventHandler($name)) { |
||
1250 | return true; |
||
1251 | } |
||
1252 | } |
||
1253 | } |
||
1254 | } |
||
1255 | return false; |
||
1256 | } |
||
1257 | |||
1258 | /** |
||
1259 | * Returns the list of attached event handlers for an 'on' or 'fx' event. This function also |
||
1260 | * checks through all the behaviors for 'on' event lists when behaviors are enabled. |
||
1261 | * @param mixed $name |
||
1262 | * @throws TInvalidOperationException if the event is not defined |
||
1263 | * @return TWeakCallableCollection list of attached event handlers for an event |
||
1264 | */ |
||
1265 | public function getEventHandlers($name) |
||
1266 | { |
||
1267 | if (strncasecmp($name, 'on', 2) === 0 && method_exists($this, $name)) { |
||
1268 | $name = strtolower($name); |
||
1269 | if (!isset($this->_e[$name])) { |
||
1270 | $this->_e[$name] = new TWeakCallableCollection(); |
||
1271 | } |
||
1272 | return $this->_e[$name]; |
||
1273 | } elseif (strncasecmp($name, 'fx', 2) === 0) { |
||
1274 | $name = strtolower($name); |
||
1275 | if (!isset(self::$_ue[$name])) { |
||
1276 | self::$_ue[$name] = new TWeakCallableCollection(); |
||
1277 | } |
||
1278 | return self::$_ue[$name]; |
||
1279 | } elseif ($this->_m !== null && $this->getBehaviorsEnabled()) { |
||
1280 | foreach ($this->_m->toArray() as $behavior) { |
||
1281 | if ($behavior->getEnabled() && $behavior->hasEvent($name)) { |
||
1282 | return $behavior->getEventHandlers($name); |
||
1283 | } |
||
1284 | } |
||
1285 | } |
||
1286 | throw new TInvalidOperationException('component_event_undefined', $this::class, $name); |
||
1287 | } |
||
1288 | |||
1289 | /** |
||
1290 | * Attaches an event handler to an event. |
||
1291 | * |
||
1292 | * The handler must be a valid PHP callback, i.e., a string referring to |
||
1293 | * a global function name, or an array containing two elements with |
||
1294 | * the first element being an object and the second element a method name |
||
1295 | * of the object. In Prado, you can also use method path to refer to |
||
1296 | * an event handler. For example, array($object,'Parent.buttonClicked') |
||
1297 | * uses a method path that refers to the method $object->Parent->buttonClicked(...). |
||
1298 | * |
||
1299 | * The event handler must be of the following signature, |
||
1300 | * ```php |
||
1301 | * function handlerName($sender, $param) {} |
||
1302 | * function handlerName($sender, $param, $name) {} |
||
1303 | * ``` |
||
1304 | * where $sender represents the object that raises the event, |
||
1305 | * and $param is the event parameter. $name refers to the event name |
||
1306 | * being handled. |
||
1307 | * |
||
1308 | * This is a convenient method to add an event handler. |
||
1309 | * It is equivalent to {@see getEventHandlers}($name)->add($handler). |
||
1310 | * For complete management of event handlers, use {@see getEventHandlers} |
||
1311 | * to get the event handler list first, and then do various |
||
1312 | * {@see \Prado\Collections\TWeakCallableCollection} operations to append, insert or remove |
||
1313 | * event handlers. You may also do these operations like |
||
1314 | * getting and setting properties, e.g., |
||
1315 | * ```php |
||
1316 | * $component->OnClick[] = array($object,'buttonClicked'); |
||
1317 | * $component->OnClick->insertAt(0,array($object,'buttonClicked')); |
||
1318 | * $component->OnClick[] = function ($sender, $param) { ... }; |
||
1319 | * ``` |
||
1320 | * which are equivalent to the following |
||
1321 | * ```php |
||
1322 | * $component->getEventHandlers('OnClick')->add(array($object,'buttonClicked')); |
||
1323 | * $component->getEventHandlers('OnClick')->insertAt(0,array($object,'buttonClicked')); |
||
1324 | * ``` |
||
1325 | * |
||
1326 | * Due to the nature of {@see getEventHandlers}, any active behaviors defining |
||
1327 | * new 'on' events, this method will pass through to the behavior transparently. |
||
1328 | * |
||
1329 | * @param string $name the event name |
||
1330 | * @param callable $handler the event handler |
||
1331 | * @param null|numeric $priority the priority of the handler, defaults to null which translates into the |
||
1332 | * default priority of 10.0 within {@see \Prado\Collections\TWeakCallableCollection} |
||
1333 | * @throws TInvalidOperationException if the event does not exist |
||
1334 | */ |
||
1335 | public function attachEventHandler($name, $handler, $priority = null) |
||
1336 | { |
||
1337 | $this->getEventHandlers($name)->add($handler, $priority); |
||
1338 | } |
||
1339 | |||
1340 | /** |
||
1341 | * Detaches an existing event handler. |
||
1342 | * This method is the opposite of {@see attachEventHandler}. It will detach |
||
1343 | * any 'on' events defined by an objects active behaviors as well. |
||
1344 | * @param string $name event name |
||
1345 | * @param callable $handler the event handler to be removed |
||
1346 | * @param null|false|numeric $priority the priority of the handler, defaults to false which translates |
||
1347 | * to an item of any priority within {@see \Prado\Collections\TWeakCallableCollection}; null means the default priority |
||
1348 | * @return bool if the removal is successful |
||
1349 | */ |
||
1350 | public function detachEventHandler($name, $handler, $priority = false) |
||
1351 | { |
||
1352 | if ($this->hasEventHandler($name)) { |
||
1353 | try { |
||
1354 | $this->getEventHandlers($name)->remove($handler, $priority); |
||
1355 | return true; |
||
1356 | } catch (\Exception $e) { |
||
1357 | } |
||
1358 | } |
||
1359 | return false; |
||
1360 | } |
||
1361 | |||
1362 | /** |
||
1363 | * Raises an event. This raises both inter-object 'on' events and global 'fx' events. |
||
1364 | * This method represents the happening of an event and will |
||
1365 | * invoke all attached event handlers for the event in {@see \Prado\Collections\TWeakCallableCollection} order. |
||
1366 | * This method does not handle intra-object/behavior dynamic 'dy' events. |
||
1367 | * |
||
1368 | * There are ways to handle event responses. By default {@see EVENT_RESULT_FILTER}, |
||
1369 | * all event responses are stored in an array, filtered for null responses, and returned. |
||
1370 | * If {@see EVENT_RESULT_ALL} is specified, all returned results will be stored along |
||
1371 | * with the sender and param in an array |
||
1372 | * ```php |
||
1373 | * $result[] = array('sender'=>$sender,'param'=>$param,'response'=>$response); |
||
1374 | * ``` |
||
1375 | * |
||
1376 | * If {@see EVENT_RESULT_FEED_FORWARD} is specified, then each handler result is then |
||
1377 | * fed forward as the parameters for the next event. This allows for events to filter data |
||
1378 | * directly by affecting the event parameters |
||
1379 | * |
||
1380 | * If a callable function is set in the response type or the post function filter is specified then the |
||
1381 | * result of each called event handler is post processed by the callable function. Used in |
||
1382 | * combination with {@see EVENT_RESULT_FEED_FORWARD}, any event (and its result) can be chained. |
||
1383 | * |
||
1384 | * When raising a global 'fx' event, registered handlers in the global event list for |
||
1385 | * {@see GLOBAL_RAISE_EVENT_LISTENER} are always added into the set of event handlers. In this way, |
||
1386 | * these global events are always raised for every global 'fx' event. The registered handlers for global |
||
1387 | * raiseEvent events have priorities. Any registered global raiseEvent event handlers with a priority less than zero |
||
1388 | * are added before the main event handlers being raised and any registered global raiseEvent event handlers |
||
1389 | * with a priority equal or greater than zero are added after the main event handlers being raised. In this way |
||
1390 | * all {@see GLOBAL_RAISE_EVENT_LISTENER} handlers are always called for every raised 'fx' event. |
||
1391 | * |
||
1392 | * Behaviors may implement the following functions with TBehaviors: |
||
1393 | * ```php |
||
1394 | * public function dyPreRaiseEvent($name, $sender, $param, $responsetype, $postfunction, TCallChain $chain) { |
||
1395 | * .... // Your logic |
||
1396 | * return $chain->dyPreRaiseEvent($name, $sender, $param, $responsetype, $postfunction); //eg, the event name may be filtered/changed |
||
1397 | * } |
||
1398 | * public function dyIntraRaiseEventTestHandler($handler, $sender, $param, $name, TCallChain $chain) { |
||
1399 | * .... // Your logic |
||
1400 | * return $chain->dyIntraRaiseEventTestHandler($handler, $sender, $param, $name); //should this particular handler be executed? true/false |
||
1401 | * } |
||
1402 | * public function dyIntraRaiseEventPostHandler($name, $sender, $param, $handler, $response, TCallChain $chain) { |
||
1403 | * .... // Your logic |
||
1404 | * return $chain->dyIntraRaiseEventPostHandler($name, $sender, $param, $handler, $response); //contains the per handler response |
||
1405 | * } |
||
1406 | * public function dyPostRaiseEvent($responses, $name, $sender, $param, $responsetype, $postfunction, TCallChain $chain) { |
||
1407 | * .... // Your logic |
||
1408 | * return $chain->dyPostRaiseEvent($responses, $name, $sender, $param, $responsetype, $postfunction); |
||
1409 | * } |
||
1410 | * ``` |
||
1411 | * to be executed when raiseEvent is called. The 'intra' dynamic events are called per handler in |
||
1412 | * the handler loop. TClassBehaviors prepend the object being raised. |
||
1413 | * |
||
1414 | * dyPreRaiseEvent has the effect of being able to change the event being raised. This intra |
||
1415 | * object/behavior event returns the name of the desired event to be raised. It will pass through |
||
1416 | * if no dynamic event is specified, or if the original event name is returned. |
||
1417 | * dyIntraRaiseEventTestHandler returns true or false as to whether a specific handler should be |
||
1418 | * called for a specific raised event (and associated event arguments) |
||
1419 | * dyIntraRaiseEventPostHandler does not return anything. This allows behaviors to access the results |
||
1420 | * of an event handler in the per handler loop. |
||
1421 | * dyPostRaiseEvent returns the responses. This allows for any post processing of the event |
||
1422 | * results from the sum of all event handlers |
||
1423 | * |
||
1424 | * When handling a catch-all {@see __dycall}, the method name is the name of the event |
||
1425 | * and the parameters are the sender, the param, and then the name of the event. |
||
1426 | * |
||
1427 | * In the rare circumstance that the event handlers need to be raised in reverse order, then |
||
1428 | * specifying {@see \Prado\TEventResults::EVENT_REVERSE} can be used to reverse the order of the |
||
1429 | * handlers. |
||
1430 | * |
||
1431 | * @param string $name the event name |
||
1432 | * @param mixed $sender the event sender object |
||
1433 | * @param \Prado\TEventParameter $param the event parameter |
||
1434 | * @param null|numeric $responsetype how the results of the event are tabulated. default: {@see EVENT_RESULT_FILTER} The default filters out |
||
1435 | * null responses. optional |
||
1436 | * @param null|callable $postfunction any per handler filtering of the response result needed is passed through |
||
1437 | * this if not null. default: null. optional |
||
1438 | * @throws TInvalidOperationException if the event is undefined |
||
1439 | * @throws TInvalidDataValueException If an event handler is invalid |
||
1440 | * @return mixed the results of the event |
||
1441 | */ |
||
1442 | public function raiseEvent($name, $sender, $param, $responsetype = null, $postfunction = null) |
||
1443 | { |
||
1444 | $p = $param; |
||
1445 | if (is_callable($responsetype)) { |
||
1446 | $postfunction = $responsetype; |
||
1447 | $responsetype = null; |
||
1448 | } |
||
1449 | |||
1450 | if ($responsetype === null) { |
||
1451 | $responsetype = TEventResults::EVENT_RESULT_FILTER; |
||
1452 | } |
||
1453 | |||
1454 | $name = strtolower($name); |
||
1455 | $responses = []; |
||
1456 | |||
1457 | if ($param instanceof IEventParameter) { |
||
1458 | $param->setEventName($name); |
||
1459 | } |
||
1460 | |||
1461 | $this->callBehaviorsMethod('dyPreRaiseEvent', $name, $name, $sender, $param, $responsetype, $postfunction); |
||
1462 | |||
1463 | if ($this->hasEventHandler($name) || $this->hasEventHandler(TComponent::GLOBAL_RAISE_EVENT_LISTENER)) { |
||
1464 | $handlers = $this->getEventHandlers($name); |
||
1465 | $handlerArray = $handlers->toArray(); |
||
1466 | if (strncasecmp($name, 'fx', 2) === 0 && $this->hasEventHandler(TComponent::GLOBAL_RAISE_EVENT_LISTENER)) { |
||
1467 | $globalhandlers = $this->getEventHandlers(TComponent::GLOBAL_RAISE_EVENT_LISTENER); |
||
1468 | $handlerArray = array_merge($globalhandlers->toArrayBelowPriority(0), $handlerArray, $globalhandlers->toArrayAbovePriority(0)); |
||
1469 | } |
||
1470 | $response = null; |
||
1471 | if ($responsetype & TEventResults::EVENT_REVERSE) { |
||
1472 | $handlerArray = array_reverse($handlerArray); |
||
1473 | } |
||
1474 | foreach ($handlerArray as $handler) { |
||
1475 | $this->callBehaviorsMethod('dyIntraRaiseEventTestHandler', $return, $handler, $sender, $param, $name); |
||
1476 | if ($return === false) { |
||
1477 | continue; |
||
1478 | } |
||
1479 | |||
1480 | if (is_string($handler)) { |
||
1481 | if (($pos = strrpos($handler, '.')) !== false) { |
||
1482 | $object = $this->getSubProperty(substr($handler, 0, $pos)); |
||
1483 | $method = substr($handler, $pos + 1); |
||
1484 | if (Prado::method_visible($object, $method) || strncasecmp($method, 'dy', 2) === 0 || strncasecmp($method, 'fx', 2) === 0) { |
||
1485 | if ($method == '__dycall') { |
||
1486 | $response = $object->__dycall($name, [$sender, $param]); |
||
1487 | } else { |
||
1488 | $response = $object->$method($sender, $param); |
||
1489 | } |
||
1490 | } else { |
||
1491 | throw new TInvalidDataValueException('component_eventhandler_invalid', $this::class, $name, $handler); |
||
1492 | } |
||
1493 | } else { |
||
1494 | $response = call_user_func($handler, $sender, $param); |
||
1495 | } |
||
1496 | } elseif (is_callable($handler, true)) { |
||
1497 | if (is_object($handler) || is_string($handler[0])) { |
||
1498 | $response = call_user_func($handler, $sender, $param); |
||
1499 | } else { |
||
1500 | [$object, $method] = $handler; |
||
1501 | if (($pos = strrpos($method, '.')) !== false) { |
||
1502 | $object = $object->getSubProperty(substr($method, 0, $pos)); |
||
1503 | $method = substr($method, $pos + 1); |
||
1504 | } |
||
1505 | if (Prado::method_visible($object, $method) || strncasecmp($method, 'dy', 2) === 0 || strncasecmp($method, 'fx', 2) === 0) { |
||
1506 | if ($method == '__dycall') { |
||
1507 | $response = $object->__dycall($name, [$sender, $param]); |
||
1508 | } else { |
||
1509 | $response = $object->$method($sender, $param); |
||
1510 | } |
||
1511 | } else { |
||
1512 | throw new TInvalidDataValueException('component_eventhandler_invalid', $this::class, $name, $handler[1]); |
||
1513 | } |
||
1514 | } |
||
1515 | } else { |
||
1516 | throw new TInvalidDataValueException('component_eventhandler_invalid', $this::class, $name, gettype($handler)); |
||
1517 | } |
||
1518 | |||
1519 | $this->callBehaviorsMethod('dyIntraRaiseEventPostHandler', $return, $name, $sender, $param, $handler, $response); |
||
1520 | |||
1521 | if ($postfunction) { |
||
1522 | $response = call_user_func_array($postfunction, [$sender, $param, $this, $response]); |
||
1523 | } |
||
1524 | |||
1525 | if ($responsetype & TEventResults::EVENT_RESULT_ALL) { |
||
1526 | $responses[] = ['sender' => $sender, 'param' => $param, 'response' => $response]; |
||
1527 | } else { |
||
1528 | $responses[] = $response; |
||
1529 | } |
||
1530 | |||
1531 | if ($response !== null && ($responsetype & TEventResults::EVENT_RESULT_FEED_FORWARD)) { |
||
1532 | $param = $response; |
||
1533 | } |
||
1534 | } |
||
1535 | } elseif (strncasecmp($name, 'on', 2) === 0 && !$this->hasEvent($name)) { |
||
1536 | throw new TInvalidOperationException('component_event_undefined', $this::class, $name); |
||
1537 | } |
||
1538 | |||
1539 | if ($responsetype & TEventResults::EVENT_RESULT_FILTER) { |
||
1540 | $responses = array_filter($responses); |
||
1541 | } |
||
1542 | |||
1543 | $this->callBehaviorsMethod('dyPostRaiseEvent', $responses, $responses, $name, $sender, $param, $responsetype, $postfunction); |
||
1544 | |||
1545 | return $responses; |
||
1546 | } |
||
1547 | |||
1548 | /** |
||
1549 | * Evaluates a PHP expression in the context of this control. |
||
1550 | * |
||
1551 | * Behaviors may implement the function: |
||
1552 | * ```php |
||
1553 | * public function dyEvaluateExpressionFilter($expression, TCallChain $chain) { |
||
1554 | * return $chain->dyEvaluateExpressionFilter(str_replace('foo', 'bar', $expression)); //example |
||
1555 | * } |
||
1556 | * ``` |
||
1557 | * to be executed when evaluateExpression is called. All attached behaviors are notified through |
||
1558 | * dyEvaluateExpressionFilter. The chaining is important in this function due to the filtering |
||
1559 | * pass-through effect. |
||
1560 | * |
||
1561 | * @param string $expression PHP expression |
||
1562 | * @throws TInvalidOperationException if the expression is invalid |
||
1563 | * @return mixed the expression result |
||
1564 | */ |
||
1565 | public function evaluateExpression($expression) |
||
1566 | { |
||
1567 | $this->callBehaviorsMethod('dyEvaluateExpressionFilter', $expression, $expression); |
||
1568 | try { |
||
1569 | return eval("return $expression;"); |
||
1570 | } catch (\Throwable $e) { |
||
1571 | throw new TInvalidOperationException('component_expression_invalid', $this::class, $expression, $e->getMessage()); |
||
1572 | } |
||
1573 | } |
||
1574 | |||
1575 | /** |
||
1576 | * Evaluates a list of PHP statements. |
||
1577 | * |
||
1578 | * Behaviors may implement the function: |
||
1579 | * ```php |
||
1580 | * public function dyEvaluateStatementsFilter($statements, TCallChain $chain) { |
||
1581 | * return $chain->dyEvaluateStatementsFilter(str_replace('foo', 'bar', $statements)); //example |
||
1582 | * } |
||
1583 | * ``` |
||
1584 | * to be executed when evaluateStatements is called. All attached behaviors are notified through |
||
1585 | * dyEvaluateStatementsFilter. The chaining is important in this function due to the filtering |
||
1586 | * pass-through effect. |
||
1587 | * |
||
1588 | * @param string $statements PHP statements |
||
1589 | * @throws TInvalidOperationException if the statements are invalid |
||
1590 | * @return string content echoed or printed by the PHP statements |
||
1591 | */ |
||
1592 | public function evaluateStatements($statements) |
||
1593 | { |
||
1594 | $this->callBehaviorsMethod('dyEvaluateStatementsFilter', $statements, $statements); |
||
1595 | try { |
||
1596 | ob_start(); |
||
1597 | if (eval($statements) === false) { |
||
1598 | throw new \Exception(''); |
||
1599 | } |
||
1600 | $content = ob_get_contents(); |
||
1601 | ob_end_clean(); |
||
1602 | return $content; |
||
1603 | } catch (\Exception $e) { |
||
1604 | throw new TInvalidOperationException('component_statements_invalid', $this::class, $statements, $e->getMessage()); |
||
1605 | } |
||
1606 | } |
||
1607 | |||
1608 | /** |
||
1609 | * This method is invoked after the component is instantiated by a template. |
||
1610 | * When this method is invoked, the component's properties have been initialized. |
||
1611 | * The default implementation of this method will invoke |
||
1612 | * the potential parent component's {@see addParsedObject}. |
||
1613 | * This method can be overridden. |
||
1614 | * |
||
1615 | * Behaviors may implement the function: |
||
1616 | * ```php |
||
1617 | * public function dyCreatedOnTemplate($parent, TCallChain $chain) { |
||
1618 | * return $chain->dyCreatedOnTemplate($parent); //example |
||
1619 | * } |
||
1620 | * ``` |
||
1621 | * to be executed when createdOnTemplate is called. All attached behaviors are notified through |
||
1622 | * dyCreatedOnTemplate. |
||
1623 | * |
||
1624 | * @param \Prado\TComponent $parent potential parent of this control |
||
1625 | * @see addParsedObject |
||
1626 | */ |
||
1627 | public function createdOnTemplate($parent) |
||
1628 | { |
||
1629 | $this->callBehaviorsMethod('dyCreatedOnTemplate', $parent, $parent); |
||
1630 | $parent->addParsedObject($this); |
||
1631 | } |
||
1632 | |||
1633 | /** |
||
1634 | * Processes an object that is created during parsing template. |
||
1635 | * The object can be either a component or a static text string. |
||
1636 | * This method can be overridden to customize the handling of newly created objects in template. |
||
1637 | * Only framework developers and control developers should use this method. |
||
1638 | * |
||
1639 | * Behaviors may implement the function: |
||
1640 | * ```php |
||
1641 | * public function dyAddParsedObject($object, TCallChain $chain) { |
||
1642 | * return $chain-> dyAddParsedObject($object); |
||
1643 | * } |
||
1644 | * ``` |
||
1645 | * to be executed when addParsedObject is called. All attached behaviors are notified through |
||
1646 | * dyAddParsedObject. |
||
1647 | * |
||
1648 | * @param \Prado\TComponent|string $object text string or component parsed and instantiated in template |
||
1649 | * @see createdOnTemplate |
||
1650 | */ |
||
1651 | public function addParsedObject($object) |
||
1652 | { |
||
1653 | $this->callBehaviorsMethod('dyAddParsedObject', $return, $object); |
||
1654 | } |
||
1655 | |||
1656 | /** |
||
1657 | *This is the method registered for all instanced objects should a class behavior be added after |
||
1658 | * the class is instanced. Only when the class to which the behavior is being added is in this |
||
1659 | * object's class hierarchy, via {@see getClassHierarchy}, is the behavior added to this instance. |
||
1660 | * @param mixed $sender the application |
||
1661 | * @param TClassBehaviorEventParameter $param |
||
1662 | * @since 3.2.3 |
||
1663 | */ |
||
1664 | public function fxAttachClassBehavior($sender, $param) |
||
1665 | { |
||
1666 | if ($this->isa($param->getClass())) { |
||
1667 | if (($behavior = $param->getBehavior()) instanceof IBehavior) { |
||
1668 | $behavior = clone $behavior; |
||
1669 | } |
||
1670 | return $this->attachBehavior($param->getName(), $behavior, $param->getPriority()); |
||
1671 | } |
||
1672 | } |
||
1673 | |||
1674 | /** |
||
1675 | * This is the method registered for all instanced objects should a class behavior be removed after |
||
1676 | * the class is instanced. Only when the class to which the behavior is being added is in this |
||
1677 | * object's class hierarchy, via {@see getClassHierarchy}, is the behavior removed from this instance. |
||
1678 | * @param mixed $sender the application |
||
1679 | * @param TClassBehaviorEventParameter $param |
||
1680 | * @since 3.2.3 |
||
1681 | */ |
||
1682 | public function fxDetachClassBehavior($sender, $param) |
||
1683 | { |
||
1684 | if ($this->isa($param->getClass())) { |
||
1685 | return $this->detachBehavior($param->getName(), $param->getPriority()); |
||
1686 | } |
||
1687 | } |
||
1688 | |||
1689 | /** |
||
1690 | * instanceBehavior is an internal method that takes a Behavior Object, a class name, or array of |
||
1691 | * ['class' => 'MyBehavior', 'property1' => 'Value1'...] and creates a Behavior in return. eg. |
||
1692 | * ```php |
||
1693 | * $b = $this->instanceBehavior('MyBehavior'); |
||
1694 | * $b = $this->instanceBehavior(['class' => 'MyBehavior', 'property1' => 'Value1']); |
||
1695 | * $b = $this->instanceBehavior(new MyBehavior); |
||
1696 | * ``` |
||
1697 | * If the behavior is an array, the key IBaseBehavior::CONFIG_KEY is stripped and used to initialize |
||
1698 | * the behavior. |
||
1699 | * |
||
1700 | * @param array|IBaseBehavior|string $behavior string, Behavior, or array of ['class' => 'MyBehavior', 'property1' => 'Value1' ...]. |
||
1701 | * @throws TInvalidDataTypeException if the behavior is not an {@see \Prado\Util\IBaseBehavior} |
||
1702 | * @return IBaseBehavior&TComponent an instance of $behavior or $behavior itself |
||
1703 | * @since 4.2.0 |
||
1704 | */ |
||
1705 | protected static function instanceBehavior($behavior) |
||
1706 | { |
||
1707 | $config = null; |
||
1708 | $isArray = false; |
||
1709 | $init = false; |
||
1710 | if (is_string($behavior) || (($isArray = is_array($behavior)) && isset($behavior['class']))) { |
||
1711 | if ($isArray && array_key_exists(IBaseBehavior::CONFIG_KEY, $behavior)) { |
||
1712 | $config = $behavior[IBaseBehavior::CONFIG_KEY]; |
||
1713 | unset($behavior[IBaseBehavior::CONFIG_KEY]); |
||
1714 | } |
||
1715 | $behavior = Prado::createComponent($behavior); |
||
1716 | $init = true; |
||
1717 | } |
||
1718 | if (!($behavior instanceof IBaseBehavior)) { |
||
1719 | throw new TInvalidDataTypeException('component_not_a_behavior', $behavior::class); |
||
1720 | } |
||
1721 | if ($init) { |
||
1722 | $behavior->init($config); |
||
1723 | } |
||
1724 | return $behavior; |
||
1725 | } |
||
1726 | |||
1727 | |||
1728 | /** |
||
1729 | * This will add a class behavior to all classes instanced (that are listening) and future newly instanced objects. |
||
1730 | * This registers the behavior for future instances and pushes the changes to all the instances that are listening as well. |
||
1731 | * The universal class behaviors are stored in an inverted stack with the latest class behavior being at the first position in the array. |
||
1732 | * This is done so class behaviors are added last first. |
||
1733 | * @param string $name name the key of the class behavior |
||
1734 | * @param object|string $behavior class behavior or name of the object behavior per instance |
||
1735 | * @param null|array|IBaseBehavior|string $class string of class or class on which to attach this behavior. Defaults to null which will error |
||
1736 | * but more important, if this is on PHP 5.3 it will use Late Static Binding to derive the class |
||
1737 | * it should extend. |
||
1738 | * ```php |
||
1739 | * TPanel::attachClassBehavior('javascripts', new TJsPanelClassBehavior()); |
||
1740 | * TApplication::attachClassBehavior('jpegize', \Prado\Util\Behaviors\TJPEGizeAssetBehavior::class, \Prado\Web\TFileAsset::class); |
||
1741 | * ``` |
||
1742 | * An array is used to initialize values of the behavior. eg. ['class' => '\\MyBehavior', 'property' => 'value']. |
||
1743 | * @param null|numeric $priority priority of behavior, default: null the default |
||
1744 | * priority of the {@see \Prado\Collections\TWeakCallableCollection} Optional. |
||
1745 | * @throws TInvalidOperationException if the class behavior is being added to a |
||
1746 | * {@see \Prado\TComponent}; due to recursion. |
||
1747 | * @throws TInvalidOperationException if the class behavior is already defined |
||
1748 | * @return array|object the behavior if its an IClassBehavior and an array of all |
||
1749 | * behaviors that have been attached from 'fxAttachClassBehavior' when the Class |
||
1750 | * Behavior being attached is a per instance IBaseBehavior. |
||
1751 | * @since 3.2.3 |
||
1752 | */ |
||
1753 | public static function attachClassBehavior($name, $behavior, $class = null, $priority = null) |
||
1754 | { |
||
1755 | if (!$class) { |
||
1756 | $class = get_called_class(); |
||
1757 | } |
||
1758 | if (!$class) { |
||
1759 | throw new TInvalidOperationException('component_no_class_provided_nor_late_binding'); |
||
1760 | } |
||
1761 | |||
1762 | $class = strtolower($class); |
||
1763 | if ($class === strtolower(TComponent::class)) { |
||
1764 | throw new TInvalidOperationException('component_no_tcomponent_class_behaviors'); |
||
1765 | } |
||
1766 | if (empty(self::$_um[$class])) { |
||
1767 | self::$_um[$class] = []; |
||
1768 | } |
||
1769 | $name = strtolower($name !== null ? $name : ''); |
||
1770 | if (!empty($name) && !is_numeric($name) && isset(self::$_um[$class][$name])) { |
||
1771 | throw new TInvalidOperationException('component_class_behavior_defined', $class, $name); |
||
1772 | } |
||
1773 | $behaviorObject = self::instanceBehavior($behavior); |
||
1774 | $behaviorObject->setName($name); |
||
1775 | $isClassBehavior = $behaviorObject instanceof \Prado\Util\IClassBehavior; |
||
1776 | $param = new TClassBehaviorEventParameter($class, $name, $isClassBehavior ? $behaviorObject : $behavior, $priority); |
||
1777 | if (empty($name) || is_numeric($name)) { |
||
1778 | self::$_um[$class][] = $param; |
||
1779 | } else { |
||
1780 | self::$_um[$class] = [$name => $param] + self::$_um[$class]; |
||
1781 | } |
||
1782 | $results = $behaviorObject->raiseEvent('fxAttachClassBehavior', null, $param); |
||
1783 | return $isClassBehavior ? $behaviorObject : $results; |
||
1784 | } |
||
1785 | |||
1786 | /** |
||
1787 | * This will remove a behavior from a class. It unregisters it from future instances and |
||
1788 | * pulls the changes from all the instances that are listening as well. |
||
1789 | * PHP 5.3 uses Late Static Binding to derive the static class upon which this method is called. |
||
1790 | * @param string $name the key of the class behavior |
||
1791 | * @param string $class class on which to attach this behavior. Defaults to null. |
||
1792 | * @param null|false|numeric $priority priority: false is any priority, null is default |
||
1793 | * {@see \Prado\Collections\TWeakCallableCollection} priority, and numeric is a specific priority. |
||
1794 | * @throws TInvalidOperationException if the the class cannot be derived from Late Static Binding and is not |
||
1795 | * not supplied as a parameter. |
||
1796 | * @return null|array|object the behavior if its an IClassBehavior and an array of all behaviors |
||
1797 | * that have been detached from 'fxDetachClassBehavior' when the Class Behavior being |
||
1798 | * attached is a per instance IBehavior. Null if no behavior of $name to detach. |
||
1799 | * @since 3.2.3 |
||
1800 | */ |
||
1801 | public static function detachClassBehavior($name, $class = null, $priority = false) |
||
1802 | { |
||
1803 | if (!$class) { |
||
1804 | $class = get_called_class(); |
||
1805 | } |
||
1806 | if (!$class) { |
||
1807 | throw new TInvalidOperationException('component_no_class_provided_nor_late_binding'); |
||
1808 | } |
||
1809 | |||
1810 | $class = strtolower($class); |
||
1811 | $name = strtolower($name); |
||
1812 | if (empty(self::$_um[$class]) || !isset(self::$_um[$class][$name])) { |
||
1813 | return null; |
||
1814 | } |
||
1815 | $param = self::$_um[$class][$name]; |
||
1816 | $behavior = $param->getBehavior(); |
||
1817 | $behaviorObject = self::instanceBehavior($behavior); |
||
1818 | $behaviorObject->setName($name); |
||
1819 | $isClassBehavior = $behaviorObject instanceof IClassBehavior; |
||
1820 | unset(self::$_um[$class][$name]); |
||
1821 | if (empty(self::$_um[$class])) { |
||
1822 | unset(self::$_um[$class]); |
||
1823 | } |
||
1824 | $results = $behaviorObject->raiseEvent('fxDetachClassBehavior', null, $param); |
||
1825 | return $isClassBehavior ? $behaviorObject : $results; |
||
1826 | } |
||
1827 | |||
1828 | /** |
||
1829 | * Returns the named behavior object. If the $behaviorname is not found, but is |
||
1830 | * an existing class or interface, this will return the first instanceof. |
||
1831 | * The name 'asa' stands for 'as a'. |
||
1832 | * @param string $behaviorname the behavior name or the class name of the behavior. |
||
1833 | * @return object the behavior object of name or class, or null if the behavior does not exist |
||
1834 | * @since 3.2.3 |
||
1835 | */ |
||
1836 | public function asa($behaviorname) |
||
1837 | { |
||
1838 | $behaviorname = strtolower($behaviorname); |
||
1839 | if (isset($this->_m[$behaviorname])) { |
||
1840 | return $this->_m[$behaviorname]; |
||
1841 | } |
||
1842 | if ((class_exists($behaviorname, false) || interface_exists($behaviorname, false)) && $this->_m) { |
||
1843 | foreach ($this->_m->toArray() as $behavior) { |
||
1844 | if ($behavior instanceof $behaviorname) { |
||
1845 | return $behavior; |
||
1846 | } |
||
1847 | } |
||
1848 | } |
||
1849 | return null; |
||
1850 | } |
||
1851 | |||
1852 | /** |
||
1853 | * Returns whether or not the object or any of the behaviors are of a particular class. |
||
1854 | * The name 'isa' stands for 'is a'. This first checks if $this is an instanceof the class. |
||
1855 | * Then it checks if the $class is in the hierarchy, which includes first level traits. |
||
1856 | * It then checks each Behavior. If a behavior implements {@see \Prado\Util\IInstanceCheck}, |
||
1857 | * then the behavior can determine what it is an instanceof. If this behavior function returns true, |
||
1858 | * then this method returns true. If the behavior instance checking function returns false, |
||
1859 | * then no further checking is performed as it is assumed to be correct. |
||
1860 | * |
||
1861 | * If the behavior instance check function returns nothing or null or the behavior |
||
1862 | * doesn't implement the {@see \Prado\Util\IInstanceCheck} interface, then the default instanceof occurs. |
||
1863 | * The default isa behavior is to check if the behavior is an instanceof the class. |
||
1864 | * |
||
1865 | * The behavior {@see \Prado\Util\IInstanceCheck} is to allow a behavior to have the host object |
||
1866 | * act as a completely different object. |
||
1867 | * |
||
1868 | * @param mixed|string $class class or string |
||
1869 | * @return bool whether or not the object or a behavior is an instance of a particular class |
||
1870 | * @since 3.2.3 |
||
1871 | */ |
||
1872 | public function isa($class) |
||
1873 | { |
||
1874 | if ($this instanceof $class || in_array(strtolower(is_object($class) ? $class::class : $class), $this->getClassHierarchy(true))) { |
||
1875 | return true; |
||
1876 | } |
||
1877 | if ($this->_m !== null && $this->getBehaviorsEnabled()) { |
||
1878 | foreach ($this->_m->toArray() as $behavior) { |
||
1879 | if (!$behavior->getEnabled()) { |
||
1880 | continue; |
||
1881 | } |
||
1882 | |||
1883 | $check = null; |
||
1884 | if (($behavior->isa(\Prado\Util\IInstanceCheck::class)) && $check = $behavior->isinstanceof($class, $this)) { |
||
1885 | return true; |
||
1886 | } |
||
1887 | if ($check === null && ($behavior->isa($class))) { |
||
1888 | return true; |
||
1889 | } |
||
1890 | } |
||
1891 | } |
||
1892 | return false; |
||
1893 | } |
||
1894 | |||
1895 | /** |
||
1896 | * Returns all the behaviors attached to the TComponent. IBaseBehavior[s] may |
||
1897 | * be attached but not {@see \Prado\Util\IBaseBehavior::getEnabled Enabled}. |
||
1898 | * @param ?string $class Filters the result by class, default null for no filter. |
||
1899 | * @return array The behaviors [optionally filtered] attached to the TComponent. |
||
1900 | * @since 4.2.2 |
||
1901 | */ |
||
1902 | public function getBehaviors(?string $class = null) |
||
1903 | { |
||
1904 | if ($class === null) { |
||
1905 | return isset($this->_m) ? $this->_m->toArray() : []; |
||
1906 | } elseif (class_exists($class, false) || interface_exists($class, false)) { |
||
1907 | return array_filter($this->_m->toArray(), fn ($b) => $b instanceof $class); |
||
1908 | } |
||
1909 | return []; |
||
1910 | } |
||
1911 | |||
1912 | /** |
||
1913 | * Attaches a list of behaviors to the component. |
||
1914 | * Each behavior is indexed by its name and should be an instance of |
||
1915 | * {@see \Prado\Util\IBaseBehavior}, a string specifying the behavior class, or a |
||
1916 | * {@see \Prado\Util\TClassBehaviorEventParameter}. |
||
1917 | * @param array $behaviors list of behaviors to be attached to the component |
||
1918 | * @param bool $cloneIBehavior Should IBehavior be cloned before attaching. |
||
1919 | * Default is false. |
||
1920 | * @since 3.2.3 |
||
1921 | */ |
||
1922 | public function attachBehaviors($behaviors, bool $cloneIBehavior = false) |
||
1923 | { |
||
1924 | foreach ($behaviors as $name => $behavior) { |
||
1925 | if ($behavior instanceof TClassBehaviorEventParameter) { |
||
1926 | $paramBehavior = $behavior->getBehavior(); |
||
1927 | if ($cloneIBehavior && ($paramBehavior instanceof IBehavior)) { |
||
1928 | $paramBehavior = clone $paramBehavior; |
||
1929 | } |
||
1930 | $this->attachBehavior($behavior->getName(), $paramBehavior, $behavior->getPriority()); |
||
1931 | } else { |
||
1932 | if ($cloneIBehavior && ($behavior instanceof IBehavior)) { |
||
1933 | $behavior = clone $behavior; |
||
1934 | } |
||
1935 | $this->attachBehavior($name, $behavior); |
||
1936 | } |
||
1937 | } |
||
1938 | } |
||
1939 | |||
1940 | /** |
||
1941 | * Detaches select behaviors from the component. |
||
1942 | * Each behavior is indexed by its name and should be an instance of |
||
1943 | * {@see \Prado\Util\IBaseBehavior}, a string specifying the behavior class, or a |
||
1944 | * {@see \Prado\Util\TClassBehaviorEventParameter}. |
||
1945 | * @param array $behaviors list of behaviors to be detached from the component |
||
1946 | * @since 3.2.3 |
||
1947 | */ |
||
1948 | public function detachBehaviors($behaviors) |
||
1949 | { |
||
1950 | if ($this->_m !== null) { |
||
1951 | foreach ($behaviors as $name => $behavior) { |
||
1952 | if ($behavior instanceof TClassBehaviorEventParameter) { |
||
1953 | $this->detachBehavior($behavior->getName(), $behavior->getPriority()); |
||
1954 | } else { |
||
1955 | $this->detachBehavior(is_string($behavior) ? $behavior : $name); |
||
1956 | } |
||
1957 | } |
||
1958 | } |
||
1959 | } |
||
1960 | |||
1961 | /** |
||
1962 | * Detaches all behaviors from the component. |
||
1963 | * @since 3.2.3 |
||
1964 | */ |
||
1965 | public function clearBehaviors() |
||
1966 | { |
||
1967 | if ($this->_m !== null) { |
||
1968 | foreach ($this->_m->getKeys() as $name) { |
||
1969 | $this->detachBehavior($name); |
||
1970 | } |
||
1971 | $this->_m = null; |
||
1972 | } |
||
1973 | } |
||
1974 | |||
1975 | /** |
||
1976 | * Attaches a behavior to this component. |
||
1977 | * This method will create the behavior object based on the given |
||
1978 | * configuration. After that, the behavior object will be initialized |
||
1979 | * by calling its {@see \Prado\Util\IBaseBehavior::attach} method. |
||
1980 | * |
||
1981 | * Already attached behaviors may implement the function: |
||
1982 | * ```php |
||
1983 | * public function dyAttachBehavior($name,$behavior[, ?TCallChain $chain = null]) { |
||
1984 | * if ($chain) |
||
1985 | * return $chain->dyDetachBehavior($name, $behavior); |
||
1986 | * } |
||
1987 | * ``` |
||
1988 | * to be executed when attachBehavior is called. All attached behaviors are notified through |
||
1989 | * dyAttachBehavior. |
||
1990 | * |
||
1991 | * @param null|numeric|string $name the behavior's name. It should uniquely identify this behavior. |
||
1992 | * @param array|IBaseBehavior|string $behavior the behavior configuration. This is the name of the Behavior Class |
||
1993 | * instanced by {@see \Prado\PradoBase::createComponent}, or is a Behavior, or is an array of |
||
1994 | * ['class'=>'TBehavior' property1='value 1' property2='value2'...] with the class and properties |
||
1995 | * with values. |
||
1996 | * @param null|numeric $priority |
||
1997 | * @return IBaseBehavior the behavior object |
||
1998 | * @since 3.2.3 |
||
1999 | */ |
||
2000 | public function attachBehavior($name, $behavior, $priority = null) |
||
2001 | { |
||
2002 | $name = strtolower($name !== null ? $name : ''); |
||
2003 | if ($this->_m && isset($this->_m[$name])) { |
||
2004 | $this->detachBehavior($name); |
||
2005 | } |
||
2006 | $behavior = self::instanceBehavior($behavior); |
||
2007 | if ($this->_m === null) { |
||
2008 | $this->_m = new TPriorityMap(); |
||
2009 | } |
||
2010 | if (empty($name) || is_numeric($name)) { |
||
2011 | $name = $this->_m->getNextIntegerKey(); |
||
2012 | } |
||
2013 | $this->_m->add($name, $behavior, $priority); |
||
2014 | $behavior->setName($name); |
||
2015 | $behavior->attach($this); |
||
2016 | $this->callBehaviorsMethod('dyAttachBehavior', $return, $name, $behavior); |
||
2017 | return $behavior; |
||
2018 | } |
||
2019 | |||
2020 | /** |
||
2021 | * Detaches a behavior from the component. |
||
2022 | * The behavior's {@see \Prado\Util\IBaseBehavior::detach} method will be invoked. |
||
2023 | * |
||
2024 | * Behaviors may implement the function: |
||
2025 | * ```php |
||
2026 | * public function dyDetachBehavior($name, $behavior[, ?TCallChain $chain = null]) { |
||
2027 | * if ($chain) |
||
2028 | * return $chain->dyDetachBehavior($name, $behavior); |
||
2029 | * } |
||
2030 | * ``` |
||
2031 | * to be executed when detachBehavior is called. All attached behaviors are notified through |
||
2032 | * dyDetachBehavior. |
||
2033 | * |
||
2034 | * @param string $name the behavior's name. It uniquely identifies the behavior. |
||
2035 | * @param false|numeric $priority the behavior's priority. This defaults to false, which is any priority. |
||
2036 | * @return null|IBaseBehavior the detached behavior. Null if the behavior does not exist. |
||
2037 | * @since 3.2.3 |
||
2038 | */ |
||
2039 | public function detachBehavior($name, $priority = false) |
||
2040 | { |
||
2041 | $name = strtolower($name); |
||
2042 | if ($this->_m != null && ($behavior = $this->_m->itemAt($name, $priority))) { |
||
2043 | $this->callBehaviorsMethod('dyDetachBehavior', $return, $name, $behavior); |
||
2044 | $behavior->detach($this); |
||
2045 | $this->_m->remove($name, $priority); |
||
2046 | return $behavior; |
||
2047 | } |
||
2048 | return null; |
||
2049 | } |
||
2050 | |||
2051 | /** |
||
2052 | * Enables all behaviors attached to this component independent of the behaviors |
||
2053 | * |
||
2054 | * Behaviors may implement the function: |
||
2055 | * ```php |
||
2056 | * public function dyEnableBehaviors([?TCallChain $chain = null]) { |
||
2057 | * if ($chain) |
||
2058 | * return $chain->dyEnableBehaviors(); |
||
2059 | * } |
||
2060 | * ``` |
||
2061 | * to be executed when enableBehaviors is called. All attached behaviors are notified through |
||
2062 | * dyEnableBehaviors. |
||
2063 | * @since 3.2.3 |
||
2064 | */ |
||
2065 | public function enableBehaviors() |
||
2066 | { |
||
2067 | if (!$this->_behaviorsenabled) { |
||
2068 | $this->_behaviorsenabled = true; |
||
2069 | $this->callBehaviorsMethod('dyEnableBehaviors', $return); |
||
2070 | } |
||
2071 | } |
||
2072 | |||
2073 | /** |
||
2074 | * Disables all behaviors attached to this component independent of the behaviors |
||
2075 | * |
||
2076 | * Behaviors may implement the function: |
||
2077 | * ```php |
||
2078 | * public function dyDisableBehaviors([?TCallChain $chain = null]) { |
||
2079 | * if ($chain) |
||
2080 | * return $chain->dyDisableBehaviors(); |
||
2081 | * } |
||
2082 | * ``` |
||
2083 | * to be executed when disableBehaviors is called. All attached behaviors are notified through |
||
2084 | * dyDisableBehaviors. |
||
2085 | * @since 3.2.3 |
||
2086 | */ |
||
2087 | public function disableBehaviors() |
||
2088 | { |
||
2089 | if ($this->_behaviorsenabled) { |
||
2090 | $callchain = $this->getCallChain('dyDisableBehaviors'); |
||
2091 | $this->_behaviorsenabled = false; |
||
2092 | if ($callchain) { // normal dynamic events won't work because behaviors are disabled. |
||
2093 | $callchain->call(); |
||
2094 | } |
||
2095 | } |
||
2096 | } |
||
2097 | |||
2098 | |||
2099 | /** |
||
2100 | * Returns if all the behaviors are turned on or off for the object. |
||
2101 | * @return bool whether or not all behaviors are enabled (true) or not (false) |
||
2102 | * @since 3.2.3 |
||
2103 | */ |
||
2104 | public function getBehaviorsEnabled() |
||
2105 | { |
||
2106 | return $this->_behaviorsenabled; |
||
2107 | } |
||
2108 | |||
2109 | /** |
||
2110 | * Enables an attached object behavior. This cannot enable or disable whole class behaviors. |
||
2111 | * A behavior is only effective when it is enabled. |
||
2112 | * A behavior is enabled when first attached. |
||
2113 | * |
||
2114 | * Behaviors may implement the function: |
||
2115 | * ```php |
||
2116 | * public function dyEnableBehavior($name, $behavior[, ?TCallChain $chain = null]) { |
||
2117 | * if ($chain) |
||
2118 | * return $chain->dyEnableBehavior($name, $behavior); |
||
2119 | * } |
||
2120 | * ``` |
||
2121 | * to be executed when enableBehavior is called. All attached behaviors are notified through |
||
2122 | * dyEnableBehavior. |
||
2123 | * |
||
2124 | * @param string $name the behavior's name. It uniquely identifies the behavior. |
||
2125 | * @return bool Was the behavior found and enabled. |
||
2126 | * @since 3.2.3 |
||
2127 | */ |
||
2128 | public function enableBehavior($name): bool |
||
2129 | { |
||
2130 | $name = strtolower($name); |
||
2131 | if ($this->_m != null && isset($this->_m[$name])) { |
||
2132 | $behavior = $this->_m[$name]; |
||
2133 | if ($behavior->getEnabled() === false) { |
||
2134 | $behavior->setEnabled(true); |
||
2135 | $this->callBehaviorsMethod('dyEnableBehavior', $return, $name, $behavior); |
||
2136 | } |
||
2137 | return true; |
||
2138 | } |
||
2139 | return false; |
||
2140 | } |
||
2141 | |||
2142 | /** |
||
2143 | * Disables an attached behavior. This cannot enable or disable whole class behaviors. |
||
2144 | * A behavior is only effective when it is enabled. |
||
2145 | * |
||
2146 | * Behaviors may implement the function: |
||
2147 | * ```php |
||
2148 | * public function dyDisableBehavior($name, $behavior[, ?TCallChain $chain = null]) { |
||
2149 | * if ($chain) |
||
2150 | * return $chain->dyDisableBehavior($name, $behavior); |
||
2151 | * } |
||
2152 | * ``` |
||
2153 | * to be executed when disableBehavior is called. All attached behaviors are notified through |
||
2154 | * dyDisableBehavior. |
||
2155 | * |
||
2156 | * @param string $name the behavior's name. It uniquely identifies the behavior. |
||
2157 | * @return bool Was the behavior found and disabled. |
||
2158 | * @since 3.2.3 |
||
2159 | */ |
||
2160 | public function disableBehavior($name): bool |
||
2161 | { |
||
2162 | $name = strtolower($name); |
||
2163 | if ($this->_m != null && isset($this->_m[$name])) { |
||
2164 | $behavior = $this->_m[$name]; |
||
2165 | if ($behavior->getEnabled() === true) { |
||
2166 | $behavior->setEnabled(false); |
||
2167 | $this->callBehaviorsMethod('dyDisableBehavior', $return, $name, $behavior); |
||
2168 | } |
||
2169 | return true; |
||
2170 | } |
||
2171 | return false; |
||
2172 | } |
||
2173 | |||
2174 | /** |
||
2175 | * Returns an array with the names of all variables of that object that should be serialized. |
||
2176 | * Do not call this method. This is a PHP magic method that will be called automatically |
||
2177 | * prior to any serialization. |
||
2178 | */ |
||
2179 | public function __sleep() |
||
2180 | { |
||
2181 | $a = (array) $this; |
||
2182 | $a = array_keys($a); |
||
2183 | $exprops = []; |
||
2184 | $this->_getZappableSleepProps($exprops); |
||
2185 | return array_diff($a, $exprops); |
||
2186 | } |
||
2187 | |||
2188 | /** |
||
2189 | * Returns an array with the names of all variables of this object that should NOT be serialized |
||
2190 | * because their value is the default one or useless to be cached for the next page loads. |
||
2191 | * Reimplement in derived classes to add new variables, but remember to also to call the parent |
||
2192 | * implementation first. |
||
2193 | * @param array $exprops by reference |
||
2194 | */ |
||
2195 | protected function _getZappableSleepProps(&$exprops) |
||
2206 | } |
||
2207 | } |
||
2208 | } |
||
2209 |