Browse Source

Fix inability to get View instance from widget registry.

Even though View instance was added to registry it could not be retrieved as
WidgetRegistry::_resolveWidget() throw exception if object did not implement
WidgetInterface.
ADmad 11 years ago
parent
commit
f3e64e180b

+ 5 - 3
src/View/Widget/WidgetRegistry.php

@@ -160,16 +160,18 @@ class WidgetRegistry {
  * @param mixed $widget The widget to get
  * @return WidgetInterface
  * @throws \RuntimeException when class cannot be loaded or does not
- *   implement WidgetInterface.
+ *   implement WidgetInterface or not a View instance.
  */
 	protected function _resolveWidget($widget) {
 		$type = gettype($widget);
-		if ($type === 'object' && $widget instanceof WidgetInterface) {
+		if ($type === 'object' &&
+			($widget instanceof WidgetInterface || $widget instanceof View)
+		) {
 			return $widget;
 		}
 		if ($type === 'object') {
 			throw new \RuntimeException(
-				'Input objects must implement Cake\View\Widget\WidgetInterface.'
+				'Widget objects must implement Cake\View\Widget\WidgetInterface.'
 			);
 		}
 		if ($type === 'string') {

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

@@ -55,6 +55,18 @@ class WidgetRegistryTestCase extends TestCase {
 	}
 
 /**
+ * Test getting view instance from registry.
+ *
+ * @return void
+ */
+	public function testGetViewInstance() {
+		$inputs = new WidgetRegistry($this->templates, $this->view, []);
+
+		$result = $inputs->get('_view');
+		$this->assertInstanceOf('Cake\View\View', $result);
+	}
+
+/**
  * Test loading widgets files in the app.
  *
  * @return void