Beispiel #1
0
 /**
  * Test to make sure router is able to do a reverse lookup and get the URL for a
  * given controller+method+params
  */
 function test_GetUrl()
 {
     $this->Println("RouterTest.GetUrl");
     $routemap = array("GET:" => array("route" => "Test.One"), "GET:test2" => array("route" => "Test.Two"), "GET:test3/(:num)/route" => array("route" => "Test.Three", "params" => array("a" => 1)), "GET:test4/(:any)/route/(:num)" => array("route" => "Test.Four", "params" => array("a" => 1, "b" => 3)), "GET:test5/route" => array("route" => "Test5.Route"));
     $router = new GenericRouter('http://localhost/subdir/', 'Test.One', $routemap);
     $_SERVER['REQUEST_METHOD'] = "GET";
     // BEGIN TEST
     $this->Println(">> Router test 1");
     $this->assertEquals('http://localhost/subdir/', $router->GetUrl("Test", "One"));
     // BEGIN TEST
     $this->Println(">> Router test 2");
     $this->assertEquals('http://localhost/subdir/test2', $router->GetUrl("Test", "Two"));
     // BEGIN TEST
     $this->Println(">> Router test 3");
     $this->assertEquals('http://localhost/subdir/test3/222/route', $router->GetUrl("Test", "Three", array("a" => 222)));
     // BEGIN TEST
     $this->Println(">> Router test 4");
     $this->assertEquals('http://localhost/subdir/test4/xxx/route/333', $router->GetUrl("Test", "Four", array("a" => "xxx", "b" => 333)));
     // BEGIN TEST
     $this->Println(">> Router test 5");
     $this->assertEquals('http://localhost/subdir/test5/route', $router->GetUrl("Test5", "Route", array()));
     // BEGIN TEST
     $this->Println(">> Router test 6");
     $exceptionWasThrown = false;
     try {
         $url = $router->GetUrl("Test", "Unknown");
         // unknown route should throw an exception
     } catch (Exception $ex) {
         $exceptionWasThrown = true;
     }
     $this->assertTrue($exceptionWasThrown, 'Expected router to throw an exception for an unknown route');
 }
Beispiel #2
0
 /**
  * Processes user input and executes the specified controller method, ensuring
  * that the controller dependencies are all injected properly
  *
  * @param Phreezer $phreezer Object persistance engine
  * @param IRenderEngine $renderEngine rendering engine
  * @param string (optional) $action the user requested action (if not provided will use router->GetRoute())
  * @param Context (optional) a context object for persisting state
  * @param IRouter (optional) router object for reading/writing URLs (if not provided, GenericRouter will be used)
  */
 static function Dispatch($phreezer, $renderEngine, $action = '', $context = null, $router = null)
 {
     if ($router == null) {
         require_once 'GenericRouter.php';
         $router = new GenericRouter();
     }
     // get the route and normalize the controller name
     list($controller_param, $method_param) = $router->GetRoute($action);
     $controller_class = $controller_param . "Controller";
     if (self::$FAST_LOOKUP) {
         if (!class_exists($controller_class)) {
             $controller_file = "Controller/{$controller_class}.php";
             include_once $controller_file;
         }
         $controller = new $controller_class($phreezer, $renderEngine, $context, $router);
         $controller->{$method_param}();
         return true;
     }
     // if the controller was in a sub-directory, get rid of the directory path
     $slashPos = strpos($controller_class, '/');
     while ($slashPos !== false) {
         $controller_class = substr($controller_class, $slashPos + 1);
         $slashPos = strpos($controller_class, '/');
     }
     if (!class_exists($controller_class)) {
         // attempt to locate the controller file
         $controller_file = "Controller/" . $controller_param . "Controller.php";
         $controller_filepath = null;
         // search for the controller file in the default locations, then the include path
         $paths = array_merge(array('./libs/', './'), explode(PATH_SEPARATOR, get_include_path()));
         $found = false;
         foreach ($paths as $path) {
             $controller_filepath = self::ControllerFileExists($path . "/" . $controller_file);
             if ($controller_filepath) {
                 $found = true;
                 break;
             }
         }
         if (!$found) {
             throw new Exception("File ~/libs/" . $controller_file . " was not found in include path");
         }
         // convert any php errors into an exception
         if (self::$IGNORE_DEPRECATED) {
             ExceptionThrower::Start();
         } else {
             ExceptionThrower::Start(E_ALL);
             ExceptionThrower::$IGNORE_DEPRECATED = false;
         }
         // we should be fairly certain the file exists at this point
         include_once $controller_filepath;
         // we found the file but the expected class doesn't appear to be defined
         if (!class_exists($controller_class)) {
             throw new Exception("Controller file was found, but class '" . $controller_class . "' is not defined");
         }
     }
     // create an instance of the controller class
     $controller = new $controller_class($phreezer, $renderEngine, $context, $router);
     // we have a valid instance, just verify there is a matching method
     if (!is_callable(array($controller, $method_param))) {
         throw new Exception("'" . $controller_class . "." . $method_param . "' is not a valid action");
     }
     // do not call the requested method/route if the controller request has been cancelled
     if (!$controller->IsTerminated()) {
         // file, class and method all are ok, go ahead and call it
         call_user_func(array(&$controller, $method_param));
     }
     // reset error handling back to whatever it was
     //restore_exception_handler();
     ExceptionThrower::Stop();
     return true;
 }