Advertisement
Guest User

Untitled

a guest
Mar 7th, 2011
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.41 KB | None | 0 0
  1. class CommonDispatcher implements Dispatcher {
  2.     private $router,$error404;
  3.     protected $context;
  4.     protected $factory;
  5.    
  6.     function __construct(Router $router, HttpContext $context,
  7.         Factory $controllerFactory, $error404Controller='Error404')
  8.     {
  9.         $this->router = $router;
  10.         $this->context = $context;
  11.         $this->factory = $controllerFactory;
  12.         $this->error404 = $error404Controller;
  13.     }
  14.    
  15.     function dispatch() {
  16.         $url = $this->getUrl();
  17.         $method = 'defaultAction';
  18.         $ctrl = null;
  19.         try {
  20.             if ( $url == '/' ) {
  21.                 $ctrl = $this->factory->produce('Index');
  22.             } else {
  23.                 $route = $this->router->findRoute($url);
  24.                 $ctrl = $this->factory->produce($route->getMapClass());
  25.                 if ( $route->getMapMethod() ) {
  26.                     if ( method_exists($ctrl, $route->getMapMethod()) ) {
  27.                         $method = $route->getMapMethod();
  28.                     } else {
  29.                         $ctrl = $this->factory->produce($this->error404);
  30.                     }
  31.                 }
  32.             }
  33.         } catch ( ClassLoaderException $e ) {
  34.             $ctrl = $this->factory->produce($this->error404);
  35.         }
  36.         $this->executeAction($ctrl, $method);
  37.     }
  38.    
  39.     protected function executeAction(Action $ctrl, $method) {
  40.         $ctrl->$method();
  41.     }
  42.    
  43.     protected function getUrl() {
  44.         $url = $this->context->getServer()->getStr('REQUEST_URI', '/');
  45.         $parts = explode('?', $url);
  46.         return (sizeof($parts) > 1? $parts[0] : $url);
  47.     }
  48.    
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement