Пример #1
0
 /**
  * STAGE 2: Find the right action and execute it.
  */
 public static function stage2($frontController)
 {
     /////////////////////////////
     // ==> SECTION: mvc <==
     $frontController->returnResponse(true);
     // return the response (do not echo it to the browser)
     // show exceptions immediately, instead of adding to the response
     $frontController->throwExceptions(true);
     // without this no exceptions are thrown
     $config = self::$registry['config'];
     require 'lib/Controller/Action.php';
     // ZFDemo's customized Zend_Controller_Action
     require_once 'Zend/Controller/Request/Http.php';
     $request = new Zend_Controller_Request_Http();
     require_once 'Zend/Controller/Response/Http.php';
     $response = new Zend_Controller_Response_Http();
     $response->append('body', '');
     // initialize a body segment
     // safety shutoff, in case controllers are stuck in loop, each calling the other
     $maxDispatches = isset($config->maxDispatches) ? $config->maxDispatches : 5;
     // rerouteTo will designate either an error code (mapped to error controller), or module/controller/action
     $rerouteTo = $didRerouteTo = $rerouteToReason = null;
     if (isset(self::$registry['testDbFailed']) && substr($request->getRequestUri(), -12) !== '/index/reset') {
         // during STAGE 1, sanity check of the DB connection/tables failed, so reroute to informative page
         $rerouteTo = self::reroute($request, self::$registry['config']->testDbFailed, $frontController);
     }
     do {
         if ($rerouteTo) {
             // if a reroute was requested, process it now
             $didRerouteTo = self::reroute($request, $rerouteTo, $frontController, $rerouteToReason);
             $rerouteToReason = $rerouteTo = null;
         }
         /////////////////////////////
         // ==> SECTION: mvc <==
         try {
             // "Run" the configured MVC "program" - the calculated action of the selected controller
             $frontController->dispatch($request, $response);
         } catch (ZFDemo_Exception_Reroute $exception) {
             // action controller requested a reroute form of internal redirection
             $rerouteTo = $exception->getRouteTo();
             $suggestedHttpCode = $exception->getHttpCode();
             $rerouteToReason = 'Reroute: ' . $exception->getMessage() . '; ' . $exception->responseCodeAsText();
         } catch (Exception $exception) {
             // don't allow *any* exceptions to end this script, without handling them properly
             if (!$config->analyzeDispatchErrors) {
                 // we are not analyzing the errors, so
                 self::doExit(404, $exception);
                 // log event and exit
             }
             /////////////////////////////
             // ==> SECTION: except <==
             list($suggestedHttpCode, $rerouteToReason) = self::analyzeError($exception, $request, $response);
             // if we have not already tried dispatching this controller code
             if ($didRerouteTo['code'] !== $suggestedHttpCode) {
                 $rerouteTo = $suggestedHttpCode;
                 #$rerouteToReason = $exception->getMessage();
             } else {
                 // Already tried to dispatch the selected error controller, so now fail hard.
                 ZFDemo_Log::logDispatchFailure($didRerouteTo);
                 if (self::isLocalRequest() && $config->view->showLog) {
                     echo ZFDemo_Log::get($exception);
                 }
                 // Reader excercise: make output "pretty" when dispatching error controller fails
                 self::doExit($suggestedHttpCode, $exception, is_int($didRerouteTo['code']) && $didRerouteTo['code'] > 400 ? _('An additional error occurred while trying to use the error action controller.') : '');
             }
         }
     } while ($rerouteTo && --$maxDispatches);
     // now loop to dispatch the alternative controller (if any)
     if ($maxDispatches === 0) {
         ZFDemo_Log::log(_('ERROR Too many reroutes. Controllers stuck in loop?') . $request->getRequestUri());
     }
     /////////////////////////////
     // ==> SECTION: except <==
     if (isset($suggestedHttpCode) && $suggestedHttpCode != 200) {
         // something unusual happened during dispatch, like a 404 or 500 error
         self::setHeaderStatus($suggestedHttpCode);
     }
     /////////////////////////////
     // ==> SECTION: mvc <==
     $baseUrl = $request->getBaseUrl();
     // save for use in header/footer/site templates
     if (false === strpos($baseUrl, '/index.php')) {
         $baseUrl .= '/index.php';
     }
     self::$view->baseUrl = $baseUrl;
     if (ZFDemo::isLocalRequest()) {
         // Since this is only added for local network requests, this code
         // may remain in production applications to aid in ongoing development.
         // Add some helpful debugging information for inserting into HTML comments:
         self::$view->controller = $request->getControllerName();
         self::$view->module = $request->getModuleName();
         self::$view->action = $request->getActionName();
     }
     return $response;
 }