Show
Ignore:
Timestamp:
03/02/06 17:20:34 (6 years ago)
Author:
haas
Message:

Finish Router class doc, regression test

Files:
1 modified

Legend:

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

    r138 r161  
    3030 
    3131/** 
    32  * 
    33  *  @todo Document this class 
    34  *  @package PHPonTrax 
     32 *  Convert a URL to an action 
     33 *  @tutorial PHPonTrax/Router.cls 
    3534 */ 
    3635class Router { 
    3736 
     37    /** 
     38     *  Route table 
     39     * 
     40     *  For a description of the structure, see 
     41     *  {@tutorial PHPonTrax/Router.cls#table the Router tutorial}. 
     42     *  Routes are added by calling {@link connect()} and looked up 
     43     *  by calling {@link find_route()}. 
     44     *  <b>FIXME:</b> Should we have a Route class to describe an 
     45     *  entry in the route table? 
     46     *  @var string[][] 
     47     */ 
    3848    private $routes = array(); 
     49 
     50    /** 
     51     *  Last route found by a call to find_route() 
     52     *  @var string[] 
     53     */ 
    3954    private $selected_route = null; 
     55 
     56    /** 
     57     *  Default route path 
     58     * 
     59     *  This route path is added to the route table if the table is 
     60     *  empty when find_route() is called. 
     61     *  @var string constant 
     62     */ 
    4063    private $default_route_path = ":controller/:action/:id"; 
     64 
     65    /** 
     66     *  Count of the number of elements in $routes 
     67     *  @var integer 
     68     */ 
    4169    public $routes_count = 0; 
    4270 
    4371    /** 
    44      * 
    45      *  @todo Document this method 
     72     *  Accessor method to return contents of $selected_route 
     73     *  @return string[] Contents of $selected_route 
     74     *  @uses $selected_route 
    4675     */ 
    4776    function get_selected_route() { 
     
    5079 
    5180    /** 
     81     *  Accessor method to add a route to the route table 
    5282     * 
    53      *  @todo Document this method 
     83     *  The route is added to the end of 
     84     *  {@link $routes the route table}. If $params is not an array, 
     85     *  NULL is stored in the route parameter area. 
     86     *  @param string $path 
     87     *  @param mixed[] $params 
     88     *  @uses $routes 
     89     *  @uses $routes_count 
    5490     */ 
    5591    function connect($path, $params = null) { 
     
    6197 
    6298    /** 
     99     *  Find first route in route table with path that matches argument 
    63100     * 
    64      *  @todo Document this method 
     101     *  First, assure that the route table {@link $routes} has at 
     102     *  least one route by adding 
     103     *  {@link $default_route_path the default route} if the table is 
     104     *  empty.  Then search the table to find the first route in the 
     105     *  table whose path matches the argument $url. If $url is an 
     106     *  empty string, it matches a path that is an empty string. 
     107     *  Otherwise, try to match $url to the path part of the table 
     108     *  entry according to {@link http://www.php.net/manual/en/ref.pcre.php Perl regular expression} 
     109     *  rules.  Return the first matching route to the caller, and 
     110     *  also save a copy in {@link $selected_route}. 
     111     *  @param string $url 
     112     *  @uses build_route_regexp() 
     113     *  @uses $default_route_path 
     114     *  @uses $routes 
     115     *  @uses $routes_count 
     116     *  @uses $selected_route 
     117     *  @return string[] Selected route. Path is in return['path'], 
     118     *                   params in return['params'], 
    65119     */ 
    66120    function find_route($url) { 
    67  
    68121        // ensure at least one route (the default route) exists 
    69122        if($this->routes_count == 0) { 
    70             $this->routes['path'] = $this->default_route_path; 
    71             $this->routes['params'] = null; 
     123            $this->connect($this->default_route_path); 
    72124        } 
    73125 
     
    78130            unset($reg_exp); 
    79131            $route_regexp = $this->build_route_regexp($route['path']); 
    80  
    81132            if($url == "" && $route_regexp == "") { 
    82133                $this->selected_route = $route; 
     
    92143 
    93144        return $this->selected_route; 
    94     } 
     145    }                                 // function find_route($url) 
    95146 
    96147    /** 
     148     *  Build a regular expression that matches a route 
    97149     * 
    98      *  @todo Document this method 
     150     *  <b>FIXME:</b> Should this method be private? 
     151     *  @param string $route_path  A route path. 
     152     *  @return string Regular expression that matches the route in 
     153     *                $route_path  
    99154     */ 
    100155    function build_route_regexp($route_path) { 
     156        //        echo "entering build_route_regexp(), \$route_path is '$route_path'\n"; 
    101157 
    102158        $route_regexp = null; 
     
    105161            $route_path = explode("/",$route_path); 
    106162        } 
    107  
     163        //        echo "route path:\n"; 
     164        //        var_dump($route_path); 
    108165        if(count($route_path) > 0) { 
    109166            foreach($route_path as $path_element) { 
     
    124181} 
    125182 
     183// -- set Emacs parameters -- 
     184// Local variables: 
     185// tab-width: 4 
     186// c-basic-offset: 4 
     187// c-hanging-comment-ender-p: nil 
     188// indent-tabs-mode: nil 
     189// End: 
    126190?>