ソースを参照

Make priority a configuration value.

Default to the property (which is now protected) but allow users of
a filter to redefine the priority.
mark_story 12 年 前
コミット
c0666fbd8e

+ 13 - 3
src/Routing/DispatcherFilter.php

@@ -65,7 +65,7 @@ class DispatcherFilter implements EventListener {
  *
  * @var int
  */
-	public $priority = 10;
+	protected $_priority = 10;
 
 /**
  * Default config
@@ -79,6 +79,7 @@ class DispatcherFilter implements EventListener {
 	protected $_defaultConfig = [
 		'when' => null,
 		'for' => null,
+		'priority' => null,
 	];
 
 /**
@@ -87,6 +88,9 @@ class DispatcherFilter implements EventListener {
  * @param array $config Settings for the filter.
  */
 	public function __construct($config = []) {
+		if (!isset($config['priority'])) {
+			$config['priority'] = $this->_priority;
+		}
 		$this->config($config);
 		if (isset($config['when']) && !is_callable($config['when'])) {
 			throw new Error\Exception('"when" conditions must be a callable.');
@@ -104,8 +108,14 @@ class DispatcherFilter implements EventListener {
  */
 	public function implementedEvents() {
 		return array(
-			'Dispatcher.beforeDispatch' => array('callable' => 'handle', 'priority' => $this->priority),
-			'Dispatcher.afterDispatch' => array('callable' => 'handle', 'priority' => $this->priority),
+			'Dispatcher.beforeDispatch' => [
+				'callable' => 'handle',
+				'priority' => $this->_config['priority']
+			],
+			'Dispatcher.afterDispatch' => [
+				'callable' => 'handle',
+				'priority' => $this->_config['priority']
+			],
 		);
 	}
 

+ 1 - 1
src/Routing/Filter/AssetDispatcher.php

@@ -36,7 +36,7 @@ class AssetDispatcher extends DispatcherFilter {
  *
  * @var int
  */
-	public $priority = 9;
+	protected $_priority = 9;
 
 /**
  * Checks if a requested asset exists and sends it to the browser

+ 1 - 1
src/Routing/Filter/CacheDispatcher.php

@@ -33,7 +33,7 @@ class CacheDispatcher extends DispatcherFilter {
  *
  * @var int
  */
-	public $priority = 9;
+	protected $_priority = 9;
 
 /**
  * Checks whether the response was cached and set the body accordingly.

+ 25 - 0
tests/TestCase/Routing/DispatcherFilterTest.php

@@ -36,6 +36,31 @@ class DispatcherFilterTest extends TestCase {
 	}
 
 /**
+ * Test setting priority
+ *
+ * @return void
+ */
+	public function testConstructPriority() {
+		$filter = new DispatcherFilter();
+		$this->assertEquals(10, $filter->config('priority'));
+
+		$filter = new DispatcherFilter(['priority' => 100]);
+		$this->assertEquals(100, $filter->config('priority'));
+	}
+
+/**
+ * Test implemented events
+ *
+ * @return void
+ */
+	public function testImplementedEvents() {
+		$filter = new DispatcherFilter(['priority' => 100]);
+		$events = $filter->implementedEvents();
+		$this->assertEquals(100, $events['Dispatcher.beforeDispatch']['priority']);
+		$this->assertEquals(100, $events['Dispatcher.afterDispatch']['priority']);
+	}
+
+/**
  * Test constructor error invalid when
  *
  * @expectedException Cake\Error\Exception