Changeset 308 for trunk/trax/vendor/trax/action_controller.php
- Timestamp:
- 03/19/09 10:40:34 (3 years ago)
- Files:
-
- 1 modified
-
trunk/trax/vendor/trax/action_controller.php (modified) (16 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/trax/vendor/trax/action_controller.php
r300 r308 56 56 * @var string 57 57 */ 58 pr ivate$controller;58 protected $controller; 59 59 60 60 /** … … 64 64 * @var string 65 65 */ 66 pr ivate$action;67 68 /** 69 * Value of :id parsed from URL then forced to lower case66 protected $action; 67 68 /** 69 * Path to add to other filesystem paths 70 70 * 71 71 * Set by {@link recognize_route()} 72 72 * @var string 73 73 */ 74 private $id;75 76 /**77 * Path to add to other filesystem paths78 *79 * Set by {@link recognize_route()}80 * @var string81 */82 74 private $added_path = ''; 83 84 /**85 * Parameters for the action routine86 *87 * Set by {@link recognize_route()}, passed as arguments to the88 * controller's action routine.89 * @var string[]90 */91 private $action_params = array();92 75 93 76 /** … … 201 184 * @var string[] 202 185 */ 203 private $before_filters = array(); 186 public $before_filters = array(); 187 188 public $before_filter_options = array(); 204 189 205 190 /** … … 211 196 private $after_filters = array(); 212 197 198 private $after_filter_options = array(); 199 213 200 /** 214 201 * @todo Document this attribute … … 224 211 * @todo Document this attribute 225 212 */ 226 protected $before_filter = null;213 #protected $before_filter = null; 227 214 228 215 /** 229 216 * @todo Document this attribute 230 217 */ 231 protected $after_filter = null;218 #protected $after_filter = null; 232 219 233 220 /** … … 318 305 */ 319 306 function __set($key, $value) { 320 //error_log("__set($key, $value)"); 307 #if(is_string($value) || is_array($value)) { 308 #error_log("__set($key, $value)"); 309 #} 321 310 if($key == "before_filter") { 322 311 $this->add_before_filter($value); 312 unset($this->$key); 323 313 } elseif($key == "after_filter") { 324 314 $this->add_after_filter($value); … … 396 386 * @uses load_router() 397 387 * @uses $action 398 * @uses $action_params399 388 * @uses $application_controller_file 400 389 * @uses $controller … … 513 502 514 503 // ':id' in route params overrides URL 515 $this->id = $route_params[':id']; 516 } elseif(@in_array(":id",$route_path) 517 && array_key_exists(@array_search(":id", $route_path), 518 $this->url_path)) { 519 $this->id = strtolower($this->url_path[@array_search(":id", $route_path)]); 520 // Parameters for the action routine. 521 // FIXME: make more general than just id 522 if($this->id != "") { 523 $this->action_params['id'] = $this->id; 524 } 504 $id = $route_params[':id']; 505 } elseif(@in_array(":id",$route_path)) { 506 #print_r($this->url_path); 507 #print_r($route_path); 508 if(count($this->extra_path)) { 509 foreach(array_reverse($this->extra_path) as $extra_path) { 510 #array_unshift($this->url_path, $extra_path); 511 } 512 } 513 #print_r($this->url_path); 514 515 $id = strtolower($this->url_path[@array_search(":id", $route_path)]); 525 516 } 526 517 // For historical reasons, continue to pass id 527 518 // in $_REQUEST 528 if($ this->id != "") {529 $_REQUEST['id'] = $ this->id;519 if($id != "") { 520 $_REQUEST['id'] = $id; 530 521 } 531 //error_log('id='.$ this->id);522 //error_log('id='.$id); 532 523 533 524 $this->views_path .= "/" . $this->controller; … … 551 542 * 552 543 * @uses $action 553 * @uses $action_params554 544 * @uses $application_controller_file 555 545 * @uses $application_helper_file … … 584 574 } 585 575 //error_log('process_route(): controller="'.$this->controller 586 // .'" action="'.$this->action.'" id="'.$this->id.'"');576 // .'" action="'.$this->action.'"'); 587 577 588 578 # Include main application controller file … … 662 652 # Call the controller method based on the URL 663 653 if($this->controller_object->execute_before_filters()) { 664 $controller_layout = null;654 $controller_layout = null; 665 655 if(isset($this->controller_object->layout)) { 666 656 $controller_layout = $this->controller_object->layout; 667 657 } 668 if(method_exists($this->controller_object, $this->action)) { 669 //error_log('method '.$this->action.' exists, calling it'); 670 $action = $this->action; 671 //error_log('calling action routine ' 672 // . get_class($this->controller_object) 673 // .'::'.$action.'() with params ' 674 // .var_export($this->action_params,true)); 675 $this->controller_object->$action($this->action_params); 658 659 #Get PUBLIC methods from controller object 660 $all_methods = get_class_methods($this->controller_object); 661 662 # Get Inherited methods from active_controller 663 $inherited_methods = array_merge( 664 get_class_methods(__CLASS__), 665 $this->controller_object->before_filters, 666 $this->controller_object->after_filters 667 ); 668 669 # Get non-inherited methods 670 $action_methods = array_diff($all_methods, $inherited_methods); 671 #error_log("available methods:".print_r($action_methods, true)); 672 673 if(in_array($this->action, $action_methods)) { 674 #error_log('method '.$this->action.' exists, calling it'); 675 $action = $this->controller_object->called_action = $this->action; 676 #error_log('calling action routine ' 677 # . get_class($this->controller_object) 678 # .'::'.$action.'()'); 679 $this->controller_object->$action(); 676 680 } elseif(file_exists($this->views_path . "/" . $this->action . "." . Trax::$views_extension)) { 677 //error_log('views file "'.$this->action.'"');678 $action = $this-> action;681 #error_log('views file "'.$this->action.'"'); 682 $action = $this->controller_object->called_action = $this->action; 679 683 } elseif(method_exists($this->controller_object, "index")) { 680 //error_log('calling action routine ' 681 // . get_class($this->controller_object) 682 // .'::index() with params ' 683 // .var_export($this->action_params,true)); 684 $action = "index"; 685 $this->controller_object->index($this->action_params); 684 #error_log('calling action routine ' 685 # . get_class($this->controller_object) 686 # .'::index()'); 687 $action = $this->controller_object->called_action = "index"; 688 $this->controller_object->index(); 686 689 } else { 687 690 //error_log('no action'); … … 799 802 } 800 803 if(isset($extra_path) && is_array($extra_path)) { 804 $this->extra_path = $extra_path; 801 805 $extra_path = implode("/", $extra_path); 802 806 $this->added_path = $extra_path; … … 818 822 */ 819 823 function execute_before_filters() { 824 825 #if(isset($this->before_filter)) { 826 #$this->add_before_filter($this->before_filter); 827 #} 828 #error_log("before_filters:".print_r($this->before_filters, true)); 829 #error_log("before_filter_options:".print_r($this->before_filter_options, true)); 820 830 $return = true; 821 831 if(count($this->before_filters) > 0) { 832 $action = $this->action ? $this->action : "index"; 822 833 foreach($this->before_filters as $filter_function) { 834 if(array_key_exists($filter_function, $this->before_filter_options)) { 835 if(is_array($options = $this->before_filter_options[$filter_function])) { 836 if(array_key_exists('except', $options)) { 837 if(preg_match("/\b$action\b/", $options['except'])) { 838 #error_log("before filter except match: action:{$action} == ".$options['except']); 839 continue; 840 } 841 } 842 if(array_key_exists('only', $options)) { 843 if(!preg_match("/\b$action\b/", $options['only'])) { 844 #error_log("before filter only non match: action:{$action} == ".$options['only']); 845 continue; 846 } 847 } 848 } 849 } 823 850 if(method_exists($this, $filter_function)) { 824 851 if(false === $this->$filter_function()) { … … 840 867 * @uses $before_filters 841 868 */ 842 function add_before_filter($filter_function_name ) {869 function add_before_filter($filter_function_name, $options = array(), $prepend = false) { 843 870 //error_log("adding before filter: $filter_function_name"); 844 871 if(is_string($filter_function_name) && !empty($filter_function_name)) { 845 872 if(!in_array($filter_function_name, $this->before_filters)) { 846 $this->before_filters[] = $filter_function_name; 873 if($prepend) { 874 array_unshift($this->before_filters, $filter_function_name); 875 } else { 876 array_push($this->before_filters, $filter_function_name); 877 } 878 if(count($options)) { 879 if(count($this->before_filter_options[$filter_function_name])) { 880 $this->before_filter_options[$filter_function_name] = array_merge($this->before_filter_options[$filter_function_name], $options); 881 } else { 882 $this->before_filter_options[$filter_function_name] = $options; 883 } 884 } 847 885 } 848 886 } elseif(is_array($filter_function_name)) { 849 if(count($this->before_filters) > 0) { 850 $this->before_filters = array_merge($this->before_filters, $filter_function_name); 851 } else { 852 $this->before_filters = $filter_function_name; 853 } 854 } 855 } 887 foreach($filter_function_name as $filter_name => $options) { 888 if(!in_array($filter_name, $this->before_filters)) { 889 if($prepend) { 890 array_unshift($this->before_filters, $filter_name); 891 } else { 892 array_push($this->before_filters, $filter_name); 893 } 894 if(count($options)) { 895 if(count($this->before_filter_options[$filter_name])) { 896 $this->before_filter_options[$filter_name] = array_merge($this->before_filter_options[$filter_name], $options); 897 } else { 898 $this->before_filter_options[$filter_name] = $options; 899 } 900 } 901 } 902 } 903 } 904 } 905 906 function prepend_before_filter($filter_function_name, $options = array()) { 907 $this->add_before_filter($filter_function_name, $options, true); 908 } 856 909 857 910 /** … … 860 913 */ 861 914 function execute_after_filters() { 862 if(count($this->after_filters) > 0) { 915 916 #if(isset($this->after_filter)) { 917 #$this->add_after_filter($this->after_filter); 918 #} 919 #error_log("after_filters:".print_r($this->after_filters, true)); 920 #error_log("after_filter_options:".print_r($this->after_filter_options, true)); 921 $return = true; 922 if(count($this->after_filters) > 0) { 923 $action = $this->action ? $this->action : "index"; 863 924 foreach($this->after_filters as $filter_function) { 925 if(array_key_exists($filter_function, $this->after_filter_options)) { 926 if(is_array($options = $this->after_filter_options[$filter_function])) { 927 if(array_key_exists('except', $options)) { 928 if(preg_match("/\b$action\b/", $options['except'])) { 929 #error_log("after filter except match: action:{$action} == ".$options['except']); 930 continue; 931 } 932 } 933 if(array_key_exists('only', $options)) { 934 if(!preg_match("/\b$action\b/", $options['only'])) { 935 #error_log("after filter only non match: action:{$action} == ".$options['only']); 936 continue; 937 } 938 } 939 } 940 } 864 941 if(method_exists($this, $filter_function)) { 865 $this->$filter_function(); 942 if(false === $this->$filter_function()) { 943 //error_log("execute_after_filters(): returning false"); 944 $return = false; 945 } 866 946 } 867 947 } 868 948 } 869 }870 949 return $return; 950 } 871 951 872 952 /** … … 878 958 * @uses $after_filters 879 959 */ 880 function add_after_filter($filter_function_name) { 960 function add_after_filter($filter_function_name, $options = array(), $prepend = false) { 961 //error_log("adding after filter: $filter_function_name"); 881 962 if(is_string($filter_function_name) && !empty($filter_function_name)) { 882 963 if(!in_array($filter_function_name, $this->after_filters)) { 883 $this->after_filters[] = $filter_function_name; 884 } 964 if($prepend) { 965 array_unshift($this->after_filters, $filter_function_name); 966 } else { 967 array_push($this->after_filters, $filter_function_name); 968 } 969 if(count($options)) { 970 if(count($this->after_filter_options[$filter_function_name])) { 971 $this->after_filter_options[$filter_function_name] = array_merge($this->after_filter_options[$filter_function_name], $options); 972 } else { 973 $this->after_filter_options[$filter_function_name] = $options; 974 } 975 } 976 } 885 977 } elseif(is_array($filter_function_name)) { 886 if(count($this->after_filters) > 0) { 887 $this->after_filters = array_merge($this->after_filters, $filter_function_name); 888 } else { 889 $this->after_filters = $filter_function_name; 890 } 891 } 892 } 978 foreach($filter_function_name as $filter_name => $options) { 979 if(!in_array($filter_name, $this->after_filters)) { 980 if($prepend) { 981 array_unshift($this->after_filters, $filter_name); 982 } else { 983 array_push($this->after_filters, $filter_name); 984 } 985 if(count($options)) { 986 if(count($this->after_filter_options[$filter_name])) { 987 $this->after_filter_options[$filter_name] = array_merge($this->after_filter_options[$filter_name], $options); 988 } else { 989 $this->after_filter_options[$filter_name] = $options; 990 } 991 } 992 } 993 } 994 } 995 } 996 997 function prepend_after_filter($filter_function_name, $options = array()) { 998 $this->add_after_filter($filter_function_name, $options, true); 999 } 893 1000 894 1001 /**
