Browse Source

Make View::$plugin protected and add getter/setter for it.

ADmad 7 years ago
parent
commit
19795f69c3

+ 2 - 2
src/Mailer/Email.php

@@ -2739,9 +2739,9 @@ class Email implements JsonSerializable, Serializable
         list($templatePlugin) = pluginSplit($View->getTemplate());
         list($layoutPlugin) = pluginSplit($View->getLayout());
         if ($templatePlugin) {
-            $View->plugin = $templatePlugin;
+            $View->setPlugin($templatePlugin);
         } elseif ($layoutPlugin) {
-            $View->plugin = $layoutPlugin;
+            $View->setPlugin($layoutPlugin);
         }
 
         if ($View->get('content') === null) {

+ 3 - 3
src/View/HelperRegistry.php

@@ -66,8 +66,8 @@ class HelperRegistry extends ObjectRegistry implements EventDispatcherInterface
         try {
             $this->load($helper);
         } catch (Exception\MissingHelperException $exception) {
-            if ($this->_View->plugin) {
-                $this->load($this->_View->plugin . '.' . $helper);
+            if ($this->_View->getPlugin()) {
+                $this->load($this->_View->getPlugin() . '.' . $helper);
 
                 return true;
             }
@@ -146,7 +146,7 @@ class HelperRegistry extends ObjectRegistry implements EventDispatcherInterface
         $instance = new $class($this->_View, $settings);
 
         $instance->theme = $this->_View->getTheme();
-        $instance->plugin = $this->_View->plugin;
+        $instance->plugin = $this->_View->getRequest()->getParam('Plugin');
 
         $enable = isset($settings['enabled']) ? $settings['enabled'] : true;
         if ($enable) {

+ 27 - 2
src/View/View.php

@@ -99,9 +99,9 @@ class View implements EventDispatcherInterface
     /**
      * The name of the plugin.
      *
-     * @var string
+     * @var string|null
      */
-    public $plugin;
+    protected $plugin;
 
     /**
      * Name of the controller that created the View if any.
@@ -1200,6 +1200,7 @@ class View implements EventDispatcherInterface
             'request' => 'getRequest',
             'response' => 'getResponse',
             'subDir' => 'getSubdir',
+            'plugin' => 'getPlugin',
         ];
         if (isset($protected[$name])) {
             $method = $protected[$name];
@@ -1276,6 +1277,7 @@ class View implements EventDispatcherInterface
             'request' => 'setRequest',
             'response' => 'setResponse',
             'subDir' => 'setSubDir',
+            'plugin' => 'setPlugin',
             'elementCache' => 'setElementCache',
         ];
         if (isset($protected[$name])) {
@@ -1451,6 +1453,29 @@ class View implements EventDispatcherInterface
     }
 
     /**
+     * Returns the plugin name.
+     *
+     * @return string|null
+     */
+    public function getPlugin()
+    {
+        return $this->plugin;
+    }
+
+    /**
+     * Sets the plugin name.
+     *
+     * @param string $name Plugin name.
+     * @return $this
+     */
+    public function setPlugin($name)
+    {
+        $this->plugin = $name;
+
+        return $this;
+    }
+
+    /**
      * Set The cache configuration View will use to store cached elements
      *
      * @param string $elementCache Cache config name.

+ 1 - 1
tests/TestCase/Error/DebuggerTest.php

@@ -356,7 +356,6 @@ class DebuggerTest extends TestCase
         $result = Debugger::exportVar($View);
         $expected = <<<TEXT
 object(Cake\View\View) {
-	plugin => null
 	name => ''
 	passedArgs => []
 	uuids => []
@@ -369,6 +368,7 @@ object(Cake\View\View) {
 	string => '  '
 	[protected] _helpers => object(Cake\View\HelperRegistry) {}
 	[protected] Blocks => object(Cake\View\ViewBlock) {}
+	[protected] plugin => null
 	[protected] helpers => [
 		(int) 0 => 'Html',
 		(int) 1 => 'Form'

+ 1 - 1
tests/TestCase/View/HelperRegistryTest.php

@@ -100,7 +100,7 @@ class HelperRegistryTest extends TestCase
         $result = $this->Helpers->Form;
         $this->assertInstanceOf('Cake\View\Helper\FormHelper', $result);
 
-        $this->View->plugin = 'TestPlugin';
+        $this->View->setPlugin('TestPlugin');
         Plugin::load(['TestPlugin']);
         $result = $this->Helpers->OtherHelper;
         $this->assertInstanceOf('TestPlugin\View\Helper\OtherHelperHelper', $result);

+ 1 - 1
tests/TestCase/View/ViewBuilderTest.php

@@ -245,7 +245,7 @@ class ViewBuilderTest extends TestCase
         $this->assertEquals('default', $view->getLayout());
         $this->assertEquals('Articles/', $view->getTemplatePath());
         $this->assertEquals('Admin/', $view->getLayoutPath());
-        $this->assertEquals('TestPlugin', $view->plugin);
+        $this->assertEquals('TestPlugin', $view->getPlugin());
         $this->assertEquals('TestTheme', $view->getTheme());
         $this->assertSame($request, $view->getRequest());
         $this->assertInstanceOf(Response::class, $view->getResponse());

+ 4 - 4
tests/TestCase/View/ViewTest.php

@@ -737,7 +737,7 @@ class ViewTest extends TestCase
         $result = $View->getLayoutFileName('TestPlugin.default');
         $this->assertPathEquals($expected, $result);
 
-        $View->plugin = 'TestPlugin';
+        $View->setRequest($View->getRequest()->withParam('plugin', 'TestPlugin'));
         $expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS . 'src' . DS .
             'Template' . DS . 'Layout' . DS . 'default.ctp';
         $result = $View->getLayoutFileName('default');
@@ -888,7 +888,7 @@ class ViewTest extends TestCase
         $result = $this->View->elementExists('TestPlugin.element');
         $this->assertFalse($result);
 
-        $this->View->plugin = 'TestPlugin';
+        $this->View->setRequest($this->View->getRequest()->withParam('plugin', 'TestPlugin'));
         $result = $this->View->elementExists('plugin_element');
         $this->assertTrue($result);
     }
@@ -906,7 +906,7 @@ class ViewTest extends TestCase
         $result = $this->View->element('TestPlugin.plugin_element');
         $this->assertEquals("Element in the TestPlugin\n", $result);
 
-        $this->View->plugin = 'TestPlugin';
+        $this->View->setRequest($this->View->getRequest()->withParam('plugin', 'TestPlugin'));
         $result = $this->View->element('plugin_element');
         $this->assertEquals("Element in the TestPlugin\n", $result);
 
@@ -928,7 +928,7 @@ class ViewTest extends TestCase
         $result = $this->View->element('TestPlugin.plugin_element');
         $this->assertEquals('this is the plugin prefixed element using params[plugin]', $result);
 
-        $this->View->plugin = 'TestPlugin';
+        $this->View->setRequest($this->View->getRequest()->withParam('plugin', 'TestPlugin'));
         $result = $this->View->element('test_plugin_element');
         $this->assertEquals('this is the test set using View::$plugin plugin prefixed element', $result);