Browse Source

Remove Cell::$template. Use cell's view builder's methods instead.

ADmad 7 years ago
parent
commit
e298d36893

+ 56 - 14
src/View/Cell.php

@@ -51,14 +51,6 @@ abstract class Cell
     public $View;
     public $View;
 
 
     /**
     /**
-     * Name of the template that will be rendered.
-     * This property is inflected from the action name that was invoked.
-     *
-     * @var string
-     */
-    public $template;
-
-    /**
      * Automatically set to the name of a plugin.
      * Automatically set to the name of a plugin.
      *
      *
      * @var string
      * @var string
@@ -205,7 +197,7 @@ abstract class Cell
                 ));
                 ));
             }
             }
 
 
-            $builder = $this->viewBuilder();
+            $builder = $this->viewBuilder()->setLayout(false);
 
 
             if ($template !== null &&
             if ($template !== null &&
                 strpos($template, '/') === false &&
                 strpos($template, '/') === false &&
@@ -213,11 +205,9 @@ abstract class Cell
             ) {
             ) {
                 $template = Inflector::underscore($template);
                 $template = Inflector::underscore($template);
             }
             }
-            if ($template === null) {
-                $template = $builder->getTemplate() ?: $this->template;
+            if ($template !== null) {
+                $builder->setTemplate($template);
             }
             }
-            $builder->setLayout(false)
-                ->setTemplate($template);
 
 
             $className = get_class($this);
             $className = get_class($this);
             $namePrefix = '\View\Cell\\';
             $namePrefix = '\View\Cell\\';
@@ -295,6 +285,58 @@ abstract class Cell
     }
     }
 
 
     /**
     /**
+     * Magic accessor for removed properties.
+     *
+     * @param string $name Property name
+     * @return mixed
+     */
+    public function __get($name)
+    {
+        $deprecated = [
+            'template' => 'getTemplate',
+        ];
+        if (isset($deprecated[$name])) {
+            $method = $deprecated[$name];
+            deprecationWarning(sprintf(
+                'Cell::$%s is deprecated. Use $cell->viewBuilder()->%s() instead.',
+                $name,
+                $method
+            ));
+
+            return $this->viewBuilder()->{$method}();
+        }
+
+        return $this->{$name};
+    }
+
+    /**
+     * Magic setter for removed properties.
+     *
+     * @param string $name Property name.
+     * @param mixed $value Value to set.
+     * @return void
+     */
+    public function __set($name, $value)
+    {
+        $deprecated = [
+            'template' => 'setTemplate',
+        ];
+        if (isset($deprecated[$name])) {
+            $method = $deprecated[$name];
+            deprecationWarning(sprintf(
+                'Cell::$%s is deprecated. Use $cell->viewBuilder()->%s() instead.',
+                $name,
+                $method
+            ));
+            $this->viewBuilder()->{$method}($value);
+
+            return;
+        }
+
+        $this->{$name} = $value;
+    }
+
+    /**
      * Debug info.
      * Debug info.
      *
      *
      * @return array
      * @return array
@@ -305,10 +347,10 @@ abstract class Cell
             'plugin' => $this->plugin,
             'plugin' => $this->plugin,
             'action' => $this->action,
             'action' => $this->action,
             'args' => $this->args,
             'args' => $this->args,
-            'template' => $this->template,
             'viewClass' => $this->viewClass,
             'viewClass' => $this->viewClass,
             'request' => $this->request,
             'request' => $this->request,
             'response' => $this->response,
             'response' => $this->response,
+            'viewBuilder' => $this->viewBuilder(),
         ];
         ];
     }
     }
 }
 }

+ 2 - 1
src/View/CellTrait.php

@@ -94,9 +94,10 @@ trait CellTrait
     {
     {
         /* @var \Cake\View\Cell $instance */
         /* @var \Cake\View\Cell $instance */
         $instance = new $className($this->request, $this->response, $this->getEventManager(), $options);
         $instance = new $className($this->request, $this->response, $this->getEventManager(), $options);
-        $instance->template = Inflector::underscore($action);
 
 
         $builder = $instance->viewBuilder();
         $builder = $instance->viewBuilder();
+        $builder->setTemplate(Inflector::underscore($action));
+
         if (!empty($plugin)) {
         if (!empty($plugin)) {
             $builder->setPlugin($plugin);
             $builder->setPlugin($plugin);
         }
         }

+ 17 - 13
tests/TestCase/View/CellTest.php

@@ -74,7 +74,7 @@ class CellTest extends TestCase
         $cell = $this->View->cell('Articles::teaserList');
         $cell = $this->View->cell('Articles::teaserList');
         $render = "{$cell}";
         $render = "{$cell}";
 
 
-        $this->assertEquals('teaser_list', $cell->template);
+        $this->assertEquals('teaser_list', $cell->viewBuilder()->getTemplate());
         $this->assertContains('<h2>Lorem ipsum</h2>', $render);
         $this->assertContains('<h2>Lorem ipsum</h2>', $render);
         $this->assertContains('<h2>Usectetur adipiscing eli</h2>', $render);
         $this->assertContains('<h2>Usectetur adipiscing eli</h2>', $render);
         $this->assertContains('<h2>Topis semper blandit eu non</h2>', $render);
         $this->assertContains('<h2>Topis semper blandit eu non</h2>', $render);
@@ -100,7 +100,7 @@ class CellTest extends TestCase
         set_error_handler($capture);
         set_error_handler($capture);
 
 
         $cell = $this->View->cell('Articles::teaserList');
         $cell = $this->View->cell('Articles::teaserList');
-        $cell->template = 'nope';
+        $cell->viewBuilder()->setTemplate('nope');
         $result = "{$cell}";
         $result = "{$cell}";
     }
     }
 
 
@@ -128,12 +128,12 @@ class CellTest extends TestCase
     {
     {
         $appCell = $this->View->cell('Articles');
         $appCell = $this->View->cell('Articles');
 
 
-        $this->assertEquals('display', $appCell->template);
+        $this->assertEquals('display', $appCell->viewBuilder()->getTemplate());
         $this->assertContains('dummy', "{$appCell}");
         $this->assertContains('dummy', "{$appCell}");
 
 
         $pluginCell = $this->View->cell('TestPlugin.Dummy');
         $pluginCell = $this->View->cell('TestPlugin.Dummy');
         $this->assertContains('dummy', "{$pluginCell}");
         $this->assertContains('dummy', "{$pluginCell}");
-        $this->assertEquals('display', $pluginCell->template);
+        $this->assertEquals('display', $pluginCell->viewBuilder()->getTemplate());
     }
     }
 
 
     /**
     /**
@@ -143,11 +143,13 @@ class CellTest extends TestCase
      */
      */
     public function testSettingCellTemplateFromAction()
     public function testSettingCellTemplateFromAction()
     {
     {
-        $appCell = $this->View->cell('Articles::customTemplate');
+        $this->deprecated(function () {
+            $appCell = $this->View->cell('Articles::customTemplate');
 
 
-        $this->assertContains('This is the alternate template', "{$appCell}");
-        $this->assertEquals('alternate_teaser_list', $appCell->template);
-        $this->assertEquals('alternate_teaser_list', $appCell->viewBuilder()->getTemplate());
+            $this->assertContains('This is the alternate template', "{$appCell}");
+            $this->assertEquals('alternate_teaser_list', $appCell->viewBuilder()->getTemplate());
+            $this->assertEquals('alternate_teaser_list', $appCell->template);
+        });
     }
     }
 
 
     /**
     /**
@@ -160,7 +162,7 @@ class CellTest extends TestCase
         $appCell = $this->View->cell('Articles::customTemplatePath');
         $appCell = $this->View->cell('Articles::customTemplatePath');
 
 
         $this->assertContains('Articles subdir custom_template_path template', "{$appCell}");
         $this->assertContains('Articles subdir custom_template_path template', "{$appCell}");
-        $this->assertEquals('custom_template_path', $appCell->template);
+        $this->assertEquals('custom_template_path', $appCell->viewBuilder()->getTemplate());
         $this->assertEquals('Cell/Articles/Subdir', $appCell->viewBuilder()->getTemplatePath());
         $this->assertEquals('Cell/Articles/Subdir', $appCell->viewBuilder()->getTemplatePath());
     }
     }
 
 
@@ -287,7 +289,7 @@ class CellTest extends TestCase
     public function testPluginCellAlternateTemplate()
     public function testPluginCellAlternateTemplate()
     {
     {
         $cell = $this->View->cell('TestPlugin.Dummy::echoThis', ['msg' => 'hello world!']);
         $cell = $this->View->cell('TestPlugin.Dummy::echoThis', ['msg' => 'hello world!']);
-        $cell->template = '../../Element/translate';
+        $cell->viewBuilder()->setTemplate('../../Element/translate');
         $this->assertContains('This is a translatable string', "{$cell}");
         $this->assertContains('This is a translatable string', "{$cell}");
     }
     }
 
 
@@ -479,9 +481,11 @@ class CellTest extends TestCase
             ->with('cell_test_app_view_cell_articles_cell_customTemplate_default', "<h1>This is the alternate template</h1>\n");
             ->with('cell_test_app_view_cell_articles_cell_customTemplate_default', "<h1>This is the alternate template</h1>\n");
         Cache::setConfig('default', $mock);
         Cache::setConfig('default', $mock);
 
 
-        $cell = $this->View->cell('Articles::customTemplate', [], ['cache' => true]);
-        $result = $cell->render();
-        $this->assertContains('This is the alternate template', $result);
+        $this->deprecated(function () {
+            $cell = $this->View->cell('Articles::customTemplate', [], ['cache' => true]);
+            $result = $cell->render();
+            $this->assertContains('This is the alternate template', $result);
+        });
 
 
         Cache::drop('default');
         Cache::drop('default');
     }
     }

+ 0 - 1
tests/test_app/TestApp/View/Cell/ArticlesCell.php

@@ -76,7 +76,6 @@ class ArticlesCell extends \Cake\View\Cell
      */
      */
     public function customTemplateViewBuilder()
     public function customTemplateViewBuilder()
     {
     {
-        $this->template = 'derp';
         $this->counter++;
         $this->counter++;
         $this->viewBuilder()->setTemplate('alternate_teaser_list');
         $this->viewBuilder()->setTemplate('alternate_teaser_list');
     }
     }