Browse Source

Don't push view instance into widgets list.

ADmad 6 years ago
parent
commit
5a6eafac77
2 changed files with 25 additions and 9 deletions
  1. 14 3
      src/View/Widget/WidgetLocator.php
  2. 11 6
      tests/TestCase/View/Widget/WidgetLocatorTest.php

+ 14 - 3
src/View/Widget/WidgetLocator.php

@@ -55,6 +55,13 @@ class WidgetLocator
     protected $_templates;
 
     /**
+     * View instance.
+     *
+     * @var \Cake\View\View
+     */
+    protected $_view;
+
+    /**
      * Constructor
      *
      * @param \Cake\View\StringTemplate $templates Templates instance to use.
@@ -64,6 +71,8 @@ class WidgetLocator
     public function __construct(StringTemplate $templates, View $view, array $widgets = [])
     {
         $this->_templates = $templates;
+        $this->_view = $view;
+
         if (!empty($widgets)) {
             $this->add($widgets);
             foreach ($this->_widgets as $key => $widget) {
@@ -73,7 +82,6 @@ class WidgetLocator
                 }
             }
         }
-        $this->_widgets['_view'] = $view;
     }
 
     /**
@@ -135,7 +143,6 @@ class WidgetLocator
      *
      * @param string $name The widget name to get.
      * @return object WidgetInterface instance.
-     *  or \Cake\View\View instance in case of special name "_view".
      * @throws \RuntimeException when widget is undefined.
      * @throws \ReflectionException
      */
@@ -190,7 +197,11 @@ class WidgetLocator
             $reflection = new ReflectionClass($className);
             $arguments = [$this->_templates];
             foreach ($widget as $requirement) {
-                $arguments[] = $this->get($requirement);
+                if ($requirement === '_view') {
+                    $arguments[] = $this->_view;
+                } else {
+                    $arguments[] = $this->get($requirement);
+                }
             }
             $instance = $reflection->newInstanceArgs($arguments);
         } else {

+ 11 - 6
tests/TestCase/View/Widget/WidgetLocatorTest.php

@@ -20,6 +20,7 @@ use Cake\TestSuite\TestCase;
 use Cake\View\StringTemplate;
 use Cake\View\View;
 use Cake\View\Widget\WidgetLocator;
+use TestApp\View\Widget\TestUsingViewWidget;
 
 /**
  * WidgetLocator test case
@@ -68,16 +69,20 @@ class WidgetLocatorTest extends TestCase
     }
 
     /**
-     * Test getting view instance from locator.
+     * Test that view instance is property passed to widget constructor which need it.
      *
      * @return void
      */
-    public function testGetViewInstance()
+    public function testGeneratingWidgetUsingViewInstance()
     {
-        $inputs = new WidgetLocator($this->templates, $this->view, []);
-
-        $result = $inputs->get('_view');
-        $this->assertInstanceOf('Cake\View\View', $result);
+        $inputs = new WidgetLocator(
+            $this->templates,
+            $this->view,
+            ['test' => [TestUsingViewWidget::class, '_view']]
+        );
+
+        $widget = $inputs->get('test');
+        $this->assertInstanceOf(View::class, $widget->getView());
     }
 
     /**