# RouterBaseRule.class.php

Go to the documentation of this file.

    00001 <?php
    00002 /***************************************************************************
    00003  *   Copyright (C) 2008 by Sergey S. Sergeev                               *
    00004  *                                                                         *
    00005  *   This program is free software; you can redistribute it and/or modify  *
    00006  *   it under the terms of the GNU Lesser General Public License as        *
    00007  *   published by the Free Software Foundation; either version 3 of the    *
    00008  *   License, or (at your option) any later version.                       *
    00009  *                                                                         *
    00010  ***************************************************************************/
    00011
    00012     abstract class RouterBaseRule implements RouterRule
    00013     {
    00014         protected $defaults = array();
    00015
    00019         public function chain(RouterRule $route, $separator = '/')
    00020         {
    00021             $chain = new RouterChainRule();
    00022
    00023             $chain->
    00024                 chain($this)->
    00025                 chain($route, $separator);
    00026
    00027             return $chain;
    00028         }
    00029
    00030         public function getDefault($name)
    00031         {
    00032             if (isset($this->defaults[$name])) {
    00033                 return $this->defaults[$name];
    00034             }
    00035
    00036             return null;
    00037         }
    00038
    00039         public function setDefaults(array $defaults)
    00040         {
    00041             $this->defaults = $defaults;
    00042
    00043             return $this;
    00044         }
    00045
    00049         public function getDefaults()
    00050         {
    00051             return $this->defaults;
    00052         }
    00053
    00057         protected function getPath(HttpUrl $url)
    00058         {
    00059             $reducedUrl = clone $url;
    00060
    00061             $base = RouterRewrite::me()->getBaseUrl();
    00062
    00063             if (!$base instanceof HttpUrl)
    00064                 throw new RouterException('Setup base url');
    00065
    00066             if (!$base->getScheme()) {
    00067                 $reducedUrl->
    00068                     setScheme(null)->
    00069                     setAuthority(null);
    00070             }
    00071
    00072             $reducedUrl->setQuery(null);
    00073
    00074             if (
    00075                 (
    00076                     $reducedUrl->getScheme()
    00077                     && ($base->getScheme() != $reducedUrl->getScheme())
    00078                 ) || (
    00079                     $reducedUrl->getAuthority()
    00080                     && ($base->getAuthority() != $reducedUrl->getAuthority())
    00081                 )
    00082             ) {
    00083                 return $reducedUrl;
    00084             }
    00085
    00086             $result = HttpUrl::create();
    00087
    00088             $baseSegments = explode('/', $base->getPath());
    00089             $segments = explode('/', $reducedUrl->getPath());
    00090
    00091             $originalSegments = $segments;
    00092
    00093             array_pop($baseSegments);
    00094
    00095             while (
    00096                 $baseSegments
    00097                 && $segments
    00098                 && ($baseSegments[0] == $segments[0])
    00099             ) {
    00100                 array_shift($baseSegments);
    00101                 array_shift($segments);
    00102             }
    00103
    00104             if ($baseSegments && $baseSegments[0])
    00105                 $segments = $originalSegments;
    00106
    00107             $result->setPath(implode('/', $segments));
    00108
    00109             return $result;
    00110         }
    00111
    00115         protected function processPath(HttpRequest $request)
    00116         {
    00117             if ($request->hasServerVar('REQUEST_URI'))
    00118                 $path =
    00119                     $this->
    00120                         getPath(
    00121                             HttpUrl::create()->
    00122                             parse($request->getServerVar('REQUEST_URI'))
    00123                         );
    00124             else
    00125                 throw new RouterException('Cannot resolve path');
    00126
    00127             return $path;
    00128         }
    00129     }
    00130 ?>