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

Source for file catchall.php

Documentation is available at catchall.php

  1. <?php
  2. /**
  3.  * File containing the ezcMvcCatchAllRoute 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 acts like a catch all for /.../... type routes.
  30.  *
  31.  * The routes are matched against the uri property of the request object.
  32.  *
  33.  * @package MvcTools
  34.  * @version //autogentag//
  35.  * @mainclass
  36.  */
  37. class ezcMvcCatchAllRoute implements ezcMvcRoute
  38. {
  39.     /**
  40.      * If url has no controller to match, use this as default.
  41.      *
  42.      * @var string 
  43.      */
  44.     protected $controller;
  45.  
  46.     /**
  47.      * If url has no action to match, use this as default.
  48.      *
  49.      * @var string 
  50.      */
  51.     protected $action;
  52.  
  53.     /**
  54.      * Only allow to catch routes that match at least this prefix.
  55.      *
  56.      * @var array 
  57.      */
  58.     private $prefix array();
  59.  
  60.     /**
  61.      * Construct a CatchAll Route
  62.      *
  63.      * @param string $defaultController 
  64.      * @param string $defaultAction 
  65.      */
  66.     public function __construct$defaultController 'index'$defaultAction 'index' )
  67.     {
  68.         $this->controller = $defaultController;
  69.         $this->action = $defaultAction;
  70.     }
  71.  
  72.     /**
  73.      * Returns the request information that the matches() method will match the
  74.      * pattern against.
  75.      *
  76.      * @param ezcMvcRequest $request 
  77.      * @return string 
  78.      */
  79.     protected function getUriStringezcMvcRequest $request )
  80.     {
  81.         return $request->uri;
  82.     }
  83.  
  84.     /**
  85.      * Returns routing information if the route matched, or null in case the
  86.      * route did not match.
  87.      *
  88.      * @param ezcMvcRequest $request Request to test.
  89.      * @return null|ezcMvcRoutingInformation
  90.      */
  91.     public function matchesezcMvcRequest $request )
  92.     {
  93.         $requestParts explode'/'$this->getUriString$request ) );
  94.  
  95.         if !$this->checkPrefixMatch$requestParts ) )
  96.         {
  97.             return null;
  98.         }
  99.  
  100.         $params array();
  101.         $i = -1;
  102.         foreach $requestParts as $part )
  103.         {
  104.             switch $i )
  105.             {
  106.                 case -1:
  107.                     // ignore, as it's the bit before the first /
  108.                     break;
  109.                 case 0:
  110.                     $this->controller = $part;
  111.                     break;
  112.                 case 1:
  113.                     $this->action = $part;
  114.                     break;
  115.                 default:
  116.                     $params[$this->createParamName$i )$part;
  117.             }
  118.             $i++;
  119.         }
  120.  
  121.         $controllerName $this->createControllerName();
  122.         if class_exists$controllerName ) )
  123.         {
  124.             $actionMethod call_user_funcarray$controllerName'createActionMethodName' )$this->action );
  125.             if !method_exists$controllerName$actionMethod ) )
  126.             {
  127.                 return null;
  128.             }
  129.             $request->variables array_merge$request->variables$params );
  130.             return new ezcMvcRoutingInformation$this->getUriString$request )$controllerName$this->action );
  131.         }
  132.         return null;
  133.     }
  134.  
  135.     /**
  136.      * Create the param name from the indexed parameter
  137.      *
  138.      * @param  int $index 
  139.      * @return string 
  140.      */
  141.     protected function createParamName$index )
  142.     {
  143.         $paramName 'param' $index;
  144.         return $paramName;
  145.     }
  146.  
  147.     /**
  148.      * Create the controller name from the matched name
  149.      *
  150.      * @return string 
  151.      */
  152.     protected function createControllerName()
  153.     {
  154.         $controllerName $this->controller . 'Controller';
  155.         return $controllerName;
  156.     }
  157.  
  158.     /**
  159.      * Check if the prefix matches.
  160.      *
  161.      * @param  array $parts 
  162.      * @return boolean 
  163.      */
  164.     protected function checkPrefixMatch$parts )
  165.     {
  166.         for $i 0$i count$this->prefix )$i++ )
  167.         {
  168.             if !isset$parts[$i|| $parts[$i!== $this->prefix[$i)
  169.             {
  170.                 return false;
  171.             }
  172.         }
  173.         return true;
  174.     }
  175.  
  176.     /**
  177.      * Adds a prefix to the route.
  178.      *
  179.      * @param string $prefix 
  180.      */
  181.     public function prefix$prefix )
  182.     {
  183.         $this->prefix explode'/'$prefix );
  184.     }
  185. }
  186. ?>
Documentation generated by phpDocumentor 1.4.3