Browse Source

Do instanceof check when adding to registry instead of when accessing.

ADmad 11 years ago
parent
commit
26148db0a1
2 changed files with 15 additions and 12 deletions
  1. 15 11
      src/View/Widget/WidgetRegistry.php
  2. 0 1
      tests/TestCase/View/Widget/WidgetRegistryTest.php

+ 15 - 11
src/View/Widget/WidgetRegistry.php

@@ -80,7 +80,7 @@ class WidgetRegistry {
 				$this->add($widgets);
 			}
 		}
-		$this->add(['_view' => $view]);
+		$this->_widgets['_view'] = $view;
 	}
 
 /**
@@ -106,7 +106,7 @@ class WidgetRegistry {
  *
  * {{{
  * $registry->add([
- *   'label' => new MyLabel($templates),
+ *   'label' => new MyLabelWidget($templates),
  *   'checkbox' => ['Fancy.MyCheckbox', 'label']
  * ]);
  * }}}
@@ -117,8 +117,18 @@ class WidgetRegistry {
  *
  * @param array $widgets Array of widgets to use.
  * @return void
+ * @throws \RuntimeException When class does not implement WidgetInterface.
  */
 	public function add(array $widgets) {
+		foreach ($widgets as $object) {
+			if (gettype($object) === 'object' &&
+				!($object instanceof WidgetInterface)
+			) {
+				throw new \RuntimeException(
+					'Widget objects must implement Cake\View\Widget\WidgetInterface.'
+				);
+			}
+		}
 		$this->_widgets = $widgets + $this->_widgets;
 	}
 
@@ -160,20 +170,14 @@ class WidgetRegistry {
  * @param mixed $widget The widget to get
  * @return WidgetInterface
  * @throws \RuntimeException when class cannot be loaded or does not
- *   implement WidgetInterface or not a View instance.
+ *   implement WidgetInterface.
  */
 	protected function _resolveWidget($widget) {
 		$type = gettype($widget);
-		if ($type === 'object' &&
-			($widget instanceof WidgetInterface || $widget instanceof View)
-		) {
-			return $widget;
-		}
 		if ($type === 'object') {
-			throw new \RuntimeException(
-				'Widget objects must implement Cake\View\Widget\WidgetInterface.'
-			);
+			return $widget;
 		}
+
 		if ($type === 'string') {
 			$widget = [$widget];
 		}

+ 0 - 1
tests/TestCase/View/Widget/WidgetRegistryTest.php

@@ -122,7 +122,6 @@ class WidgetRegistryTestCase extends TestCase {
 		$inputs->add([
 			'text' => new \StdClass()
 		]);
-		$inputs->get('text');
 	}
 
 /**