コード例 #1
0
ファイル: Director.php プロジェクト: racontemoi/shibuichi
 /**
  * Handle an HTTP request, defined with a HTTPRequest object.
  *
  * @return HTTPResponse|string
  */
 protected static function handleRequest(HTTPRequest $request, Session $session)
 {
     krsort(Director::$rules);
     if (isset($_REQUEST['debug'])) {
         Debug::show(Director::$rules);
     }
     foreach (Director::$rules as $priority => $rules) {
         foreach ($rules as $pattern => $controllerOptions) {
             if (is_string($controllerOptions)) {
                 if (substr($controllerOptions, 0, 2) == '->') {
                     $controllerOptions = array('Redirect' => substr($controllerOptions, 2));
                 } else {
                     $controllerOptions = array('Controller' => $controllerOptions);
                 }
             }
             if (($arguments = $request->match($pattern, true)) !== false) {
                 // controllerOptions provide some default arguments
                 $arguments = array_merge($controllerOptions, $arguments);
                 // Find the controller name
                 if (isset($arguments['Controller'])) {
                     $controller = $arguments['Controller'];
                 }
                 // Pop additional tokens from the tokeniser if necessary
                 if (isset($controllerOptions['_PopTokeniser'])) {
                     $request->shift($controllerOptions['_PopTokeniser']);
                 }
                 // Handle redirections
                 if (isset($arguments['Redirect'])) {
                     return "redirect:" . Director::absoluteURL($arguments['Redirect'], true);
                 } else {
                     /*
                     if(isset($arguments['Action'])) {
                     	$arguments['Action'] = str_replace('-','',$arguments['Action']);
                     }
                     
                     if(isset($arguments['Action']) && ClassInfo::exists($controller.'_'.$arguments['Action']))
                     	$controller = $controller.'_'.$arguments['Action'];
                     */
                     if (isset($arguments['URLSegment'])) {
                         self::$urlSegment = $arguments['URLSegment'] . "/";
                     }
                     Director::$urlParams = $arguments;
                     $controllerObj = new $controller();
                     $controllerObj->setSession($session);
                     return $controllerObj->handleRequest($request);
                 }
             }
         }
     }
 }
コード例 #2
0
ファイル: Director.php プロジェクト: ramziammar/websites
 static function getControllerForURL($url)
 {
     if (isset($_GET['debug_profile'])) {
         Profiler::mark("Director", "getControllerForURL");
     }
     $url = preg_replace(array('/\\/+/', '/^\\//', '/\\/$/'), array('/', '', ''), $url);
     $urlParts = split('/+', $url);
     krsort(Director::$rules);
     if (isset($_REQUEST['debug'])) {
         Debug::show(Director::$rules);
     }
     foreach (Director::$rules as $priority => $rules) {
         foreach ($rules as $pattern => $controller) {
             $patternParts = explode('/', $pattern);
             $matched = true;
             $arguments = array();
             foreach ($patternParts as $i => $part) {
                 $part = trim($part);
                 if (isset($part[0]) && $part[0] == '$') {
                     $arguments[substr($part, 1)] = isset($urlParts[$i]) ? $urlParts[$i] : null;
                     if ($part == '$Controller' && !class_exists($arguments['Controller'])) {
                         $matched = false;
                         break;
                     }
                 } else {
                     if (!isset($urlParts[$i]) || $urlParts[$i] != $part) {
                         $matched = false;
                         break;
                     }
                 }
             }
             if ($matched) {
                 if (substr($controller, 0, 2) == '->') {
                     if ($_REQUEST['debug'] == 1) {
                         Debug::message("Redirecting to {$controller}");
                     }
                     if (isset($_GET['debug_profile'])) {
                         Profiler::unmark("Director", "getControllerForURL");
                     }
                     return "redirect:" . Director::absoluteURL(substr($controller, 2), true);
                 } else {
                     if (isset($arguments['Controller']) && $controller == "*") {
                         $controller = $arguments['Controller'];
                     }
                     if (isset($_REQUEST['debug'])) {
                         Debug::message("Using controller {$controller}");
                     }
                     if (isset($arguments['Action'])) {
                         $arguments['Action'] = str_replace('-', '', $arguments['Action']);
                     }
                     if (isset($arguments['Action']) && ClassInfo::exists($controller . '_' . $arguments['Action'])) {
                         $controller = $controller . '_' . $arguments['Action'];
                     }
                     Director::$urlParams = $arguments;
                     $controllerObj = new $controller();
                     $controllerObj->setURLParams($arguments);
                     if (isset($arguments['URLSegment'])) {
                         self::$urlSegment = $arguments['URLSegment'] . "/";
                     }
                     if (isset($_GET['debug_profile'])) {
                         Profiler::unmark("Director", "getControllerForURL");
                     }
                     return $controllerObj;
                 }
             }
         }
     }
 }