Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class CommonDispatcher implements Dispatcher {
- private $router,$error404;
- protected $context;
- protected $factory;
- function __construct(Router $router, HttpContext $context,
- Factory $controllerFactory, $error404Controller='Error404')
- {
- $this->router = $router;
- $this->context = $context;
- $this->factory = $controllerFactory;
- $this->error404 = $error404Controller;
- }
- function dispatch() {
- $url = $this->getUrl();
- $method = 'defaultAction';
- $ctrl = null;
- try {
- if ( $url == '/' ) {
- $ctrl = $this->factory->produce('Index');
- } else {
- $route = $this->router->findRoute($url);
- $ctrl = $this->factory->produce($route->getMapClass());
- if ( $route->getMapMethod() ) {
- if ( method_exists($ctrl, $route->getMapMethod()) ) {
- $method = $route->getMapMethod();
- } else {
- $ctrl = $this->factory->produce($this->error404);
- }
- }
- }
- } catch ( ClassLoaderException $e ) {
- $ctrl = $this->factory->produce($this->error404);
- }
- $this->executeAction($ctrl, $method);
- }
- protected function executeAction(Action $ctrl, $method) {
- $ctrl->$method();
- }
- protected function getUrl() {
- $url = $this->context->getServer()->getStr('REQUEST_URI', '/');
- $parts = explode('?', $url);
- return (sizeof($parts) > 1? $parts[0] : $url);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement