Quellcode durchsuchen

Applied ZF-8594 patch to trunk

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@20246 44c647ce-9c0f-0410-b52a-842ac1e357ba
dasprid vor 16 Jahren
Ursprung
Commit
3bd1143063

+ 9 - 1
library/Zend/Controller/Front.php

@@ -907,7 +907,15 @@ class Zend_Controller_Front
             */
             $this->_plugins->routeStartup($this->_request);
 
-            $router->route($this->_request);
+            try {
+                $router->route($this->_request);
+            }  catch (Exception $e) {
+                if ($this->throwExceptions()) {
+                    throw $e;
+                }
+
+                $this->_response->setException($e);
+            }
 
             /**
             * Notify plugins of router completion

+ 35 - 3
library/Zend/Controller/Plugin/ErrorHandler.php

@@ -47,6 +47,11 @@ class Zend_Controller_Plugin_ErrorHandler extends Zend_Controller_Plugin_Abstrac
     const EXCEPTION_NO_ACTION = 'EXCEPTION_NO_ACTION';
 
     /**
+     * Const - No route exception; no routing was possible
+     */
+    const EXCEPTION_NO_ROUTE = 'EXCEPTION_NO_ROUTE';
+
+    /**
      * Const - Other Exception; exceptions thrown by application controllers
      */
     const EXCEPTION_OTHER = 'EXCEPTION_OTHER';
@@ -187,8 +192,28 @@ class Zend_Controller_Plugin_ErrorHandler extends Zend_Controller_Plugin_Abstrac
     }
 
     /**
-     * postDispatch() plugin hook -- check for exceptions and dispatch error
-     * handler if necessary
+     * Route shutdown hook -- Ccheck for router exceptions
+     * 
+     * @param Zend_Controller_Request_Abstract $request 
+     */
+    public function routeShutdown(Zend_Controller_Request_Abstract $request)
+    {
+        $this->_handleError($request);
+    }
+
+    /**
+     * Post dispatch hook -- check for exceptions and dispatch error handler if
+     * necessary
+     *
+     * @param Zend_Controller_Request_Abstract $request
+     */
+    public function postDispatch(Zend_Controller_Request_Abstract $request)
+    {
+        $this->_handleError($request);
+    }
+
+    /**
+     * Handle errors and exceptions
      *
      * If the 'noErrorHandler' front controller flag has been set,
      * returns early.
@@ -196,7 +221,7 @@ class Zend_Controller_Plugin_ErrorHandler extends Zend_Controller_Plugin_Abstrac
      * @param  Zend_Controller_Request_Abstract $request
      * @return void
      */
-    public function postDispatch(Zend_Controller_Request_Abstract $request)
+    protected function _handleError(Zend_Controller_Request_Abstract $request)
     {
         $frontController = Zend_Controller_Front::getInstance();
         if ($frontController->getParam('noErrorHandler')) {
@@ -225,6 +250,13 @@ class Zend_Controller_Plugin_ErrorHandler extends Zend_Controller_Plugin_Abstrac
             $exceptionType    = get_class($exception);
             $error->exception = $exception;
             switch ($exceptionType) {
+                case 'Zend_Controller_Router_Exception':
+                    if (404 == $exception->getCode()) {
+                        $error->type = self::EXCEPTION_NO_ROUTE;
+                    } else {
+                        $error->type = self::EXCEPTION_OTHER;
+                    }
+                    break;
                 case 'Zend_Controller_Dispatcher_Exception':
                     $error->type = self::EXCEPTION_NO_CONTROLLER;
                     break;

+ 8 - 0
library/Zend/Controller/Router/Rewrite.php

@@ -380,6 +380,8 @@ class Zend_Controller_Router_Rewrite extends Zend_Controller_Router_Abstract
         }
 
         // Find the matching route
+        $routeMatched = false;
+        
         foreach (array_reverse($this->_routes) as $name => $route) {
             // TODO: Should be an interface method. Hack for 1.0 BC
             if (method_exists($route, 'isAbstract') && $route->isAbstract()) {
@@ -396,10 +398,16 @@ class Zend_Controller_Router_Rewrite extends Zend_Controller_Router_Abstract
             if ($params = $route->match($match)) {
                 $this->_setRequestParams($request, $params);
                 $this->_currentRoute = $name;
+                $routeMatched        = true;
                 break;
             }
         }
 
+         if (!$routeMatched) {
+             require_once 'Zend/Controller/Router/Exception.php';
+             throw new Zend_Controller_Router_Exception('No route matched the request', 404);
+         }
+
         if($this->_useCurrentParamsAsGlobal) {
             $params = $request->getParams();
             foreach($params as $param => $value) {

+ 14 - 4
tests/Zend/Controller/Router/RewriteTest.php

@@ -263,10 +263,15 @@ class Zend_Controller_Router_RewriteTest extends PHPUnit_Framework_TestCase
 
         $this->_router->addRoute('default', new Zend_Controller_Router_Route(':controller/:action'));
 
-        $token = $this->_router->route($request);
+        try {
+            $token = $this->_router->route($request);
+            $this->fail('An expected Zend_Controller_Router_Exception was not raised');
+        } catch (Zend_Controller_Router_Exception $expected) {
+            $this->assertEquals('No route matched the request', $expected->getMessage());
+        }
 
-        $this->assertNull($token->getControllerName());
-        $this->assertNull($token->getActionName());
+        $this->assertNull($request->getControllerName());
+        $this->assertNull($request->getActionName());
     }
 
     public function testDefaultRouteMatched()
@@ -381,7 +386,12 @@ class Zend_Controller_Router_RewriteTest extends PHPUnit_Framework_TestCase
         $request = new Zend_Controller_Router_RewriteTest_Request('http://localhost/ctrl/act');
         $this->_router->removeDefaultRoutes();
 
-        $token = $this->_router->route($request);
+        try {
+            $token = $this->_router->route($request);
+            $this->fail('An expected Zend_Controller_Router_Exception was not raised');
+        } catch (Zend_Controller_Router_Exception $expected) {
+            $this->assertEquals('No route matched the request', $expected->getMessage());
+        }
 
         $routes = $this->_router->getRoutes();
         $this->assertEquals(0, count($routes));