浏览代码

Bind DispatchFilter in the new ActionDispatcher.

Previously I removed this code as I thought the global dependencies were
a bit icky - they still are. However, without this code someone who
updates to the middleware stack cannot use any dispatch filters. This is
not desirable, as I'd like people to be able to upgrade their dispatch
filters piece by piece, and avoid a big rewrite.

This reverts commit 366484c270c1cd6a85bd5b8f03629380d192a0c3.
Mark Story 9 年之前
父节点
当前提交
34a0541824
共有 2 个文件被更改,包括 32 次插入0 次删除
  1. 5 0
      src/Http/ActionDispatcher.php
  2. 27 0
      tests/TestCase/Http/ActionDispatcherTest.php

+ 5 - 0
src/Http/ActionDispatcher.php

@@ -61,6 +61,11 @@ class ActionDispatcher
         if ($eventManager) {
             $this->eventManager($eventManager);
         }
+
+        // Compatibility with DispatcherFilters.
+        foreach (DispatcherFactory::filters() as $filter) {
+            $this->addFilter($filter);
+        }
         $this->factory = $factory ?: new ControllerFactory();
     }
 

+ 27 - 0
tests/TestCase/Http/ActionDispatcherTest.php

@@ -19,6 +19,7 @@ use Cake\Http\ActionDispatcher;
 use Cake\Network\Request;
 use Cake\Network\Response;
 use Cake\Network\Session;
+use Cake\Routing\DispatcherFactory;
 use Cake\Routing\Filter\ControllerFactoryFilter;
 use Cake\Routing\Router;
 use Cake\TestSuite\TestCase;
@@ -43,6 +44,17 @@ class ActionDispatcherTest extends TestCase
     }
 
     /**
+     * Teardown
+     *
+     * @return void
+     */
+    public function tearDown()
+    {
+        parent::tearDown();
+        DispatcherFactory::clear();
+    }
+
+    /**
      * Ensure the constructor args end up on the right protected properties.
      *
      * @return void
@@ -58,6 +70,21 @@ class ActionDispatcherTest extends TestCase
     }
 
     /**
+     * Ensure that filters connected to the DispatcherFactory are
+     * also applied
+     */
+    public function testDispatcherFactoryCompat()
+    {
+        $filter = $this->getMockBuilder('Cake\Routing\DispatcherFilter')
+            ->setMethods(['beforeDispatch', 'afterDispatch'])
+            ->getMock();
+        DispatcherFactory::add($filter);
+        $dispatcher = new ActionDispatcher();
+        $this->assertCount(1, $dispatcher->getFilters());
+        $this->assertSame($filter, $dispatcher->getFilters()[0]);
+    }
+
+    /**
      * Test adding routing filters
      *
      * @return void