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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 |
||
| 282 | class TComponent |
||
| 283 | { |
||
| 284 | /** |
||
| 285 | * @var array event handler lists |
||
| 286 | */ |
||
| 287 | private $_e=array(); |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @var boolean if listening is enabled. Automatically turned on or off in |
||
| 291 | * constructor according to {@link getAutoGlobalListen}. Default false, off |
||
| 292 | */ |
||
| 293 | private $_listeningenabled=false; |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @var array static registered global event handler lists |
||
| 297 | */ |
||
| 298 | private static $_ue=array(); |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @var boolean if object behaviors are on or off. default true, on |
||
| 302 | */ |
||
| 303 | private $_behaviorsenabled=true; |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @var TPriorityMap list of object behaviors |
||
| 307 | */ |
||
| 308 | private $_m=null; |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @var array static global class behaviors, these behaviors are added upon instantiation of a class |
||
| 312 | */ |
||
| 313 | private static $_um=array(); |
||
| 314 | |||
| 315 | |||
| 316 | /** |
||
| 317 | * @const string the name of the global {@link raiseEvent} listener |
||
| 318 | */ |
||
| 319 | const GLOBAL_RAISE_EVENT_LISTENER='fxGlobalListener'; |
||
| 320 | |||
| 321 | |||
| 322 | /** |
||
| 323 | * The common __construct |
||
| 324 | * If desired by the new object, this will auto install and listen to global event functions |
||
| 325 | * as defined by the object via 'fx' methods. This also attaches any predefined behaviors. |
||
| 326 | * This function installs all class behaviors in a class hierarchy from the deepest subclass |
||
| 327 | * through each parent to the top most class, TComponent. |
||
| 328 | */ |
||
| 329 | public function __construct() { |
||
| 330 | if($this->getAutoGlobalListen()) |
||
| 331 | $this->listen(); |
||
| 332 | |||
| 333 | $classes=array_reverse($this->getClassHierarchy(true)); |
||
| 334 | foreach($classes as $class) { |
||
| 335 | if(isset(self::$_um[$class])) |
||
| 336 | $this->attachBehaviors(self::$_um[$class]); |
||
| 337 | } |
||
| 338 | } |
||
| 339 | |||
| 340 | |||
| 341 | /** |
||
| 342 | * Tells TComponent whether or not to automatically listen to global events. |
||
| 343 | * Defaults to false because PHP variable cleanup is affected if this is true. |
||
| 344 | * When unsetting a variable that is listening to global events, {@link unlisten} |
||
| 345 | * must explicitly be called when cleaning variables allocation or else the global |
||
| 346 | * event registry will contain references to the old object. This is true for PHP 5.4 |
||
| 347 | * |
||
| 348 | * Override this method by a subclass to change the setting. When set to true, this |
||
| 349 | * will enable {@link __construct} to call {@link listen}. |
||
| 350 | * |
||
| 351 | * @return boolean whether or not to auto listen to global events during {@link __construct}, default false |
||
| 352 | */ |
||
| 353 | public function getAutoGlobalListen() { |
||
| 354 | return false; |
||
| 355 | } |
||
| 356 | |||
| 357 | |||
| 358 | /** |
||
| 359 | * The common __destruct |
||
| 360 | * This unlistens from the global event routines if listening |
||
| 361 | * |
||
| 362 | * PHP 5.3 does not __destruct objects when they are nulled and thus unlisten must be |
||
| 363 | * called must be explicitly called. |
||
| 364 | */ |
||
| 365 | public function __destruct() { |
||
| 366 | if($this->_listeningenabled) |
||
| 367 | $this->unlisten(); |
||
| 368 | } |
||
| 369 | |||
| 370 | |||
| 371 | /** |
||
| 372 | * This utility function is a private array filter method. The array values |
||
| 373 | * that start with 'fx' are filtered in. |
||
| 374 | */ |
||
| 375 | private function filter_prado_fx($name) { |
||
|
|
|||
| 376 | return strncasecmp($name,'fx',2)===0; |
||
| 377 | } |
||
| 378 | |||
| 379 | |||
| 380 | /** |
||
| 381 | * This returns an array of the class name and the names of all its parents. The base object first, |
||
| 382 | * {@link TComponent}, and the deepest subclass is last. |
||
| 383 | * @param boolean optional should the names be all lowercase true/false |
||
| 384 | * @return array array of strings being the class hierarchy of $this. |
||
| 385 | */ |
||
| 386 | public function getClassHierarchy($lowercase = false) |
||
| 387 | { |
||
| 388 | $class=get_class($this); |
||
| 389 | $classes=array($class); |
||
| 390 | while($class=get_parent_class($class)){array_unshift($classes,$class);} |
||
| 391 | if($lowercase) |
||
| 392 | return array_map('strtolower',$classes); |
||
| 393 | return $classes; |
||
| 394 | } |
||
| 395 | |||
| 396 | |||
| 397 | /** |
||
| 398 | * This adds an object's fx event handlers into the global broadcaster to listen into any |
||
| 399 | * broadcast global events called through {@link raiseEvent} |
||
| 400 | * |
||
| 401 | * Behaviors may implement the function: |
||
| 402 | * <code> |
||
| 403 | * public function dyListen($globalEvents[, $chain]) { |
||
| 404 | * $this->listen(); //eg |
||
| 405 | * } |
||
| 406 | * </code> |
||
| 407 | * to be executed when listen is called. All attached behaviors are notified through dyListen. |
||
| 408 | * |
||
| 409 | * @return numeric the number of global events that were registered to the global event registry |
||
| 410 | */ |
||
| 411 | public function listen() { |
||
| 412 | if($this->_listeningenabled) |
||
| 413 | return; |
||
| 414 | |||
| 415 | $fx=array_filter(get_class_methods($this),array($this,'filter_prado_fx')); |
||
| 416 | |||
| 417 | foreach($fx as $func) |
||
| 418 | $this->attachEventHandler($func,array($this,$func)); |
||
| 419 | |||
| 420 | if(is_a($this,'IDynamicMethods')) { |
||
| 421 | $this->attachEventHandler(TComponent::GLOBAL_RAISE_EVENT_LISTENER,array($this,'__dycall')); |
||
| 422 | array_push($fx,TComponent::GLOBAL_RAISE_EVENT_LISTENER); |
||
| 423 | } |
||
| 424 | |||
| 425 | $this->_listeningenabled=true; |
||
| 426 | |||
| 427 | $this->dyListen($fx); |
||
| 428 | |||
| 429 | return count($fx); |
||
| 430 | } |
||
| 431 | |||
| 432 | /** |
||
| 433 | * this removes an object's fx events from the global broadcaster |
||
| 434 | * |
||
| 435 | * Behaviors may implement the function: |
||
| 436 | * <code> |
||
| 437 | * public function dyUnlisten($globalEvents[, $chain]) { |
||
| 438 | * $this->behaviorUnlisten(); //eg |
||
| 439 | * } |
||
| 440 | * </code> |
||
| 441 | * to be executed when listen is called. All attached behaviors are notified through dyUnlisten. |
||
| 442 | * |
||
| 443 | * @return numeric the number of global events that were unregistered from the global event registry |
||
| 444 | */ |
||
| 445 | public function unlisten() { |
||
| 446 | if(!$this->_listeningenabled) |
||
| 447 | return; |
||
| 448 | |||
| 449 | $fx=array_filter(get_class_methods($this),array($this,'filter_prado_fx')); |
||
| 450 | |||
| 451 | foreach($fx as $func) |
||
| 452 | $this->detachEventHandler($func,array($this,$func)); |
||
| 453 | |||
| 454 | if(is_a($this,'IDynamicMethods')) { |
||
| 455 | $this->detachEventHandler(TComponent::GLOBAL_RAISE_EVENT_LISTENER,array($this,'__dycall')); |
||
| 456 | array_push($fx,TComponent::GLOBAL_RAISE_EVENT_LISTENER); |
||
| 457 | } |
||
| 458 | |||
| 459 | $this->_listeningenabled=false; |
||
| 460 | |||
| 461 | $this->dyUnlisten($fx); |
||
| 462 | |||
| 463 | return count($fx); |
||
| 464 | } |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Gets the state of listening to global events |
||
| 468 | * @return boolean is Listening to global broadcast enabled |
||
| 469 | */ |
||
| 470 | public function getListeningToGlobalEvents() |
||
| 471 | { |
||
| 472 | return $this->_listeningenabled; |
||
| 473 | } |
||
| 474 | |||
| 475 | |||
| 476 | /** |
||
| 477 | * Calls a method. |
||
| 478 | * Do not call this method directly. This is a PHP magic method that we override |
||
| 479 | * to allow behaviors, dynamic events (intra-object/behavior events), |
||
| 480 | * undefined dynamic and global events, and |
||
| 481 | * to allow using the following syntax to call a property setter or getter. |
||
| 482 | * <code> |
||
| 483 | * $this->getPropertyName($value); // if there's a $this->getjsPropertyName() method |
||
| 484 | * $this->setPropertyName($value); // if there's a $this->setjsPropertyName() method |
||
| 485 | * </code> |
||
| 486 | * |
||
| 487 | * Additional object behaviors override class behaviors. |
||
| 488 | * dynamic and global events do not fail even if they aren't implemented. |
||
| 489 | * Any intra-object/behavior dynamic events that are not implemented by the behavior |
||
| 490 | * return the first function paramater or null when no parameters are specified. |
||
| 491 | * |
||
| 492 | * @param string method name that doesn't exist and is being called on the object |
||
| 493 | * @param mixed method parameters |
||
| 494 | * @throws TInvalidOperationException If the property is not defined or read-only or |
||
| 495 | * method is undefined |
||
| 496 | * @return mixed result of the method call, or false if 'fx' or 'dy' function but |
||
| 497 | * is not found in the class, otherwise it runs |
||
| 498 | */ |
||
| 499 | public function __call($method, $args) |
||
| 563 | |||
| 564 | |||
| 565 | /** |
||
| 566 | * Returns a property value or an event handler list by property or event name. |
||
| 567 | * Do not call this method. This is a PHP magic method that we override |
||
| 568 | * to allow using the following syntax to read a property: |
||
| 569 | * <code> |
||
| 570 | * $value=$component->PropertyName; |
||
| 571 | * $value=$component->jsPropertyName; // return JavaScript literal |
||
| 572 | * </code> |
||
| 573 | * and to obtain the event handler list for an event, |
||
| 574 | * <code> |
||
| 575 | * $eventHandlerList=$component->EventName; |
||
| 576 | * </code> |
||
| 577 | * This will also return the global event handler list when specifing an 'fx' |
||
| 578 | * event, |
||
| 579 | * <code> |
||
| 580 | * $globalEventHandlerList=$component->fxEventName; |
||
| 581 | * </code> |
||
| 582 | * When behaviors are enabled, this will return the behavior of a specific |
||
| 583 | * name, a property of a behavior, or an object 'on' event defined by the behavior. |
||
| 584 | * @param string the property name or the event name |
||
| 585 | * @return mixed the property value or the event handler list as {@link TPriorityList} |
||
| 586 | * @throws TInvalidOperationException if the property/event is not defined. |
||
| 587 | */ |
||
| 588 | public function __get($name) |
||
| 589 | { |
||
| 590 | if(method_exists($this,$getter='get'.$name)) |
||
| 591 | { |
||
| 592 | // getting a property |
||
| 593 | return $this->$getter(); |
||
| 594 | } |
||
| 595 | else if(method_exists($this,$jsgetter='getjs'.$name)) |
||
| 596 | { |
||
| 597 | // getting a javascript property |
||
| 598 | return (string)$this->$jsgetter(); |
||
| 599 | } |
||
| 600 | else if(strncasecmp($name,'on',2)===0 && method_exists($this,$name)) |
||
| 601 | { |
||
| 602 | // getting an event (handler list) |
||
| 603 | $name=strtolower($name); |
||
| 604 | if(!isset($this->_e[$name])) |
||
| 605 | $this->_e[$name]=new TPriorityList; |
||
| 606 | return $this->_e[$name]; |
||
| 607 | } |
||
| 608 | else if(strncasecmp($name,'fx',2)===0) |
||
| 609 | { |
||
| 610 | // getting a global event (handler list) |
||
| 611 | $name=strtolower($name); |
||
| 612 | if(!isset(self::$_ue[$name])) |
||
| 613 | self::$_ue[$name]=new TPriorityList; |
||
| 614 | return self::$_ue[$name]; |
||
| 615 | } |
||
| 616 | else if($this->_behaviorsenabled) |
||
| 617 | { |
||
| 618 | // getting a behavior property/event (handler list) |
||
| 619 | if(isset($this->_m[$name])) |
||
| 620 | return $this->_m[$name]; |
||
| 621 | else if($this->_m!==null) |
||
| 622 | { |
||
| 623 | foreach($this->_m->toArray() as $behavior) |
||
| 624 | { |
||
| 625 | if((!($behavior instanceof IBehavior)||$behavior->getEnabled())&& |
||
| 626 | (property_exists($behavior,$name)||$behavior->canGetProperty($name)||$behavior->hasEvent($name))) |
||
| 627 | return $behavior->$name; |
||
| 628 | } |
||
| 629 | } |
||
| 630 | } |
||
| 631 | throw new TInvalidOperationException('component_property_undefined',get_class($this),$name); |
||
| 632 | } |
||
| 633 | |||
| 634 | /** |
||
| 635 | * Sets value of a component property. |
||
| 636 | * Do not call this method. This is a PHP magic method that we override |
||
| 637 | * to allow using the following syntax to set a property or attach an event handler. |
||
| 638 | * <code> |
||
| 639 | * $this->PropertyName=$value; |
||
| 640 | * $this->jsPropertyName=$value; // $value will be treated as a JavaScript literal |
||
| 641 | * $this->EventName=$handler; |
||
| 642 | * $this->fxEventName=$handler; //global event listener |
||
| 643 | * </code> |
||
| 644 | * When behaviors are enabled, this will also set a behaviors properties and events. |
||
| 645 | * @param string the property name or event name |
||
| 646 | * @param mixed the property value or event handler |
||
| 647 | * @throws TInvalidOperationException If the property is not defined or read-only. |
||
| 648 | */ |
||
| 649 | public function __set($name,$value) |
||
| 650 | { |
||
| 651 | if(method_exists($this,$setter='set'.$name)) |
||
| 652 | { |
||
| 653 | if(strncasecmp($name,'js',2)===0&&$value&&!($value instanceof TJavaScriptLiteral)) |
||
| 654 | $value = new TJavaScriptLiteral($value); |
||
| 655 | return $this->$setter($value); |
||
| 656 | } |
||
| 657 | else if(method_exists($this,$jssetter='setjs'.$name)) |
||
| 658 | { |
||
| 659 | if($value&&!($value instanceof TJavaScriptString)) |
||
| 660 | $value=new TJavaScriptString($value); |
||
| 661 | return $this->$jssetter($value); |
||
| 662 | } |
||
| 663 | else if((strncasecmp($name,'on',2)===0&&method_exists($this,$name))||strncasecmp($name,'fx',2)===0) |
||
| 664 | { |
||
| 665 | return $this->attachEventHandler($name,$value); |
||
| 666 | } |
||
| 667 | else if($this->_m!==null&&$this->_m->getCount()>0&&$this->_behaviorsenabled) |
||
| 668 | { |
||
| 669 | $sets=0; |
||
| 670 | foreach($this->_m->toArray() as $behavior) |
||
| 671 | { |
||
| 672 | if((!($behavior instanceof IBehavior)||$behavior->getEnabled())&& |
||
| 673 | (property_exists($behavior,$name)||$behavior->canSetProperty($name)||$behavior->hasEvent($name))) { |
||
| 674 | $behavior->$name=$value; |
||
| 675 | $sets++; |
||
| 676 | } |
||
| 677 | } |
||
| 678 | if($sets)return $value; |
||
| 679 | |||
| 680 | } |
||
| 681 | |||
| 682 | if(method_exists($this,'get'.$name)||method_exists($this,'getjs'.$name)) |
||
| 683 | { |
||
| 684 | throw new TInvalidOperationException('component_property_readonly',get_class($this),$name); |
||
| 685 | } |
||
| 686 | else |
||
| 687 | { |
||
| 688 | throw new TInvalidOperationException('component_property_undefined',get_class($this),$name); |
||
| 689 | } |
||
| 690 | } |
||
| 691 | |||
| 692 | /** |
||
| 693 | * Checks if a property value is null, there are no events in the object |
||
| 694 | * event list or global event list registered under the name, and, if |
||
| 695 | * behaviors are enabled, |
||
| 696 | * Do not call this method. This is a PHP magic method that we override |
||
| 697 | * to allow using isset() to detect if a component property is set or not. |
||
| 698 | * This also works for global events. When behaviors are enabled, it |
||
| 699 | * will check for a behavior of the specified name, and also check |
||
| 700 | * the behavior for events and properties. |
||
| 701 | * @param string the property name or the event name |
||
| 702 | * @since 3.2.3 |
||
| 703 | */ |
||
| 704 | public function __isset($name) |
||
| 705 | { |
||
| 706 | if(method_exists($this,$getter='get'.$name)) |
||
| 707 | return $this->$getter()!==null; |
||
| 708 | else if(method_exists($this,$jsgetter='getjs'.$name)) |
||
| 709 | return $this->$jsgetter()!==null; |
||
| 710 | else if(strncasecmp($name,'on',2)===0&&method_exists($this,$name)) |
||
| 711 | { |
||
| 712 | $name=strtolower($name); |
||
| 713 | return isset($this->_e[$name])&&$this->_e[$name]->getCount(); |
||
| 714 | } |
||
| 715 | else if(strncasecmp($name,'fx',2)===0) |
||
| 716 | { |
||
| 717 | $name=strtolower($name); |
||
| 718 | return isset(self::$_ue[$name])&&self::$_ue[$name]->getCount(); |
||
| 719 | } |
||
| 720 | else if($this->_m!==null&&$this->_m->getCount()>0&&$this->_behaviorsenabled) |
||
| 721 | { |
||
| 722 | if(isset($this->_m[$name])) |
||
| 723 | return true; |
||
| 724 | foreach($this->_m->toArray() as $behavior) |
||
| 725 | { |
||
| 726 | if((!($behavior instanceof IBehavior)||$behavior->getEnabled())) |
||
| 727 | return isset($behavior->$name); |
||
| 728 | } |
||
| 729 | |||
| 730 | } |
||
| 731 | else |
||
| 732 | return false; |
||
| 733 | } |
||
| 734 | |||
| 735 | /** |
||
| 736 | * Sets a component property to be null. Clears the object or global |
||
| 737 | * events. When enabled, loops through all behaviors and unsets the |
||
| 738 | * property or event. |
||
| 739 | * Do not call this method. This is a PHP magic method that we override |
||
| 740 | * to allow using unset() to set a component property to be null. |
||
| 741 | * @param string the property name or the event name |
||
| 742 | * @throws TInvalidOperationException if the property is read only. |
||
| 743 | * @since 3.2.3 |
||
| 744 | */ |
||
| 745 | public function __unset($name) |
||
| 746 | { |
||
| 747 | if(method_exists($this,$setter='set'.$name)) |
||
| 748 | $this->$setter(null); |
||
| 749 | else if(method_exists($this,$jssetter='setjs'.$name)) |
||
| 750 | $this->$jssetter(null); |
||
| 751 | else if(strncasecmp($name,'on',2)===0&&method_exists($this,$name)) |
||
| 752 | $this->_e[strtolower($name)]->clear(); |
||
| 753 | else if(strncasecmp($name,'fx',2)===0) |
||
| 754 | $this->getEventHandlers($name)->remove(array($this, $name)); |
||
| 755 | else if($this->_m!==null&&$this->_m->getCount()>0&&$this->_behaviorsenabled) |
||
| 756 | { |
||
| 757 | if(isset($this->_m[$name])) |
||
| 758 | $this->detachBehavior($name); |
||
| 759 | else { |
||
| 760 | $unset=0; |
||
| 761 | foreach($this->_m->toArray() as $behavior) |
||
| 762 | { |
||
| 763 | if((!($behavior instanceof IBehavior)||$behavior->getEnabled())) { |
||
| 764 | unset($behavior->$name); |
||
| 765 | $unset++; |
||
| 766 | } |
||
| 767 | } |
||
| 768 | if(!$unset&&method_exists($this,'get'.$name)) |
||
| 769 | throw new TInvalidOperationException('component_property_readonly',get_class($this),$name); |
||
| 770 | } |
||
| 771 | } else if(method_exists($this,'get'.$name)) |
||
| 772 | throw new TInvalidOperationException('component_property_readonly',get_class($this),$name); |
||
| 773 | } |
||
| 774 | |||
| 775 | /** |
||
| 776 | * Determines whether a property is defined. |
||
| 777 | * A property is defined if there is a getter or setter method |
||
| 778 | * defined in the class. Note, property names are case-insensitive. |
||
| 779 | * @param string the property name |
||
| 780 | * @return boolean whether the property is defined |
||
| 781 | */ |
||
| 782 | public function hasProperty($name) |
||
| 783 | { |
||
| 784 | return $this->canGetProperty($name)||$this->canSetProperty($name); |
||
| 785 | } |
||
| 786 | |||
| 787 | /** |
||
| 788 | * Determines whether a property can be read. |
||
| 789 | * A property can be read if the class has a getter method |
||
| 790 | * for the property name. Note, property name is case-insensitive. |
||
| 791 | * This also checks for getjs. When enabled, it loops through all |
||
| 792 | * active behaviors for the get property when undefined by the object. |
||
| 793 | * @param string the property name |
||
| 794 | * @return boolean whether the property can be read |
||
| 795 | */ |
||
| 796 | public function canGetProperty($name) |
||
| 797 | { |
||
| 798 | if(method_exists($this,'get'.$name)||method_exists($this,'getjs'.$name)) |
||
| 799 | return true; |
||
| 800 | else if($this->_m!==null&&$this->_behaviorsenabled) |
||
| 801 | { |
||
| 802 | foreach($this->_m->toArray() as $behavior) |
||
| 803 | { |
||
| 804 | if((!($behavior instanceof IBehavior)||$behavior->getEnabled())&&$behavior->canGetProperty($name)) |
||
| 805 | return true; |
||
| 806 | } |
||
| 807 | } |
||
| 808 | return false; |
||
| 809 | } |
||
| 810 | |||
| 811 | /** |
||
| 812 | * Determines whether a property can be set. |
||
| 813 | * A property can be written if the class has a setter method |
||
| 814 | * for the property name. Note, property name is case-insensitive. |
||
| 815 | * This also checks for setjs. When enabled, it loops through all |
||
| 816 | * active behaviors for the set property when undefined by the object. |
||
| 817 | * @param string the property name |
||
| 818 | * @return boolean whether the property can be written |
||
| 819 | */ |
||
| 820 | public function canSetProperty($name) |
||
| 821 | { |
||
| 822 | if(method_exists($this,'set'.$name)||method_exists($this,'setjs'.$name)) |
||
| 823 | return true; |
||
| 824 | else if($this->_m!==null&&$this->_behaviorsenabled) |
||
| 825 | { |
||
| 826 | foreach($this->_m->toArray() as $behavior) |
||
| 827 | { |
||
| 828 | if((!($behavior instanceof IBehavior)||$behavior->getEnabled())&&$behavior->canSetProperty($name)) |
||
| 829 | return true; |
||
| 830 | } |
||
| 831 | } |
||
| 832 | return false; |
||
| 833 | } |
||
| 834 | |||
| 835 | /** |
||
| 836 | * Evaluates a property path. |
||
| 837 | * A property path is a sequence of property names concatenated by '.' character. |
||
| 838 | * For example, 'Parent.Page' refers to the 'Page' property of the component's |
||
| 839 | * 'Parent' property value (which should be a component also). |
||
| 840 | * When a property is not defined by an object, this also loops through all |
||
| 841 | * active behaviors of the object. |
||
| 842 | * @param string property path |
||
| 843 | * @return mixed the property path value |
||
| 844 | */ |
||
| 845 | public function getSubProperty($path) |
||
| 846 | { |
||
| 847 | $object=$this; |
||
| 848 | foreach(explode('.',$path) as $property) |
||
| 849 | $object=$object->$property; |
||
| 850 | return $object; |
||
| 851 | } |
||
| 852 | |||
| 853 | /** |
||
| 854 | * Sets a value to a property path. |
||
| 855 | * A property path is a sequence of property names concatenated by '.' character. |
||
| 856 | * For example, 'Parent.Page' refers to the 'Page' property of the component's |
||
| 857 | * 'Parent' property value (which should be a component also). |
||
| 858 | * When a property is not defined by an object, this also loops through all |
||
| 859 | * active behaviors of the object. |
||
| 860 | * @param string property path |
||
| 861 | * @param mixed the property path value |
||
| 862 | */ |
||
| 863 | public function setSubProperty($path,$value) |
||
| 864 | { |
||
| 865 | $object=$this; |
||
| 866 | if(($pos=strrpos($path,'.'))===false) |
||
| 867 | $property=$path; |
||
| 868 | else |
||
| 869 | { |
||
| 870 | $object=$this->getSubProperty(substr($path,0,$pos)); |
||
| 871 | $property=substr($path,$pos+1); |
||
| 872 | } |
||
| 873 | $object->$property=$value; |
||
| 874 | } |
||
| 875 | |||
| 876 | /** |
||
| 877 | * Determines whether an event is defined. |
||
| 878 | * An event is defined if the class has a method whose name is the event name |
||
| 879 | * prefixed with 'on', 'fx', or 'dy'. |
||
| 880 | * Every object responds to every 'fx' and 'dy' event as they are in a universally |
||
| 881 | * accepted event space. 'on' event must be declared by the object. |
||
| 882 | * When enabled, this will loop through all active behaviors for 'on' events |
||
| 883 | * defined by the behavior. |
||
| 884 | * Note, event name is case-insensitive. |
||
| 885 | * @param string the event name |
||
| 886 | * @return boolean |
||
| 887 | */ |
||
| 888 | public function hasEvent($name) |
||
| 889 | { |
||
| 890 | if((strncasecmp($name,'on',2)===0&&method_exists($this,$name))||strncasecmp($name,'fx',2)===0||strncasecmp($name,'dy',2)===0) |
||
| 891 | return true; |
||
| 892 | |||
| 893 | else if($this->_m!==null&&$this->_behaviorsenabled) |
||
| 894 | { |
||
| 895 | foreach($this->_m->toArray() as $behavior) |
||
| 896 | { |
||
| 897 | if((!($behavior instanceof IBehavior)||$behavior->getEnabled())&&$behavior->hasEvent($name)) |
||
| 898 | return true; |
||
| 899 | } |
||
| 900 | } |
||
| 901 | return false; |
||
| 902 | } |
||
| 903 | |||
| 904 | /** |
||
| 905 | * Checks if an event has any handlers. This function also checks through all |
||
| 906 | * the behaviors for 'on' events when behaviors are enabled. |
||
| 907 | * 'dy' dynamic events are not handled by this function. |
||
| 908 | * @param string the event name |
||
| 909 | * @return boolean whether an event has been attached one or several handlers |
||
| 910 | */ |
||
| 911 | public function hasEventHandler($name) |
||
| 912 | { |
||
| 913 | $name=strtolower($name); |
||
| 914 | if(strncasecmp($name,'fx',2)===0) |
||
| 915 | return isset(self::$_ue[$name])&&self::$_ue[$name]->getCount()>0; |
||
| 916 | else |
||
| 917 | { |
||
| 918 | if(isset($this->_e[$name])&&$this->_e[$name]->getCount()>0) |
||
| 919 | return true; |
||
| 920 | else if($this->_m!==null&&$this->_behaviorsenabled) { |
||
| 921 | foreach($this->_m->toArray() as $behavior) |
||
| 922 | { |
||
| 923 | if((!($behavior instanceof IBehavior)||$behavior->getEnabled())&&$behavior->hasEventHandler($name)) |
||
| 924 | return true; |
||
| 925 | } |
||
| 926 | } |
||
| 927 | } |
||
| 928 | return false; |
||
| 929 | } |
||
| 930 | |||
| 931 | /** |
||
| 932 | * Returns the list of attached event handlers for an 'on' or 'fx' event. This function also |
||
| 933 | * checks through all the behaviors for 'on' event lists when behaviors are enabled. |
||
| 934 | * @return TPriorityList list of attached event handlers for an event |
||
| 935 | * @throws TInvalidOperationException if the event is not defined |
||
| 936 | */ |
||
| 937 | public function getEventHandlers($name) |
||
| 938 | { |
||
| 939 | if(strncasecmp($name,'on',2)===0&&method_exists($this,$name)) |
||
| 940 | { |
||
| 941 | $name=strtolower($name); |
||
| 942 | if(!isset($this->_e[$name])) |
||
| 943 | $this->_e[$name]=new TPriorityList; |
||
| 944 | return $this->_e[$name]; |
||
| 945 | } |
||
| 946 | else if(strncasecmp($name,'fx',2)===0) |
||
| 947 | { |
||
| 948 | $name=strtolower($name); |
||
| 949 | if(!isset(self::$_ue[$name])) |
||
| 950 | self::$_ue[$name]=new TPriorityList; |
||
| 951 | return self::$_ue[$name]; |
||
| 952 | } |
||
| 953 | else if($this->_m!==null&&$this->_behaviorsenabled) |
||
| 954 | { |
||
| 955 | foreach($this->_m->toArray() as $behavior) |
||
| 956 | { |
||
| 957 | if((!($behavior instanceof IBehavior)||$behavior->getEnabled())&&$behavior->hasEvent($name)) |
||
| 958 | return $behavior->getEventHandlers($name); |
||
| 959 | } |
||
| 960 | } |
||
| 961 | throw new TInvalidOperationException('component_event_undefined',get_class($this),$name); |
||
| 962 | } |
||
| 963 | |||
| 964 | /** |
||
| 965 | * Attaches an event handler to an event. |
||
| 966 | * |
||
| 967 | * The handler must be a valid PHP callback, i.e., a string referring to |
||
| 968 | * a global function name, or an array containing two elements with |
||
| 969 | * the first element being an object and the second element a method name |
||
| 970 | * of the object. In Prado, you can also use method path to refer to |
||
| 971 | * an event handler. For example, array($object,'Parent.buttonClicked') |
||
| 972 | * uses a method path that refers to the method $object->Parent->buttonClicked(...). |
||
| 973 | * |
||
| 974 | * The event handler must be of the following signature, |
||
| 975 | * <code> |
||
| 976 | * function handlerName($sender, $param) {} |
||
| 977 | * function handlerName($sender, $param, $name) {} |
||
| 978 | * </code> |
||
| 979 | * where $sender represents the object that raises the event, |
||
| 980 | * and $param is the event parameter. $name refers to the event name |
||
| 981 | * being handled. |
||
| 982 | * |
||
| 983 | * This is a convenient method to add an event handler. |
||
| 984 | * It is equivalent to {@link getEventHandlers}($name)->add($handler). |
||
| 985 | * For complete management of event handlers, use {@link getEventHandlers} |
||
| 986 | * to get the event handler list first, and then do various |
||
| 987 | * {@link TPriorityList} operations to append, insert or remove |
||
| 988 | * event handlers. You may also do these operations like |
||
| 989 | * getting and setting properties, e.g., |
||
| 990 | * <code> |
||
| 991 | * $component->OnClick[]=array($object,'buttonClicked'); |
||
| 992 | * $component->OnClick->insertAt(0,array($object,'buttonClicked')); |
||
| 993 | * </code> |
||
| 994 | * which are equivalent to the following |
||
| 995 | * <code> |
||
| 996 | * $component->getEventHandlers('OnClick')->add(array($object,'buttonClicked')); |
||
| 997 | * $component->getEventHandlers('OnClick')->insertAt(0,array($object,'buttonClicked')); |
||
| 998 | * </code> |
||
| 999 | * |
||
| 1000 | * Due to the nature of {@link getEventHandlers}, any active behaviors defining |
||
| 1001 | * new 'on' events, this method will pass through to the behavior transparently. |
||
| 1002 | * |
||
| 1003 | * @param string the event name |
||
| 1004 | * @param callback the event handler |
||
| 1005 | * @param numeric|null the priority of the handler, defaults to null which translates into the |
||
| 1006 | * default priority of 10.0 within {@link TPriorityList} |
||
| 1007 | * @throws TInvalidOperationException if the event does not exist |
||
| 1008 | */ |
||
| 1009 | public function attachEventHandler($name,$handler,$priority=null) |
||
| 1010 | { |
||
| 1011 | $this->getEventHandlers($name)->add($handler,$priority); |
||
| 1012 | } |
||
| 1013 | |||
| 1014 | /** |
||
| 1015 | * Detaches an existing event handler. |
||
| 1016 | * This method is the opposite of {@link attachEventHandler}. It will detach |
||
| 1017 | * any 'on' events definedb by an objects active behaviors as well. |
||
| 1018 | * @param string event name |
||
| 1019 | * @param callback the event handler to be removed |
||
| 1020 | * @param numeric|false|null the priority of the handler, defaults to false which translates |
||
| 1021 | * to an item of any priority within {@link TPriorityList}; null means the default priority |
||
| 1022 | * @return boolean if the removal is successful |
||
| 1023 | */ |
||
| 1024 | public function detachEventHandler($name,$handler,$priority=false) |
||
| 1025 | { |
||
| 1026 | if($this->hasEventHandler($name)) |
||
| 1027 | { |
||
| 1028 | try |
||
| 1029 | { |
||
| 1030 | $this->getEventHandlers($name)->remove($handler,$priority); |
||
| 1031 | return true; |
||
| 1032 | } |
||
| 1033 | catch(Exception $e) |
||
| 1034 | { |
||
| 1035 | } |
||
| 1036 | } |
||
| 1037 | return false; |
||
| 1038 | } |
||
| 1039 | |||
| 1040 | /** |
||
| 1041 | * Raises an event. This raises both inter-object 'on' events and global 'fx' events. |
||
| 1042 | * This method represents the happening of an event and will |
||
| 1043 | * invoke all attached event handlers for the event in {@link TPriorityList} order. |
||
| 1044 | * This method does not handle intra-object/behavior dynamic 'dy' events. |
||
| 1045 | * |
||
| 1046 | * There are ways to handle event responses. By defailt {@link EVENT_RESULT_FILTER}, |
||
| 1047 | * all event responses are stored in an array, filtered for null responses, and returned. |
||
| 1048 | * If {@link EVENT_RESULT_ALL} is specified, all returned results will be stored along |
||
| 1049 | * with the sender and param in an array |
||
| 1050 | * <code> |
||
| 1051 | * $result[] = array('sender'=>$sender,'param'=>$param,'response'=>$response); |
||
| 1052 | * </code> |
||
| 1053 | * |
||
| 1054 | * If {@link EVENT_RESULT_FEED_FORWARD} is specified, then each handler result is then |
||
| 1055 | * fed forward as the parameters for the next event. This allows for events to filter data |
||
| 1056 | * directly by affecting the event parameters |
||
| 1057 | * |
||
| 1058 | * If a callable function is set in the response type or the post function filter is specified then the |
||
| 1059 | * result of each called event handler is post processed by the callable function. Used in |
||
| 1060 | * combination with {@link EVENT_RESULT_FEED_FORWARD}, any event (and its result) can be chained. |
||
| 1061 | * |
||
| 1062 | * When raising a global 'fx' event, registered handlers in the global event list for |
||
| 1063 | * {@link GLOBAL_RAISE_EVENT_LISTENER} are always added into the set of event handlers. In this way, |
||
| 1064 | * these global events are always raised for every global 'fx' event. The registered handlers for global |
||
| 1065 | * raiseEvent events have priorities. Any registered global raiseEvent event handlers with a priority less than zero |
||
| 1066 | * are added before the main event handlers being raised and any registered global raiseEvent event handlers |
||
| 1067 | * with a priority equal or greater than zero are added after the main event handlers being raised. In this way |
||
| 1068 | * all {@link GLOBAL_RAISE_EVENT_LISTENER} handlers are always called for every raised 'fx' event. |
||
| 1069 | * |
||
| 1070 | * Behaviors may implement the following functions: |
||
| 1071 | * <code> |
||
| 1072 | * public function dyPreRaiseEvent($name,$sender,$param,$responsetype,$postfunction[, $chain]) { |
||
| 1073 | * return $name; //eg, the event name may be filtered/changed |
||
| 1074 | * } |
||
| 1075 | * public function dyIntraRaiseEventTestHandler($handler,$sender,$param,$name[, $chain]) { |
||
| 1076 | * return true; //should this particular handler be executed? true/false |
||
| 1077 | * } |
||
| 1078 | * public function dyIntraRaiseEventPostHandler($name,$sender,$param,$handler,$response[, $chain]) { |
||
| 1079 | * //contains the per handler response |
||
| 1080 | * } |
||
| 1081 | * public function dyPostRaiseEvent($responses,$name,$sender,$param,$responsetype,$postfunction[, $chain]) { |
||
| 1082 | * return $responses; |
||
| 1083 | * } |
||
| 1084 | * </code> |
||
| 1085 | * to be executed when raiseEvent is called. The 'intra' dynamic events are called per handler in |
||
| 1086 | * the handler loop. |
||
| 1087 | * |
||
| 1088 | * dyPreRaiseEvent has the effect of being able to change the event being raised. This intra |
||
| 1089 | * object/behavior event returns the name of the desired event to be raised. It will pass through |
||
| 1090 | * if no dynamic event is specified, or if the original event name is returned. |
||
| 1091 | * dyIntraRaiseEventTestHandler returns true or false as to whether a specific handler should be |
||
| 1092 | * called for a specific raised event (and associated event arguments) |
||
| 1093 | * dyIntraRaiseEventPostHandler does not return anything. This allows behaviors to access the results |
||
| 1094 | * of an event handler in the per handler loop. |
||
| 1095 | * dyPostRaiseEvent returns the responses. This allows for any post processing of the event |
||
| 1096 | * results from the sum of all event handlers |
||
| 1097 | * |
||
| 1098 | * When handling a catch-all {@link __dycall}, the method name is the name of the event |
||
| 1099 | * and the parameters are the sender, the param, and then the name of the event. |
||
| 1100 | * |
||
| 1101 | * @param string the event name |
||
| 1102 | * @param mixed the event sender object |
||
| 1103 | * @param TEventParameter the event parameter |
||
| 1104 | * @param numeric how the results of the event are tabulated. default: {@link EVENT_RESULT_FILTER} The default filters out |
||
| 1105 | * null responses. optional |
||
| 1106 | * @param function any per handler filtering of the response result needed is passed through |
||
| 1107 | * this if not null. default: null. optional |
||
| 1108 | * @return mixed the results of the event |
||
| 1109 | * @throws TInvalidOperationException if the event is undefined |
||
| 1110 | * @throws TInvalidDataValueException If an event handler is invalid |
||
| 1111 | */ |
||
| 1112 | public function raiseEvent($name,$sender,$param,$responsetype=null,$postfunction=null) |
||
| 1113 | { |
||
| 1114 | $p=$param; |
||
| 1115 | if(is_callable($responsetype)) |
||
| 1116 | { |
||
| 1117 | $postfunction=$responsetype; |
||
| 1118 | $responsetype=null; |
||
| 1119 | } |
||
| 1120 | |||
| 1121 | if($responsetype===null) |
||
| 1122 | $responsetype=TEventResults::EVENT_RESULT_FILTER; |
||
| 1123 | |||
| 1124 | $name=strtolower($name); |
||
| 1125 | $responses=array(); |
||
| 1126 | |||
| 1127 | $name=$this->dyPreRaiseEvent($name,$sender,$param,$responsetype,$postfunction); |
||
| 1128 | |||
| 1129 | if($this->hasEventHandler($name)||$this->hasEventHandler(TComponent::GLOBAL_RAISE_EVENT_LISTENER)) |
||
| 1130 | { |
||
| 1131 | $handlers=$this->getEventHandlers($name); |
||
| 1132 | $handlerArray=$handlers->toArray(); |
||
| 1133 | if(strncasecmp($name,'fx',2)===0&&$this->hasEventHandler(TComponent::GLOBAL_RAISE_EVENT_LISTENER)) |
||
| 1134 | { |
||
| 1135 | $globalhandlers=$this->getEventHandlers(TComponent::GLOBAL_RAISE_EVENT_LISTENER); |
||
| 1136 | $handlerArray=array_merge($globalhandlers->toArrayBelowPriority(0),$handlerArray,$globalhandlers->toArrayAbovePriority(0)); |
||
| 1137 | } |
||
| 1138 | $response=null; |
||
| 1139 | foreach($handlerArray as $handler) |
||
| 1140 | { |
||
| 1141 | if($this->dyIntraRaiseEventTestHandler($handler,$sender,$param,$name)===false) |
||
| 1142 | continue; |
||
| 1143 | |||
| 1144 | if(is_string($handler)) |
||
| 1145 | { |
||
| 1146 | if(($pos=strrpos($handler,'.'))!==false) |
||
| 1147 | { |
||
| 1148 | $object=$this->getSubProperty(substr($handler,0,$pos)); |
||
| 1149 | $method=substr($handler,$pos+1); |
||
| 1150 | if(method_exists($object,$method)||strncasecmp($method,'dy',2)===0||strncasecmp($method,'fx',2)===0) |
||
| 1151 | { |
||
| 1152 | if($method=='__dycall') |
||
| 1153 | $response=$object->__dycall($name,array($sender,$param,$name)); |
||
| 1154 | else |
||
| 1155 | $response=$object->$method($sender,$param,$name); |
||
| 1156 | } |
||
| 1157 | else |
||
| 1158 | throw new TInvalidDataValueException('component_eventhandler_invalid',get_class($this),$name,$handler); |
||
| 1159 | } |
||
| 1160 | else |
||
| 1161 | $response=call_user_func($handler,$sender,$param,$name); |
||
| 1162 | } |
||
| 1163 | else if(is_callable($handler,true)) |
||
| 1164 | { |
||
| 1165 | list($object,$method)=$handler; |
||
| 1166 | if(is_string($object)) |
||
| 1167 | $response=call_user_func($handler,$sender,$param,$name); |
||
| 1168 | else |
||
| 1169 | { |
||
| 1170 | if(($pos=strrpos($method,'.'))!==false) |
||
| 1171 | { |
||
| 1172 | $object=$this->getSubProperty(substr($method,0,$pos)); |
||
| 1173 | $method=substr($method,$pos+1); |
||
| 1174 | } |
||
| 1175 | if(method_exists($object,$method)||strncasecmp($method,'dy',2)===0||strncasecmp($method,'fx',2)===0) |
||
| 1176 | { |
||
| 1177 | if($method=='__dycall') |
||
| 1178 | $response=$object->__dycall($name,array($sender,$param,$name)); |
||
| 1179 | else |
||
| 1180 | $response=$object->$method($sender,$param,$name); |
||
| 1181 | } |
||
| 1182 | else |
||
| 1183 | throw new TInvalidDataValueException('component_eventhandler_invalid',get_class($this),$name,$handler[1]); |
||
| 1184 | } |
||
| 1185 | } |
||
| 1186 | else |
||
| 1187 | throw new TInvalidDataValueException('component_eventhandler_invalid',get_class($this),$name,gettype($handler)); |
||
| 1188 | |||
| 1189 | $this->dyIntraRaiseEventPostHandler($name,$sender,$param,$handler,$response); |
||
| 1190 | |||
| 1191 | if($postfunction) |
||
| 1192 | $response=call_user_func_array($postfunction,array($sender,$param,$this,$response)); |
||
| 1193 | |||
| 1194 | if($responsetype&TEventResults::EVENT_RESULT_ALL) |
||
| 1195 | $responses[]=array('sender'=>$sender,'param'=>$param,'response'=>$response); |
||
| 1196 | else |
||
| 1197 | $responses[]=$response; |
||
| 1198 | |||
| 1199 | if($response!==null&&($responsetype&TEventResults::EVENT_RESULT_FEED_FORWARD)) |
||
| 1200 | $param=$response; |
||
| 1201 | |||
| 1202 | } |
||
| 1203 | } |
||
| 1204 | else if(strncasecmp($name,'on',2)===0&&!$this->hasEvent($name)) |
||
| 1205 | throw new TInvalidOperationException('component_event_undefined',get_class($this),$name); |
||
| 1206 | |||
| 1207 | if($responsetype&TEventResults::EVENT_RESULT_FILTER) |
||
| 1208 | $responses=array_filter($responses); |
||
| 1209 | |||
| 1210 | $responses=$this->dyPostRaiseEvent($responses,$name,$sender,$param,$responsetype,$postfunction); |
||
| 1211 | |||
| 1212 | return $responses; |
||
| 1213 | } |
||
| 1214 | |||
| 1215 | /** |
||
| 1216 | * Evaluates a PHP expression in the context of this control. |
||
| 1217 | * |
||
| 1218 | * Behaviors may implement the function: |
||
| 1219 | * <code> |
||
| 1220 | * public function dyEvaluateExpressionFilter($expression, $chain) { |
||
| 1221 | * return $chain->dyEvaluateExpressionFilter(str_replace('foo', 'bar', $expression)); //example |
||
| 1222 | * } |
||
| 1223 | * </code> |
||
| 1224 | * to be executed when evaluateExpression is called. All attached behaviors are notified through |
||
| 1225 | * dyEvaluateExpressionFilter. The chaining is important in this function due to the filtering |
||
| 1226 | * pass-through effect. |
||
| 1227 | * |
||
| 1228 | * @param string PHP expression |
||
| 1229 | * @return mixed the expression result |
||
| 1230 | * @throws TInvalidOperationException if the expression is invalid |
||
| 1231 | */ |
||
| 1232 | public function evaluateExpression($expression) |
||
| 1233 | { |
||
| 1234 | $expression=$this->dyEvaluateExpressionFilter($expression); |
||
| 1235 | try |
||
| 1236 | { |
||
| 1237 | if(eval("\$result=$expression;")===false) |
||
| 1238 | throw new Exception(''); |
||
| 1239 | return $result; |
||
| 1240 | } |
||
| 1241 | catch(Exception $e) |
||
| 1242 | { |
||
| 1243 | throw new TInvalidOperationException('component_expression_invalid',get_class($this),$expression,$e->getMessage()); |
||
| 1244 | } |
||
| 1245 | } |
||
| 1246 | |||
| 1247 | /** |
||
| 1248 | * Evaluates a list of PHP statements. |
||
| 1249 | * |
||
| 1250 | * Behaviors may implement the function: |
||
| 1251 | * <code> |
||
| 1252 | * public function dyEvaluateStatementsFilter($statements, $chain) { |
||
| 1253 | * return $chain->dyEvaluateStatementsFilter(str_replace('foo', 'bar', $statements)); //example |
||
| 1254 | * } |
||
| 1255 | * </code> |
||
| 1256 | * to be executed when evaluateStatements is called. All attached behaviors are notified through |
||
| 1257 | * dyEvaluateStatementsFilter. The chaining is important in this function due to the filtering |
||
| 1258 | * pass-through effect. |
||
| 1259 | * |
||
| 1260 | * @param string PHP statements |
||
| 1261 | * @return string content echoed or printed by the PHP statements |
||
| 1262 | * @throws TInvalidOperationException if the statements are invalid |
||
| 1263 | */ |
||
| 1264 | public function evaluateStatements($statements) |
||
| 1265 | { |
||
| 1266 | $statements=$this->dyEvaluateStatementsFilter($statements); |
||
| 1267 | try |
||
| 1268 | { |
||
| 1269 | ob_start(); |
||
| 1270 | if(eval($statements)===false) |
||
| 1271 | throw new Exception(''); |
||
| 1272 | $content=ob_get_contents(); |
||
| 1273 | ob_end_clean(); |
||
| 1274 | return $content; |
||
| 1275 | } |
||
| 1276 | catch(Exception $e) |
||
| 1277 | { |
||
| 1278 | throw new TInvalidOperationException('component_statements_invalid',get_class($this),$statements,$e->getMessage()); |
||
| 1279 | } |
||
| 1280 | } |
||
| 1281 | |||
| 1282 | /** |
||
| 1283 | * This method is invoked after the component is instantiated by a template. |
||
| 1284 | * When this method is invoked, the component's properties have been initialized. |
||
| 1285 | * The default implementation of this method will invoke |
||
| 1286 | * the potential parent component's {@link addParsedObject}. |
||
| 1287 | * This method can be overridden. |
||
| 1288 | * |
||
| 1289 | * Behaviors may implement the function: |
||
| 1290 | * <code> |
||
| 1291 | * public function dyCreatedOnTemplate($parent, $chain) { |
||
| 1292 | * return $chain->dyCreatedOnTemplate($parent); //example |
||
| 1293 | * } |
||
| 1294 | * </code> |
||
| 1295 | * to be executed when createdOnTemplate is called. All attached behaviors are notified through |
||
| 1296 | * dyCreatedOnTemplate. |
||
| 1297 | * |
||
| 1298 | * @param TComponent potential parent of this control |
||
| 1299 | * @see addParsedObject |
||
| 1300 | */ |
||
| 1301 | public function createdOnTemplate($parent) |
||
| 1302 | { |
||
| 1303 | $parent=$this->dyCreatedOnTemplate($parent); |
||
| 1304 | $parent->addParsedObject($this); |
||
| 1305 | } |
||
| 1306 | |||
| 1307 | /** |
||
| 1308 | * Processes an object that is created during parsing template. |
||
| 1309 | * The object can be either a component or a static text string. |
||
| 1310 | * This method can be overridden to customize the handling of newly created objects in template. |
||
| 1311 | * Only framework developers and control developers should use this method. |
||
| 1312 | * |
||
| 1313 | * Behaviors may implement the function: |
||
| 1314 | * <code> |
||
| 1315 | * public function dyAddParsedObject($object[, $chain]) { |
||
| 1316 | * } |
||
| 1317 | * </code> |
||
| 1318 | * to be executed when addParsedObject is called. All attached behaviors are notified through |
||
| 1319 | * dyAddParsedObject. |
||
| 1320 | * |
||
| 1321 | * @param string|TComponent text string or component parsed and instantiated in template |
||
| 1322 | * @see createdOnTemplate |
||
| 1323 | */ |
||
| 1324 | public function addParsedObject($object) |
||
| 1325 | { |
||
| 1326 | $this->dyAddParsedObject($object); |
||
| 1327 | } |
||
| 1328 | |||
| 1329 | |||
| 1330 | /** |
||
| 1331 | * This is the method registered for all instanced objects should a class behavior be added after |
||
| 1332 | * the class is instanced. Only when the class to which the behavior is being added is in this |
||
| 1333 | * object's class hierarchy, via {@link getClassHierarchy}, is the behavior added to this instance. |
||
| 1334 | * @param $sender the application |
||
| 1335 | * @param $param TClassBehaviorEventParameter |
||
| 1336 | * @since 3.2.3 |
||
| 1337 | */ |
||
| 1338 | public function fxAttachClassBehavior($sender,$param) { |
||
| 1339 | if(in_array($param->getClass(),$this->getClassHierarchy(true))) |
||
| 1340 | return $this->attachBehavior($param->getName(),$param->getBehavior(),$param->getPriority()); |
||
| 1341 | } |
||
| 1342 | |||
| 1343 | |||
| 1344 | /** |
||
| 1345 | * This is the method registered for all instanced objects should a class behavior be removed after |
||
| 1346 | * the class is instanced. Only when the class to which the behavior is being added is in this |
||
| 1347 | * object's class hierarchy, via {@link getClassHierarchy}, is the behavior removed from this instance. |
||
| 1348 | * @param $sender the application |
||
| 1349 | * @param $param TClassBehaviorEventParameter |
||
| 1350 | * @since 3.2.3 |
||
| 1351 | */ |
||
| 1352 | public function fxDetachClassBehavior($sender,$param) { |
||
| 1353 | if(in_array($param->getClass(),$this->getClassHierarchy(true))) |
||
| 1354 | return $this->detachBehavior($param->getName(),$param->getPriority()); |
||
| 1355 | } |
||
| 1356 | |||
| 1357 | |||
| 1358 | /** |
||
| 1359 | * This will add a class behavior to all classes instanced (that are listening) and future newly instanced objects. |
||
| 1360 | * This registers the behavior for future instances and pushes the changes to all the instances that are listening as well. |
||
| 1361 | * The universal class behaviors are stored in an inverted stack with the latest class behavior being at the first position in the array. |
||
| 1362 | * This is done so class behaviors are added last first. |
||
| 1363 | * @param string name the key of the class behavior |
||
| 1364 | * @param object|string class behavior or name of the object behavior per instance |
||
| 1365 | * @param string|class string of class or class on which to attach this behavior. Defaults to null which will error |
||
| 1366 | * but more important, if this is on PHP 5.3 it will use Late Static Binding to derive the class |
||
| 1367 | * it should extend. |
||
| 1368 | * <code> |
||
| 1369 | * TPanel::attachClassBehavior('javascripts', (new TJsPanelBehavior())->init($this)); |
||
| 1370 | * </code> |
||
| 1371 | * @param numeric|null priority of behavior, default: null the default priority of the {@link TPriorityList} Optional. |
||
| 1372 | * @throws TInvalidOperationException if the class behavior is being added to a {@link TComponent}; due to recursion. |
||
| 1373 | * @throws TInvalidOperationException if the class behavior is already defined |
||
| 1374 | * @since 3.2.3 |
||
| 1375 | */ |
||
| 1376 | public static function attachClassBehavior($name,$behavior,$class=null,$priority=null) { |
||
| 1377 | if(!$class&&function_exists('get_called_class')) |
||
| 1378 | $class=get_called_class(); |
||
| 1379 | if(!$class) |
||
| 1380 | throw new TInvalidOperationException('component_no_class_provided_nor_late_binding'); |
||
| 1381 | |||
| 1382 | if(!is_string($name)) |
||
| 1383 | $name=get_class($name); |
||
| 1384 | $class=strtolower($class); |
||
| 1385 | if($class==='tcomponent') |
||
| 1386 | throw new TInvalidOperationException('component_no_tcomponent_class_behaviors'); |
||
| 1387 | if(empty(self::$_um[$class])) |
||
| 1388 | self::$_um[$class]=array(); |
||
| 1389 | if(isset(self::$_um[$class][$name])) |
||
| 1390 | throw new TInvalidOperationException('component_class_behavior_defined',$class,$name); |
||
| 1391 | $param=new TClassBehaviorEventParameter($class,$name,$behavior,$priority); |
||
| 1392 | self::$_um[$class]=array($name=>$param)+self::$_um[$class]; |
||
| 1393 | $behaviorObject=is_string($behavior)?new $behavior:$behavior; |
||
| 1394 | return $behaviorObject->raiseEvent('fxAttachClassBehavior',null,$param); |
||
| 1395 | } |
||
| 1396 | |||
| 1397 | |||
| 1398 | /** |
||
| 1399 | * This will remove a behavior from a class. It unregisters it from future instances and |
||
| 1400 | * pulls the changes from all the instances that are listening as well. |
||
| 1401 | * PHP 5.3 uses Late Static Binding to derive the static class upon which this method is called. |
||
| 1402 | * @param $name the key of the class behavior |
||
| 1403 | * @param $class string class on which to attach this behavior. Defaults to null. |
||
| 1404 | * @param $priority numeric|null|false priority. false is any priority, null is default |
||
| 1405 | * {@link TPriorityList} priority, and numeric is a specific priority. |
||
| 1406 | * @throws Exception if the the class cannot be derived from Late Static Binding and is not |
||
| 1407 | * not supplied as a parameter. |
||
| 1408 | * @since 3.2.3 |
||
| 1409 | */ |
||
| 1410 | public static function detachClassBehavior($name,$class=null,$priority=false) { |
||
| 1411 | if(!$class&&function_exists('get_called_class')) |
||
| 1412 | $class=get_called_class(); |
||
| 1413 | if(!$class) |
||
| 1414 | throw new TInvalidOperationException('component_no_class_provided_nor_late_binding'); |
||
| 1415 | |||
| 1416 | $class=strtolower($class); |
||
| 1417 | if(!is_string($name)) |
||
| 1418 | $name=get_class($name); |
||
| 1419 | if(empty(self::$_um[$class])||!isset(self::$_um[$class][$name])) |
||
| 1420 | return false; |
||
| 1421 | $param=self::$_um[$class][$name]; |
||
| 1422 | $behavior=$param->getBehavior(); |
||
| 1423 | unset(self::$_um[$class][$name]); |
||
| 1424 | $behaviorObject=is_string($behavior)?new $behavior:$behavior; |
||
| 1425 | return $behaviorObject->raiseEvent('fxDetachClassBehavior',null,$param); |
||
| 1426 | } |
||
| 1427 | |||
| 1428 | /** |
||
| 1429 | * Returns the named behavior object. |
||
| 1430 | * The name 'asa' stands for 'as a'. |
||
| 1431 | * @param string the behavior name |
||
| 1432 | * @return IBehavior the behavior object, or null if the behavior does not exist |
||
| 1433 | * @since 3.2.3 |
||
| 1434 | */ |
||
| 1435 | public function asa($behaviorname) |
||
| 1436 | { |
||
| 1437 | return isset($this->_m[$behaviorname])?$this->_m[$behaviorname]:null; |
||
| 1438 | } |
||
| 1439 | |||
| 1440 | /** |
||
| 1441 | * Returns whether or not the object or any of the behaviors are of a particular class. |
||
| 1442 | * The name 'isa' stands for 'is a'. This first checks if $this is an instanceof the class. |
||
| 1443 | * It then checks each Behavior. If a behavior implements {@link IInstanceCheck}, |
||
| 1444 | * then the behavior can determine what it is an instanceof. If this behavior function returns true, |
||
| 1445 | * then this method returns true. If the behavior instance checking function returns false, |
||
| 1446 | * then no further checking is performed as it is assumed to be correct. |
||
| 1447 | * |
||
| 1448 | * If the behavior instance check function returns nothing or null or the behavior |
||
| 1449 | * doesn't implement the {@link IInstanceCheck} interface, then the default instanceof occurs. |
||
| 1450 | * The default isa behavior is to check if the behavior is an instanceof the class. |
||
| 1451 | * |
||
| 1452 | * The behavior {@link IInstanceCheck} is to allow a behavior to have the host object |
||
| 1453 | * act as a completely different object. |
||
| 1454 | * |
||
| 1455 | * @param class or string |
||
| 1456 | * @return boolean whether or not the object or a behavior is an instance of a particular class |
||
| 1457 | * @since 3.2.3 |
||
| 1458 | */ |
||
| 1459 | public function isa($class) |
||
| 1460 | { |
||
| 1461 | if($this instanceof $class) |
||
| 1462 | return true; |
||
| 1463 | if($this->_m!==null&&$this->_behaviorsenabled) |
||
| 1464 | foreach($this->_m->toArray() as $behavior){ |
||
| 1465 | if(($behavior instanceof IBehavior)&&!$behavior->getEnabled()) |
||
| 1466 | continue; |
||
| 1467 | |||
| 1468 | $check = null; |
||
| 1469 | if(($behavior->isa('IInstanceCheck'))&&$check=$behavior->isinstanceof($class,$this)) |
||
| 1470 | return true; |
||
| 1471 | if($check===null&&($behavior->isa($class))) |
||
| 1472 | return true; |
||
| 1473 | } |
||
| 1474 | return false; |
||
| 1475 | } |
||
| 1476 | |||
| 1477 | /** |
||
| 1478 | * Attaches a list of behaviors to the component. |
||
| 1479 | * Each behavior is indexed by its name and should be an instance of |
||
| 1480 | * {@link IBehavior}, a string specifying the behavior class, or a |
||
| 1481 | * {@link TClassBehaviorEventParameter}. |
||
| 1482 | * @param array list of behaviors to be attached to the component |
||
| 1483 | * @since 3.2.3 |
||
| 1484 | */ |
||
| 1485 | public function attachBehaviors($behaviors) |
||
| 1486 | { |
||
| 1487 | foreach($behaviors as $name=>$behavior) |
||
| 1488 | if($behavior instanceof TClassBehaviorEventParameter) |
||
| 1489 | $this->attachBehavior($behavior->getName(),$behavior->getBehavior(),$behavior->getPriority()); |
||
| 1490 | else |
||
| 1491 | $this->attachBehavior($name,$behavior); |
||
| 1492 | } |
||
| 1493 | |||
| 1494 | /** |
||
| 1495 | * Detaches select behaviors from the component. |
||
| 1496 | * Each behavior is indexed by its name and should be an instance of |
||
| 1497 | * {@link IBehavior}, a string specifying the behavior class, or a |
||
| 1498 | * {@link TClassBehaviorEventParameter}. |
||
| 1499 | * @param array list of behaviors to be detached from the component |
||
| 1500 | * @since 3.2.3 |
||
| 1501 | */ |
||
| 1502 | public function detachBehaviors($behaviors) |
||
| 1503 | { |
||
| 1504 | if($this->_m!==null) |
||
| 1505 | { |
||
| 1506 | foreach($behaviors as $name=>$behavior) |
||
| 1507 | if($behavior instanceof TClassBehaviorEventParameter) |
||
| 1508 | $this->detachBehavior($behavior->getName(),$behavior->getPriority()); |
||
| 1509 | else |
||
| 1510 | $this->detachBehavior(is_string($behavior)?$behavior:$name); |
||
| 1511 | } |
||
| 1512 | } |
||
| 1513 | |||
| 1514 | /** |
||
| 1515 | * Detaches all behaviors from the component. |
||
| 1516 | * @since 3.2.3 |
||
| 1517 | */ |
||
| 1518 | public function clearBehaviors() |
||
| 1519 | { |
||
| 1520 | if($this->_m!==null) |
||
| 1521 | { |
||
| 1522 | foreach($this->_m->toArray() as $name=>$behavior) |
||
| 1523 | $this->detachBehavior($name); |
||
| 1524 | $this->_m=null; |
||
| 1525 | } |
||
| 1526 | } |
||
| 1527 | |||
| 1528 | /** |
||
| 1529 | * Attaches a behavior to this component. |
||
| 1530 | * This method will create the behavior object based on the given |
||
| 1531 | * configuration. After that, the behavior object will be initialized |
||
| 1532 | * by calling its {@link IBehavior::attach} method. |
||
| 1533 | * |
||
| 1534 | * Already attached behaviors may implement the function: |
||
| 1535 | * <code> |
||
| 1536 | * public function dyAttachBehavior($name,$behavior[, $chain]) { |
||
| 1537 | * } |
||
| 1538 | * </code> |
||
| 1539 | * to be executed when attachBehavior is called. All attached behaviors are notified through |
||
| 1540 | * dyAttachBehavior. |
||
| 1541 | * |
||
| 1542 | * @param string the behavior's name. It should uniquely identify this behavior. |
||
| 1543 | * @param mixed the behavior configuration. This is passed as the first |
||
| 1544 | * parameter to {@link YiiBase::createComponent} to create the behavior object. |
||
| 1545 | * @return IBehavior the behavior object |
||
| 1546 | * @since 3.2.3 |
||
| 1547 | */ |
||
| 1548 | public function attachBehavior($name,$behavior,$priority=null) |
||
| 1549 | { |
||
| 1550 | if(is_string($behavior)) |
||
| 1551 | $behavior=Prado::createComponent($behavior); |
||
| 1552 | if(!($behavior instanceof IBaseBehavior)) |
||
| 1553 | throw new TInvalidDataTypeException('component_not_a_behavior',get_class($behavior)); |
||
| 1554 | if($behavior instanceof IBehavior) |
||
| 1555 | $behavior->setEnabled(true); |
||
| 1556 | if($this->_m===null) |
||
| 1557 | $this->_m=new TPriorityMap; |
||
| 1558 | $behavior->attach($this); |
||
| 1559 | $this->dyAttachBehavior($name,$behavior); |
||
| 1560 | $this->_m->add($name,$behavior,$priority); |
||
| 1561 | return $behavior; |
||
| 1562 | } |
||
| 1563 | |||
| 1564 | /** |
||
| 1565 | * Detaches a behavior from the component. |
||
| 1566 | * The behavior's {@link IBehavior::detach} method will be invoked. |
||
| 1567 | * |
||
| 1568 | * Behaviors may implement the function: |
||
| 1569 | * <code> |
||
| 1570 | * public function dyDetachBehavior($name,$behavior[, $chain]) { |
||
| 1571 | * } |
||
| 1572 | * </code> |
||
| 1573 | * to be executed when detachBehavior is called. All attached behaviors are notified through |
||
| 1574 | * dyDetachBehavior. |
||
| 1575 | * |
||
| 1576 | * @param string the behavior's name. It uniquely identifies the behavior. |
||
| 1577 | * @param numeric the behavior's priority. This defaults to false, aka any priority. |
||
| 1578 | * @return IBehavior the detached behavior. Null if the behavior does not exist. |
||
| 1579 | * @since 3.2.3 |
||
| 1580 | */ |
||
| 1581 | public function detachBehavior($name,$priority=false) |
||
| 1582 | { |
||
| 1583 | if($this->_m!=null&&isset($this->_m[$name])) |
||
| 1584 | { |
||
| 1585 | $this->_m[$name]->detach($this); |
||
| 1586 | $behavior=$this->_m->itemAt($name); |
||
| 1587 | $this->_m->remove($name,$priority); |
||
| 1588 | $this->dyDetachBehavior($name,$behavior); |
||
| 1589 | return $behavior; |
||
| 1590 | } |
||
| 1591 | } |
||
| 1592 | |||
| 1593 | /** |
||
| 1594 | * Enables all behaviors attached to this component independent of the behaviors |
||
| 1595 | * |
||
| 1596 | * Behaviors may implement the function: |
||
| 1597 | * <code> |
||
| 1598 | * public function dyEnableBehaviors($name,$behavior[, $chain]) { |
||
| 1599 | * } |
||
| 1600 | * </code> |
||
| 1601 | * to be executed when enableBehaviors is called. All attached behaviors are notified through |
||
| 1602 | * dyEnableBehaviors. |
||
| 1603 | * @since 3.2.3 |
||
| 1604 | */ |
||
| 1605 | public function enableBehaviors() |
||
| 1606 | { |
||
| 1607 | if(!$this->_behaviorsenabled) |
||
| 1608 | { |
||
| 1609 | $this->_behaviorsenabled=true; |
||
| 1610 | $this->dyEnableBehaviors(); |
||
| 1611 | } |
||
| 1612 | } |
||
| 1613 | |||
| 1614 | /** |
||
| 1615 | * Disables all behaviors attached to this component independent of the behaviors |
||
| 1616 | * |
||
| 1617 | * Behaviors may implement the function: |
||
| 1618 | * <code> |
||
| 1619 | * public function dyDisableBehaviors($name,$behavior[, $chain]) { |
||
| 1620 | * } |
||
| 1621 | * </code> |
||
| 1622 | * to be executed when disableBehaviors is called. All attached behaviors are notified through |
||
| 1623 | * dyDisableBehaviors. |
||
| 1624 | * @since 3.2.3 |
||
| 1625 | */ |
||
| 1626 | public function disableBehaviors() |
||
| 1627 | { |
||
| 1628 | if($this->_behaviorsenabled) |
||
| 1629 | { |
||
| 1630 | $this->dyDisableBehaviors(); |
||
| 1631 | $this->_behaviorsenabled=false; |
||
| 1632 | } |
||
| 1633 | } |
||
| 1634 | |||
| 1635 | |||
| 1636 | /** |
||
| 1637 | * Returns if all the behaviors are turned on or off for the object. |
||
| 1638 | * @return boolean whether or not all behaviors are enabled (true) or not (false) |
||
| 1639 | * @since 3.2.3 |
||
| 1640 | */ |
||
| 1641 | public function getBehaviorsEnabled() |
||
| 1642 | { |
||
| 1643 | return $this->_behaviorsenabled; |
||
| 1644 | } |
||
| 1645 | |||
| 1646 | /** |
||
| 1647 | * Enables an attached object behavior. This cannot enable or disable whole class behaviors. |
||
| 1648 | * A behavior is only effective when it is enabled. |
||
| 1649 | * A behavior is enabled when first attached. |
||
| 1650 | * |
||
| 1651 | * Behaviors may implement the function: |
||
| 1652 | * <code> |
||
| 1653 | * public function dyEnableBehavior($name,$behavior[, $chain]) { |
||
| 1654 | * } |
||
| 1655 | * </code> |
||
| 1656 | * to be executed when enableBehavior is called. All attached behaviors are notified through |
||
| 1657 | * dyEnableBehavior. |
||
| 1658 | * |
||
| 1659 | * @param string the behavior's name. It uniquely identifies the behavior. |
||
| 1660 | * @since 3.2.3 |
||
| 1661 | */ |
||
| 1662 | public function enableBehavior($name) |
||
| 1663 | { |
||
| 1664 | if($this->_m!=null&&isset($this->_m[$name])){ |
||
| 1665 | if($this->_m[$name] instanceof IBehavior) { |
||
| 1666 | $this->_m[$name]->setEnabled(true); |
||
| 1667 | $this->dyEnableBehavior($name,$this->_m[$name]); |
||
| 1668 | return true; |
||
| 1669 | } |
||
| 1670 | return false; |
||
| 1671 | } |
||
| 1672 | return null; |
||
| 1673 | } |
||
| 1674 | |||
| 1675 | /** |
||
| 1676 | * Disables an attached behavior. This cannot enable or disable whole class behaviors. |
||
| 1677 | * A behavior is only effective when it is enabled. |
||
| 1678 | * |
||
| 1679 | * Behaviors may implement the function: |
||
| 1680 | * <code> |
||
| 1681 | * public function dyDisableBehavior($name,$behavior[, $chain]) { |
||
| 1682 | * } |
||
| 1683 | * </code> |
||
| 1684 | * to be executed when disableBehavior is called. All attached behaviors are notified through |
||
| 1685 | * dyDisableBehavior. |
||
| 1686 | * |
||
| 1687 | * @param string the behavior's name. It uniquely identifies the behavior. |
||
| 1688 | * @since 3.2.3 |
||
| 1689 | */ |
||
| 1690 | public function disableBehavior($name) |
||
| 1691 | { |
||
| 1692 | if($this->_m!=null&&isset($this->_m[$name])){ |
||
| 1693 | if($this->_m[$name] instanceof IBehavior) { |
||
| 1694 | $this->_m[$name]->setEnabled(false); |
||
| 1695 | $this->dyDisableBehavior($name,$this->_m[$name]); |
||
| 1696 | return true; |
||
| 1697 | } |
||
| 1698 | return false; |
||
| 1699 | } |
||
| 1700 | return null; |
||
| 1701 | } |
||
| 1702 | |||
| 1703 | /** |
||
| 1704 | * Returns an array with the names of all variables of that object that should be serialized. |
||
| 1705 | * Do not call this method. This is a PHP magic method that will be called automatically |
||
| 1706 | * prior to any serialization. |
||
| 1707 | */ |
||
| 1708 | public function __sleep() |
||
| 1716 | |||
| 1717 | /** |
||
| 1718 | * Returns an array with the names of all variables of this object that should NOT be serialized |
||
| 1719 | * because their value is the default one or useless to be cached for the next page loads. |
||
| 1720 | * Reimplement in derived classes to add new variables, but remember to also to call the parent |
||
| 1721 | * implementation first. |
||
| 1722 | */ |
||
| 1723 | protected function _getZappableSleepProps(&$exprops) |
||
| 1724 | { |
||
| 1725 | if($this->_listeningenabled===false) |
||
| 1726 | $exprops[] = "\0TComponent\0_listeningenabled"; |
||
| 1727 | if($this->_behaviorsenabled===true) |
||
| 1728 | $exprops[] = "\0TComponent\0_behaviorsenabled"; |
||
| 1729 | if ($this->_e===array()) |
||
| 1730 | $exprops[] = "\0TComponent\0_e"; |
||
| 1731 | if ($this->_m===null) |
||
| 1732 | $exprops[] = "\0TComponent\0_m"; |
||
| 1733 | } |
||
| 1734 | } |
||
| 1735 | |||
| 1736 | |||
| 1737 | /** |
||
| 1738 | * IDynamicMethods interface. |
||
| 1739 | * IDynamicMethods marks an object to receive undefined global or dynamic events. |
||
| 1740 | * |
||
| 1741 | * @author Brad Anderson <[email protected]> |
||
| 1742 | * @version $Id$ |
||
| 1743 | * @package System |
||
| 1744 | * @since 3.2.3 |
||
| 1745 | */ |
||
| 1746 | interface IDynamicMethods |
||
| 1747 | { |
||
| 1748 | public function __dycall($method,$args); |
||
| 1749 | } |
||
| 1750 | |||
| 1751 | |||
| 1752 | |||
| 1753 | /** |
||
| 1754 | * TClassBehaviorEventParameter class. |
||
| 1755 | * TClassBehaviorEventParameter is the parameter sent with the class behavior changes. |
||
| 1756 | * |
||
| 1757 | * @author Brad Anderson <[email protected]> |
||
| 1758 | * @version $Id$ |
||
| 1759 | * @package System |
||
| 1760 | * @since 3.2.3 |
||
| 2410 |
This check looks for method names that are not written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes
databaseConnectionSeeker.