Apache Zeta Components Manual :: File Source for rails.php

Source for file rails.php

Documentation is available at rails.php

  1. <?php
  2. /**
  3.  * File containing the ezcMvcRailsRoute class
  4.  *
  5.  * Licensed to the Apache Software Foundation (ASF) under one
  6.  * or more contributor license agreements.  See the NOTICE file
  7.  * distributed with this work for additional information
  8.  * regarding copyright ownership.  The ASF licenses this file
  9.  * to you under the Apache License, Version 2.0 (the
  10.  * "License"); you may not use this file except in compliance
  11.  * with the License.  You may obtain a copy of the License at
  12.  * 
  13.  *   http://www.apache.org/licenses/LICENSE-2.0
  14.  * 
  15.  * Unless required by applicable law or agreed to in writing,
  16.  * software distributed under the License is distributed on an
  17.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18.  * KIND, either express or implied.  See the License for the
  19.  * specific language governing permissions and limitations
  20.  * under the License.
  21.  *
  22.  * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  23.  * @version //autogentag//
  24.  * @filesource
  25.  * @package MvcTools
  26.  */
  27.  
  28. /**
  29.  * Router class that uses rails style expressions for matching routes.
  30.  *
  31.  * The routes are matched against the uri property of the request object. The
  32.  * matching algorithm works as follows:
  33.  *
  34.  * - The pattern and URI as returned by getUriString() are split on the /
  35.  *   character.
  36.  * - If the first part of the split *pattern* contains a "." (dot) then the
  37.  *   first part of the pattern and the URI are split by ".". The return of
  38.  *   this, together with the rest of the original split-by-slash string are
  39.  *   concatenated.
  40.  * - Each of the two arrays are compared with each other with the delimiters
  41.  *   being ignored.
  42.  * - A special case are elements in the pattern that start with a ":". In this
  43.  *   case the pattern element and uri element do not need to match. Instead the
  44.  *   pattern element creates a named variable with as value the element from
  45.  *   the URI array with the same index.
  46.  * - If not every element matches, the route does not match and
  47.  *   false is returned. If everything matches, true is returned.
  48.  *
  49.  * @package MvcTools
  50.  * @version //autogentag//
  51.  * @mainclass
  52.  */
  53. class ezcMvcRailsRoute implements ezcMvcRouteezcMvcReversibleRoute
  54. {
  55.     /**
  56.      * This property contains the pattern
  57.      *
  58.      * @var string 
  59.      */
  60.     protected $pattern;
  61.  
  62.     /**
  63.      * This is the name of the controller class that will be instantiated with
  64.      * the request variables obtained from the route, as well as the default
  65.      * values belonging to a route.
  66.      *
  67.      * @var string 
  68.      */
  69.     protected $controllerClassName;
  70.  
  71.     /**
  72.      * Contains the action that the controller should execute.
  73.      *
  74.      * @var string 
  75.      */
  76.     protected $action;
  77.  
  78.     /**
  79.      * The default values for the variables that are send to the controller.
  80.      * The route matchers can override those default values
  81.      *
  82.      * @var array(string) 
  83.      */
  84.     protected $defaultValues;
  85.  
  86.     /**
  87.      * Constructs a new ezcMvcRailsRoute with $pattern.
  88.      *
  89.      * When the route is matched (with the match() method), the route
  90.      * instantiates an object of the class $controllerClassName.
  91.      *
  92.      * @param string $pattern 
  93.      * @param string $controllerClassName 
  94.      * @param string $action 
  95.      * @param array(string) $defaultValues 
  96.      */
  97.     public function __construct$pattern$controllerClassName$action nullarray $defaultValues array() )
  98.     {
  99.         $this->pattern = $pattern;
  100.         $this->controllerClassName = $controllerClassName;
  101.         $this->action = $action;
  102.         $this->defaultValues = $defaultValues;
  103.     }
  104.  
  105.     /**
  106.      * Returns the request information that the matches() method will match the
  107.      * pattern against.
  108.      *
  109.      * @param ezcMvcRequest $request 
  110.      * @return string 
  111.      */
  112.     protected function getUriStringezcMvcRequest $request )
  113.     {
  114.         return $request->uri;
  115.     }
  116.  
  117.     /**
  118.      * Evaluates the URI against this route.
  119.      *
  120.      * The method first runs the match. If the pattern matches, it then creates
  121.      * an object containing routing information and returns it. If the route's
  122.      * pattern did not match it returns null.
  123.      *
  124.      * @param ezcMvcRequest $request 
  125.      * @return null|ezcMvcRoutingInformation
  126.      */
  127.     public function matchesezcMvcRequest $request )
  128.     {
  129.         if $this->match$request$matches ) )
  130.         {
  131.             $request->variables array_merge$this->defaultValues$request->variables$matches );
  132.  
  133.             return new ezcMvcRoutingInformation$this->pattern$this->controllerClassName$this->action );
  134.         }
  135.         return null;
  136.     }
  137.  
  138.     /**
  139.      * This method performs the actual pattern match against the $request's
  140.      * URI.
  141.      *
  142.      * @param ezcMvcRequest $request 
  143.      * @param array(string) $matches 
  144.      * @return bool 
  145.      */
  146.     protected function match$request&$matches )
  147.     {
  148.         $matches array();
  149.  
  150.         // first we split the pattern and request ID per /
  151.         $patternParts preg_split'@(/)@'$this->patternnullPREG_SPLIT_DELIM_CAPTURE );
  152.         $requestParts preg_split'@(/)@'$this->getUriString$request )nullPREG_SPLIT_DELIM_CAPTURE );
  153.  
  154.         if strpos$patternParts[0]'.' !== false )
  155.         {
  156.             $subPatternParts preg_split'@([.])@'$patternParts[0]nullPREG_SPLIT_DELIM_CAPTURE );
  157.             $subRequestParts preg_split'@([.])@'$requestParts[0]nullPREG_SPLIT_DELIM_CAPTURE );
  158.  
  159.             $patternParts array_merge$subPatternPartsarray_slice$patternParts) );
  160.             $requestParts array_merge$subRequestPartsarray_slice$requestParts) );
  161.         }
  162.  
  163.         // if the number of / is not the same, it can not match
  164.         if count$patternParts != count$requestParts ) )
  165.         {
  166.             return false;
  167.         }
  168.  
  169.         // now loop over all parts of the pattern, and see if it matches with
  170.         // the request URI
  171.         foreach $patternParts as $id => $patternPart )
  172.         {
  173.             if $patternPart == '' || $patternPart[0!= ':' )
  174.             {
  175.                 if $patternPart !== $requestParts[$id)
  176.                 {
  177.                     return false;
  178.                 }
  179.             }
  180.             else
  181.             {
  182.                 if $requestParts[$id== '' )
  183.                 {
  184.                     return false;
  185.                 }
  186.                 $matches[substr$patternPart)$requestParts[$id];
  187.             }
  188.         }
  189.         return true;
  190.     }
  191.  
  192.     /**
  193.      * Adds the $prefix to the route's pattern.
  194.      *
  195.      * It's up to the developer to provide a meaningfull prefix. In this case,
  196.      * it needs to be a pattern just like the normal pattern.
  197.      *
  198.      * @param mixed $prefix 
  199.      */
  200.     public function prefix$prefix )
  201.     {
  202.         $this->pattern = $prefix $this->pattern;
  203.     }
  204.  
  205.     /**
  206.      * Generates an URL back out of a route, including possible arguments
  207.      *
  208.      * @param array $arguments 
  209.      */
  210.     public function generateUrlarray $arguments null )
  211.     {
  212.         $patternParts explode'/'$this->pattern );
  213.         foreach $patternParts as &$part )
  214.         {
  215.             if strlen$part && $part[0=== ':' )
  216.             {
  217.                 $paramName substr$part);
  218.                 if !isset$arguments[$paramName) )
  219.                 {
  220.                     throw new ezcMvcMissingRouteArgumentException$this->pattern$paramName );
  221.                 }
  222.                 else
  223.                 {
  224.                     $part $arguments[$paramName];
  225.                 }
  226.             }
  227.         }
  228.         return join'/'$patternParts );
  229.     }
  230. }
  231. ?>
Documentation generated by phpDocumentor 1.4.3