Show
Ignore:
Timestamp:
03/19/09 10:40:34 (3 years ago)
Author:
john
Message:

features and bug fixes from gocoffeego project

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/trax/vendor/trax/action_controller.php

    r300 r308  
    5656     *  @var string 
    5757     */ 
    58     private $controller; 
     58    protected $controller; 
    5959 
    6060    /** 
     
    6464     *  @var string 
    6565     */ 
    66     private $action; 
    67  
    68     /** 
    69      *  Value of :id parsed from URL then forced to lower case 
     66    protected $action; 
     67 
     68    /** 
     69     *  Path to add to other filesystem paths 
    7070     * 
    7171     *  Set by {@link recognize_route()} 
    7272     *  @var string 
    7373     */ 
    74     private $id; 
    75  
    76     /** 
    77      *  Path to add to other filesystem paths 
    78      * 
    79      *  Set by {@link recognize_route()} 
    80      *  @var string 
    81      */ 
    8274    private $added_path = ''; 
    83       
    84     /** 
    85      *  Parameters for the action routine 
    86      * 
    87      *  Set by {@link recognize_route()}, passed as arguments to the 
    88      *  controller's action routine. 
    89      *  @var string[] 
    90      */ 
    91     private $action_params = array(); 
    9275 
    9376    /** 
     
    201184     *  @var string[] 
    202185     */ 
    203     private $before_filters = array(); 
     186    public $before_filters = array(); 
     187 
     188    public $before_filter_options = array(); 
    204189 
    205190    /** 
     
    211196    private $after_filters = array();      
    212197 
     198    private $after_filter_options = array(); 
     199 
    213200    /** 
    214201     *  @todo Document this attribute 
     
    224211     *  @todo Document this attribute 
    225212     */ 
    226     protected $before_filter = null; 
     213    #protected $before_filter = null; 
    227214 
    228215    /** 
    229216     *  @todo Document this attribute 
    230217     */ 
    231     protected $after_filter = null; 
     218    #protected $after_filter = null; 
    232219 
    233220    /** 
     
    318305     */ 
    319306    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        #} 
    321310        if($key == "before_filter") { 
    322311            $this->add_before_filter($value); 
     312            unset($this->$key); 
    323313        } elseif($key == "after_filter") { 
    324314            $this->add_after_filter($value); 
     
    396386     *  @uses load_router() 
    397387     *  @uses $action 
    398      *  @uses $action_params 
    399388     *  @uses $application_controller_file 
    400389     *  @uses $controller 
     
    513502 
    514503                    //  ':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)]); 
    525516                } 
    526517                //  For historical reasons, continue to pass id 
    527518                //  in $_REQUEST 
    528                 if($this->id != "") { 
    529                     $_REQUEST['id'] = $this->id; 
     519                if($id != "") { 
     520                    $_REQUEST['id'] = $id; 
    530521                } 
    531                 //error_log('id='.$this->id); 
     522                //error_log('id='.$id); 
    532523 
    533524                $this->views_path .= "/" . $this->controller; 
     
    551542     * 
    552543     *  @uses $action 
    553      *  @uses $action_params 
    554544     *  @uses $application_controller_file 
    555545     *  @uses $application_helper_file 
     
    584574        } 
    585575        //error_log('process_route(): controller="'.$this->controller 
    586         //          .'"  action="'.$this->action.'"  id="'.$this->id.'"'); 
     576        //          .'"  action="'.$this->action.'"'); 
    587577 
    588578        # Include main application controller file 
     
    662652                # Call the controller method based on the URL 
    663653                if($this->controller_object->execute_before_filters()) { 
    664                     $controller_layout = null; 
     654                    $controller_layout = null; 
    665655                    if(isset($this->controller_object->layout)) { 
    666656                        $controller_layout = $this->controller_object->layout;        
    667657                    } 
    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(); 
    676680                    } 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; 
    679683                    } 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(); 
    686689                    } else { 
    687690                        //error_log('no action'); 
     
    799802            } 
    800803            if(isset($extra_path) && is_array($extra_path)) { 
     804                $this->extra_path = $extra_path; 
    801805                $extra_path = implode("/", $extra_path); 
    802806                $this->added_path = $extra_path; 
     
    818822     */ 
    819823    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));        
    820830        $return = true; 
    821831        if(count($this->before_filters) > 0) {  
     832            $action = $this->action ? $this->action : "index"; 
    822833            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                } 
    823850                if(method_exists($this, $filter_function)) { 
    824851                    if(false === $this->$filter_function()) { 
     
    840867     *  @uses $before_filters 
    841868     */ 
    842     function add_before_filter($filter_function_name) { 
     869    function add_before_filter($filter_function_name, $options = array(), $prepend = false) { 
    843870        //error_log("adding before filter: $filter_function_name"); 
    844871        if(is_string($filter_function_name) && !empty($filter_function_name)) { 
    845872            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                } 
    847885            }                         
    848886        } 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    } 
    856909 
    857910    /** 
     
    860913     */ 
    861914    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"; 
    863924            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                } 
    864941                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                    } 
    866946                } 
    867947            } 
    868948        } 
    869     } 
    870  
     949        return $return; 
     950    } 
    871951 
    872952    /** 
     
    878958     *  @uses $after_filters 
    879959     */ 
    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"); 
    881962        if(is_string($filter_function_name) && !empty($filter_function_name)) { 
    882963            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            }                         
    885977        } 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    } 
    8931000 
    8941001    /**