Ignore:
Timestamp:
08/15/10 18:45:12 (22 months ago)
Author:
vain
Message:
  • several changes to copyright, router, bootstrap, frontcontroller and various other files
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/core/router.core.php

    r4568 r4578  
    2424    * 
    2525    * @license    GNU/GPL v2 or (at your option) any later version, see "/doc/LICENSE". 
    26     * 
    2726    * @author     Jens-André Koch <vain@clansuite.com> 
    2827    * @copyright  Jens-André Koch (2005 - onwards) 
     28    * @link       http://www.clansuite.com 
    2929    * 
    30     * @link       http://www.clansuite.com 
    31     * @link       http://gna.org/projects/clansuite 
    32     * 
    33     * @version    SVN: $Id$response.class.php 2580 2008-11-20 20:38:03Z vain $ 
     30    * @version    SVN: $Id$ 
    3431    */ 
    3532 
     
    5148 * Normally all requests made map to a specific physical resource rather than a logical name. 
    5249 * With Routing you are able to map a logical name to a specific physical name. 
    53  * Example: map a logical URL (a mod_rewritten one) to a Controller/Method/Parameter 
    54  * Map a FileRequest via logical URL (a mod_rewritten one) to a DownloadController/Method/Parameter 
     50 * Examples: map a logical URL (a mod_rewritten one) to a Controller/Method/Parameter 
     51 * or map a FileRequest via logical URL (a mod_rewritten one) to a DownloadController/Method/Parameters 
    5552 * 
    5653 * There are two different URL Formatings allowed: 
     
    5855 * 2. Fake HTML File Request or SMF-Style, like so: /mod.sub.action.id.html 
    5956 * 
    60  * SPL Iterator and ArrayAccess are used for fast iteration and easier access to the stored routes. 
    61  * 
    6257 * @category    Clansuite 
    6358 * @package     Core 
    6459 * @subpackage  Router 
    6560 */ 
    66 class Clansuite_Router implements Iterator, ArrayAccess, Clansuite_Router_Interface 
     61class Clansuite_Router implements ArrayAccess, Clansuite_Router_Interface 
    6762{ 
    6863    private static $use_cache = false; 
     
    7065    private $uri = ''; 
    7166    private $uri_segments = array(); 
    72     private $extension = ''; 
     67    private static $extension = ''; 
    7368 
    7469    /** 
     
    8378     */ 
    8479    private $routes = array(); 
    85      
     80 
    8681    /** 
    8782     * Constructor. 
    88      * 
    89      * @param string $request_url The Request URL incomming via Clansuite_HttpRequest::getRequestURI() 
    90      */ 
    91     public function __construct($request_uri) 
    92     { 
     83     */ 
     84    public function __construct(Clansuite_HttpRequest $request) 
     85    { 
     86        $request_uri = $request::getRequestURI(); 
     87 
    9388        # clean the incomming uri 
    9489        $this->uri = self::prepareRequestURI($request_uri); 
    95  
    96         # check if routes caching is activated in config, maybe we can load routes from cache 
    97         if(isset($config['routing']['cache_routes']) and true === $config['routing']['cache_routes']) 
    98         { 
    99             self::$use_cache = true; 
    100         } 
    101         else 
    102         { 
    103             self::$use_cache = false; 
    104         } 
    10590    } 
    10691 
     
    11196     * @param array $route_options 
    11297     */ 
    113     public function addRoute($url_pattern, array $route_options) 
    114     { 
    115         $this->connect($url_pattern, $route_options); 
     98    public function addRoute($url_pattern, array $route_options = null) 
     99    { 
     100        /** 
     101         * 1) Preprocess the route 
     102         */ 
     103        # split the pattern describing the URL target into uri segments 
     104        $url_pattern = ltrim($url_pattern, '/'); 
     105        $segments = explode('/', $url_pattern); 
     106 
     107        # because the incomming route might have placeholders lile (:num) or (:id) 
     108        $url_pattern = self::placeholdersToRegexp($url_pattern); 
     109 
     110        $regexp = ''; 
     111        $regexp = $this->processSegmentsRegExp($segments, $route_options); 
     112        $number_of_segments = count($segments); 
     113        $options = array('regexp' => $regexp, 
     114                         'number_of_segments' => $number_of_segments); 
     115 
     116 
     117        /** 
     118         * 2) Finally add the *now preprocessed* Route. 
     119         */ 
     120        $this->routes[$url_pattern] = $options; 
     121    } 
     122 
     123    public function processSegmentsRegExp(array $segments, array $requirements = null) 
     124    { 
     125        # start regular expression 
     126        $regexp = '/^'; 
     127 
     128        # process all segments 
     129        foreach($segments as $segment) 
     130        { 
     131            /** 
     132             * process static named parameter => ":contoller" 
     133             */ 
     134            if (preg_match('/^:([a-zA-Z_]+)$/', $segment, $match)) 
     135            { 
     136                $name = $match[1]; #controller 
     137 
     138                # is there a requirement for this param? 
     139                if(isset($requirements[$name])) 
     140                { 
     141                    # add it to the regex 
     142                    $regexp .= '\/(?P<' . $name . '>' . $requirements[$name] . ')'; 
     143                    # and remove the requirement 
     144                    unset($requirement[$name]); 
     145                } 
     146                else # no requirement 
     147                { 
     148                    $regexp .= '(?P<' . $name . '>[a-z0-9_-]+)'; 
     149                } 
     150            } 
     151            else # process static parameter = string => "/index" or "/news" 
     152            { 
     153                $regexp .= '\\/' . $segment; 
     154            } 
     155 
     156            # regexp between segments 
     157            $regexp .= '\/?'; 
     158        } 
     159 
     160        # finish regular expression 
     161        $regexp .= '$/'; 
     162 
     163        return $regexp; 
    116164    } 
    117165 
     
    138186        return $this->routes; 
    139187    } 
    140      
     188 
    141189    /** 
    142190     * Delete a route by its url pattern 
     
    161209    public function generateURL($url_pattern, array $params = null, $fragment = null, $absolute = false) 
    162210    { 
    163          
     211 
    164212    } 
    165213 
     
    194242        if(empty($this->uri) or $this->uri === '/') 
    195243        { 
    196             $route = new Clansuite_TargetRoute(); 
    197             $route->setController('news'); 
    198             $route->setAction('show'); 
    199             return $route; 
     244            Clansuite_TargetRoute::setController('news'); 
     245            Clansuite_TargetRoute::setAction('show'); 
     246 
     247            return Clansuite_TargetRoute::getInstance(); 
    200248        } 
    201249 
    202250        # attach more routes to this object via the event "onInitializeRoutes" 
    203         Clansuite_CMS::triggerEvent('onInitializeRoutes', $this); 
     251        #Clansuite_CMS::triggerEvent('onInitializeRoutes', $this); 
    204252 
    205253        # initalize Routes 
    206         #$this->loadDefaultRoutes(); 
     254        $this->loadDefaultRoutes(); 
    207255 
    208256        # first filter: drop all routes with more segments then uri_segments 
    209         #self::removeRoutesBySegmentCount(); 
     257        self::removeRoutesBySegmentCount(); 
    210258 
    211259        # map match uri 
    212  
     260        return $this->mapMatchURI(); 
     261    } 
     262 
     263    /** 
     264     * Matches the URI against the Routes Table 
     265     * takes static, dynamic and regexp routings into account 
     266     * 
     267     * @return object Clansuite_TargetRoute 
     268     */ 
     269    public function mapMatchURI() 
     270    { 
     271        Clansuite_Debug::firebug($this->uri); 
     272 
     273        /** 
     274         * Do we have a direct match ? 
     275         * URI = '/index/show' => Routes['/index/show'] 
     276         */ 
     277        if(isset($this->routes[$this->uri])) # does this check work? 
     278        { 
     279            $found_route = $this->routes[$this->uri]; 
     280        } 
     281        else # no, there wasn't a 1:1 match. now we have to check the uri segments 
     282        { 
     283            # loop over the remaining routes and try to map match the uri_segments 
     284            foreach($this->routes as $route_pattern => $route_values) 
     285            { 
     286                # @todo $this->uri might be enough here 
     287                $uri = implode('/', $this->uri_segments); 
     288 
     289                Clansuite_Debug::firebug($route_values); 
     290 
     291                /** 
     292                 * process static named parameter 
     293                 * like ":controller" or ":subcontroller" or ":action" or ":id" 
     294                 * $route_pattern 
     295                 */ 
     296                if (1 === preg_match('/^:([a-zA-Z_]+)$/', $uri, $match)) 
     297                { 
     298                    Clansuite_Debug::firebug($match); 
     299                    $name = $match[1]; #setController($match[1]); 
     300                    $found_route = $name; 
     301                } 
     302 
     303                # dynamic regexp segment? 
     304                elseif(1 === preg_match( $route_values['regexp'], $uri, $matches)) 
     305                { 
     306                    Clansuite_Debug::firebug($matches); 
     307 
     308                    # parameters found by regular expression have priority 
     309                    if(isset($matches['controller'])) 
     310                    { 
     311                        Clansuite_TargetRoute::setController($matches['controller']); 
     312                    } 
     313 
     314                    if(isset($matches['action'])) 
     315                    { 
     316                       Clansuite_TargetRoute::setAction($matches['action']); 
     317                    } 
     318 
     319                    if(isset($matches['id'])) 
     320                    { 
     321                       Clansuite_TargetRoute::setId($matches['id']); 
     322                    } 
     323                } 
     324 
     325                # route found 
     326                break; 
     327            } 
     328        } 
     329 
     330        #Clansuite_TargetRoute::setController($found_route); 
     331        #Clansuite_TargetRoute::setAction('show'); 
     332 
     333        return Clansuite_TargetRoute::getInstance(); 
     334        # Clansuite_CMS::triggerEvent('onAfterInitializeRoutes', $this); 
    213335    } 
    214336 
     
    221343     * @return bool True, if "RewriteEngine On". False otherwise. 
    222344     */ 
    223     public static function isRewriteEngineOn() 
     345    public function isRewriteEngineOn() 
    224346    { 
    225347        # maybe, we have a modrewrite config setting, this avoids overhead 
     
    232354        if(function_exists('apache_get_modules') and in_array('mod_rewrite', apache_get_modules())) 
    233355        { 
    234             # load htacces and check if RewriteEngine is enabled 
    235             $htaccess_content = @file_get_contents(ROOT . '.htaccess'); 
    236             self::$rewriteEngineOn = preg_match('/.*[^#][\t ]+RewriteEngine[\t ]+On/i', $htaccess_content); 
     356            # load htaccess and check if RewriteEngine is enabled 
     357            if(true === is_file(ROOT . '.htaccess')) 
     358            { 
     359                $htaccess_content = file_get_contents(ROOT . '.htaccess'); 
     360                self::$rewriteEngineOn = preg_match('/.*[^#][\t ]+RewriteEngine[\t ]+On/i', $htaccess_content); 
     361            } 
    237362 
    238363            if(self::$rewriteEngineOn == 1) 
     
    288413        $route = new Clansuite_TargetRoute(); 
    289414 
     415        # Controller 
    290416        if(isset($this->uri_segments['mod'])) 
    291417        { 
     
    294420        } 
    295421 
     422        # SubController 
    296423        if(isset($this->uri_segments['sub'])) 
    297424        { 
     
    300427        } 
    301428 
     429        # Action 
    302430        if(isset($this->uri_segments['action'])) 
    303431        { 
     
    306434        } 
    307435 
    308         # the rest of the uri_segments are just params for the method 
    309  
     436        # Parameters 
    310437        if(count($this->uri_segments) > 0) 
    311438        { 
     
    321448     * 
    322449     * This URLParser has to extract mod, sub, action, id/parameters from the URI. 
     450     * This is the Standard_Request_Resolver. 
    323451     * 
    324452     * @param string $url The Request URL 
    325453     */ 
    326     private function UrlParser_NoRewrite($uri) # Standard_Request_Resolver 
    327     { 
    328  
     454    private function UrlParser_NoRewrite($uri) 
     455    { 
    329456        # use some parse_url magic to get the url_query part from the uri 
    330457        $uri_query_string = parse_url($uri, PHP_URL_QUERY); 
     
    346473            foreach($uri_query_array as $query_pair) 
    347474            { 
    348                 list($key, $value) = explode('=', $query_pair); 
    349                 $parameters[$key] = $value; 
     475                if( false !== strpos($query_pair, '=')) 
     476                { 
     477                    list($key, $value) = explode('=', $query_pair); 
     478                    $parameters[$key] = $value; 
     479                } 
    350480            } 
    351481        } 
    352  
    353482        unset($uri_query_string, $uri_query_array, $query_pair, $key, $value); 
    354483 
     
    384513            $uri = mb_substr($uri, 0, $pos); 
    385514        } 
    386         unset($pos); 
    387515 
    388516        /** 
     
    401529            $uri_dot_array = explode('.', $uri); 
    402530            # chop off the last piece as the extension 
    403             $this->extension = array_pop($uri_dot_array); 
     531            self::$extension = array_pop($uri_dot_array); 
    404532            # there might be multiple dots in the url 
    405533            # thats why implode is used to reassemble the segmentized array to a string again 
     
    407535            # = ini_get('arg_separator.output') 
    408536            $uri = implode('/', $uri_dot_array); 
    409         } 
    410         unset($uri_dot_array); 
     537            unset($uri_dot_array); 
     538        } 
    411539        unset($pos); 
    412540 
     
    422550        #Clansuite_Debug::firebug($uri_split); 
    423551        $this->uri_segments = $url_split; 
     552        unset($url_split); 
    424553    } 
    425554 
     
    461590    } 
    462591 
     592    public static function checkRouteCachingActive() 
     593    { 
     594        # check if routes caching is activated in config, maybe we can load routes from cache 
     595        if(isset($config['routing']['cache_routes']) and true === $config['routing']['cache_routes']) 
     596        { 
     597            self::$use_cache = true; 
     598        } 
     599        else 
     600        { 
     601            self::$use_cache = false; 
     602        } 
     603    } 
     604 
    463605    /** 
    464606     * Register the default routes. 
     
    466608    public function loadDefaultRoutes() 
    467609    { 
    468         # check cache for routes 
     610        self::checkRouteCachingActive(); 
     611 
     612        # Load Routes from Cache 
    469613        if(true === self::$use_cache and empty($this->routes) and Clansuite_Cache::contains('clansuite.routes')) 
    470614        { 
     
    472616        } 
    473617 
    474         if(empty($this->routes)) # load routes table from routes.config.php 
     618        # Load Routes from routes.config.php 
     619        if(empty($this->routes)) 
    475620        { 
    476621            $this->addRoutes( Clansuite_Routes_Manager::loadRoutesFromConfig()); 
     
    490635        if(empty($this->routes)) 
    491636        { 
    492             $this->connect('/:controller'); 
    493             $this->connect('/:controller/:action'); 
    494             $this->connect('/:controller/:action/:id'); 
    495             $this->connect('/:controller/:action/:id/:format'); 
     637            $this->addRoute('/:controller'); 
     638            $this->addRoute('/:controller/:action'); 
     639            $this->addRoute('/:controller/:action/:id'); 
     640            $this->addRoute('/:controller/:action/:id/:format'); 
     641            /* 
     642            $this->addRoute('/:controller/:subcontroller'); 
     643            $this->addRoute('/:controller/:subcontroller/:action'); 
     644            $this->addRoute('/:controller/:subcontroller/:action/:id'); 
     645            $this->addRoute('/:controller/:subcontroller/:action/:id/:format'); 
     646            */ 
    496647        } 
    497648    } 
     
    535686/** 
    536687 * Clansuite_Mapper 
     688 * 
     689 * Provides helper methods to transform (map) 
     690 * (a) the controller name into the specific application classname and filename 
     691 * (b) the action name into the specific application actioname. 
    537692 * 
    538693 * @category    Clansuite 
     
    600755 
    601756        # attach subcontroller to classname 
    602         if($subcontroller !== null) 
     757        if(isset($subcontroller)) 
    603758        { 
    604759            $classname .= '_' . ucfirst($subcontroller); 
     
    609764 
    610765    /** 
    611      * Maps the action to an method name. 
    612      * The pseudo-namesspace prefix 'action_' is used for all actions. 
    613      * Example: action_show() 
     766     * Maps the action to it's method name. 
     767     * The prefix 'action_' (pseudo-namesspace) is used for all actions. 
     768     * Example: A action named "show" will be mapped to "action_show()" 
    614769     * This is also a way to ensure some kind of whitelisting via namespacing. 
    615770     * 
     
    617772     * In this case the actionname is action_admin_show(). 
    618773     * 
    619      * @param  string the action 
    620      * @param  string the submodule 
    621      * @return string  the mapped method name 
     774     * @param  string $action the action 
     775     * @param  string $submodule the submodule 
     776     * @return string the mapped method name 
    622777     */ 
    623778    public static function mapActionToActioname($action, $submodule = null) 
    624779    { 
    625         # action not set by URL, so we set action from config/this class 
     780        # set default value for action, when not set by URL 
    626781        if(false === isset($action)) 
    627782        { 
    628             # set the method name 
    629783            $action = self::$defaultAction; 
    630784        } 
    631785 
    632         # if $submodule is set, use it as a prefix on $action 
    633         if(isset($submodule) and ($submodule !== null)) 
     786        # if a $submodule is set, use it as a PREFIX on $action 
     787        if(isset($submodule)) 
    634788        { 
    635789            $action = $submodule . '_' . $action; 
    636790        } 
    637791 
     792        #Clansuite_Debug::firebug($action); 
     793 
    638794        # all clansuite actions are prefixed with 'action_' 
    639         return 'action_' . $action; 
     795        return self::METHOD_PREFIX . '_' . $action; 
    640796    } 
    641797} 
    642798 
     799/** 
     800 * Clansuite_TargetRoute (processed RequestObject) 
     801 */ 
    643802class Clansuite_TargetRoute extends Clansuite_Mapper 
    644803{ 
    645     private static $parameters = array( 
     804    public static $parameters = array( 
     805        # File 
    646806        'filename'      => null, 
    647807        'classname'     => null, 
     808        # Call 
    648809        'controller'    => 'index', 
    649810        'subcontroller' => null, 
     
    651812        'method'        => null, 
    652813        'params'        => null, 
     814        # Output 
    653815        'format'        => 'html', 
    654816        'language'      => 'en', 
     
    660822    ); 
    661823 
     824    /** 
     825     * Clansuite_TargetRoute is a Singleton 
     826     * 
     827     * @return instance of Clansuite_TargetRoute class 
     828     */ 
     829    public static function getInstance() 
     830    { 
     831        static $instance; 
     832        if(isset($instance) == null) 
     833        { 
     834            $instance = new Clansuite_TargetRoute(); 
     835        } 
     836        return $instance; 
     837    } 
     838 
    662839    public static function setFilename($filename) 
    663840    { 
     
    695872    } 
    696873 
     874    /** 
     875     * Returns Name of the Controller 
     876     * 
     877     * @return string Controller/Modulename 
     878     */ 
    697879    public static function getController() 
    698880    { 
     
    700882    } 
    701883 
     884    /** 
     885     * Convenience/shorthand Method for getController() 
     886     * 
     887     * @return string Controller/Modulename 
     888     */ 
     889    public static function getModuleName() 
     890    { 
     891        return self::$parameters['controller']; 
     892    } 
     893 
    702894    public static function setSubController($subcontroller) 
    703895    { 
     
    710902    } 
    711903 
     904    /** 
     905     * Method to get the SubModuleName 
     906     * 
     907     * @return $string 
     908     */ 
     909    public static function getSubModuleName() 
     910    { 
     911        return self::$parameters['subcontroller']; 
     912    } 
     913 
    712914    public static function setAction($action) 
    713915    { 
     
    718920    { 
    719921        return self::$parameters['action']; 
     922    } 
     923 
     924    public static function setId($id) 
     925    { 
     926        self::$parameters['params']['id'] = $id; 
     927    } 
     928 
     929    public static function getId() 
     930    { 
     931        return self::$parameters['params']['id']; 
     932    } 
     933 
     934    /** 
     935     * Method to get the Action with Prefix 
     936     * 
     937     * @return $string 
     938     */ 
     939    public static function getActionName() 
     940    { 
     941        return self::$parameters['method']; 
    720942    } 
    721943 
     
    732954            return self::$parameters['method']; 
    733955        } 
    734         else # add method prefix 
    735         { 
    736             $method = 'action_' . self::$parameters['action']; 
    737  
    738             # action + prefix = method, set it 
    739             self::setMethod($method); 
     956        else # add method prefix (action_) and subcontroller prefix (admin_) 
     957        { 
     958            #if(empty(self::$parameters['method'])) 
     959            #{ 
     960                self::setMethod(self::mapActionToActioname(self::getAction(), self::getSubController())); 
     961            #} 
    740962        } 
    741963 
     
    7911013    { 
    7921014        return ROOT_MOD . self::getController() . DS; 
     1015    } 
     1016 
     1017    public static function debug() 
     1018    { 
     1019        $string = (string) implode(",", self::$parameters); 
     1020        Clansuite_Debug::firebug($string); 
     1021    } 
     1022 
     1023    public static function getRoute() 
     1024    { 
     1025        return self::$parameters; 
    7931026    } 
    7941027} 
     
    8021035class Clansuite_Routes_Manager 
    8031036{ 
    804     public static function addRoutesOfModule($modulename) 
     1037    public function addRoutesOfModule($modulename) 
    8051038    { 
    8061039        self:updateApplicationRoutes($modulename); 
    8071040    } 
    8081041 
    809     public static function delRoutesOfModule($modulename) 
     1042    public function delRoutesOfModule($modulename) 
    8101043    { 
    8111044        $module_routes_file = ROOT_MOD . '/' . $modulename . '/' . $modulename . '.routes.php'; 
     
    8251058    { 
    8261059        $routes_count = count($this->routes); 
     1060 
     1061        # loop over all routes 
    8271062        for($i == 0; $i < $routes_count; $i++) 
    8281063        { 
     1064            # check if there is a route with the given name 
    8291065            if($this->routes[$i]['name'] == $route_name) 
    8301066            { 
     1067                # got one? then remove it from the routes array and stop 
    8311068                array_splice($this->routes, $i, 1); 
    8321069                break; 
    8331070            } 
    8341071        } 
     1072 
    8351073        return $this->routes;; 
    8361074    } 
     
    8411079     * @param string $modulename Name of module 
    8421080     */ 
    843     public static function updateApplicationRoutes($modulename = null) 
     1081    public function updateApplicationRoutes($modulename = null) 
    8441082    { 
    8451083        $activated_modules = array(); 
     
    8841122        { 
    8851123            # load common routes configuration 
    886             $routes = include ROOT . 'configuration/routes.config.php'; 
     1124            # includes array $routes 
     1125            include ROOT . 'configuration/routes.config.php'; 
    8871126        } 
    8881127        else 
    8891128        { 
    8901129            # load specific routes config file 
    891             $routes = include ROOT . $routes_config_file; 
     1130            include ROOT . $routes_config_file; 
    8921131        } 
    8931132 
Note: See TracChangeset for help on using the changeset viewer.