public function dispatch()
    {
        $request  = Container::getRequest();
        $response = Container::getResponse();
        
        PluginManager::handleApplicationPlugins(PluginManager::STEP_BEFORE_DISPATCH_LOOP);
        //self::debug('Begin dispatch loop...');
        while (!$this->dispatched) {
            
            try {
                PluginManager::handleApplicationPlugins(PluginManager::STEP_BEFORE_ACTION);
                //self::debug('Begin dispatch ' . $request->getController() . '::' . $request->getAction() . ' [' . json_encode($request->getParams()) . ']');
                $this->dispatchProcess($request, $response);
                //self::debug('End dispatch ' . $request->getController() . '::' . $request->getAction() . ' [' . json_encode($request->getParams()) . ']');
                PluginManager::handleApplicationPlugins(PluginManager::STEP_AFTER_ACTION);
            }
            catch (Exception $e) {
                if ($request->getController(false) == 'event' 
                 && $request->getAction(false) == 'error') {
                    throw $e;
                }
                Container::getView()->addValue('exception', $e);
                $this->renderView = true;
                $request->setController('event')->setAction('error');
                $this->setDispatched(false);
            }
        }
        PluginManager::handleApplicationPlugins(PluginManager::STEP_AFTER_DISPATCH_LOOP);
        self::debug('End dispatch loop');
        if ($this->dispatched && $this->renderLayout) {
            $layout = Container::getConfig()->getConfig('layout');
            if ($layout !== null) {
                if (!file_exists($layout)) {
                    throw new ArchException('Layout file ' . $layout . ' not found');
                }
                $response->setBody(Container::getLayout(true)->render($layout));
            }
        }
        if ($this->renderResponse) {
            $response->fixHeaders()->putTypeHeadersInResponse();
            foreach ($response->getHeaders() as $header) {
                header($header);
            }
            $response->executeCallback();
            if ($response->hasBody()) {
                echo $response->getBody();
            }
        }
        return $this;
    }