Skip to content

Commit

Permalink
Remove Cell::$template. Use cell's view builder's methods instead.
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Jun 7, 2018
1 parent 2a1b223 commit e298d36
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 29 deletions.
70 changes: 56 additions & 14 deletions src/View/Cell.php
Expand Up @@ -50,14 +50,6 @@ abstract class Cell
*/
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.
*
Expand Down Expand Up @@ -205,19 +197,17 @@ public function render($template = null)
));
}

$builder = $this->viewBuilder();
$builder = $this->viewBuilder()->setLayout(false);

if ($template !== null &&
strpos($template, '/') === false &&
strpos($template, '.') === false
) {
$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);
$namePrefix = '\View\Cell\\';
Expand Down Expand Up @@ -294,6 +284,58 @@ public function __toString()
}
}

/**
* 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.
*
Expand All @@ -305,10 +347,10 @@ public function __debugInfo()
'plugin' => $this->plugin,
'action' => $this->action,
'args' => $this->args,
'template' => $this->template,
'viewClass' => $this->viewClass,
'request' => $this->request,
'response' => $this->response,
'viewBuilder' => $this->viewBuilder(),
];
}
}
3 changes: 2 additions & 1 deletion src/View/CellTrait.php
Expand Up @@ -94,9 +94,10 @@ protected function _createCell($className, $action, $plugin, $options)
{
/* @var \Cake\View\Cell $instance */
$instance = new $className($this->request, $this->response, $this->getEventManager(), $options);
$instance->template = Inflector::underscore($action);

$builder = $instance->viewBuilder();
$builder->setTemplate(Inflector::underscore($action));

if (!empty($plugin)) {
$builder->setPlugin($plugin);
}
Expand Down
30 changes: 17 additions & 13 deletions tests/TestCase/View/CellTest.php
Expand Up @@ -74,7 +74,7 @@ public function testCellRender()
$cell = $this->View->cell('Articles::teaserList');
$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>Usectetur adipiscing eli</h2>', $render);
$this->assertContains('<h2>Topis semper blandit eu non</h2>', $render);
Expand All @@ -100,7 +100,7 @@ public function testCellImplictRenderWithError()
set_error_handler($capture);

$cell = $this->View->cell('Articles::teaserList');
$cell->template = 'nope';
$cell->viewBuilder()->setTemplate('nope');
$result = "{$cell}";
}

Expand Down Expand Up @@ -128,12 +128,12 @@ public function testDefaultCellAction()
{
$appCell = $this->View->cell('Articles');

$this->assertEquals('display', $appCell->template);
$this->assertEquals('display', $appCell->viewBuilder()->getTemplate());
$this->assertContains('dummy', "{$appCell}");

$pluginCell = $this->View->cell('TestPlugin.Dummy');
$this->assertContains('dummy', "{$pluginCell}");
$this->assertEquals('display', $pluginCell->template);
$this->assertEquals('display', $pluginCell->viewBuilder()->getTemplate());
}

/**
Expand All @@ -143,11 +143,13 @@ public function testDefaultCellAction()
*/
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);
});
}

/**
Expand All @@ -160,7 +162,7 @@ public function testSettingCellTemplatePathFromAction()
$appCell = $this->View->cell('Articles::customTemplatePath');

$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());
}

Expand Down Expand Up @@ -287,7 +289,7 @@ public function testPluginNamespacedCell()
public function testPluginCellAlternateTemplate()
{
$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}");
}

Expand Down Expand Up @@ -479,9 +481,11 @@ public function testCachedRenderSimpleCustomTemplate()
->with('cell_test_app_view_cell_articles_cell_customTemplate_default', "<h1>This is the alternate template</h1>\n");
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');
}
Expand Down
1 change: 0 additions & 1 deletion tests/test_app/TestApp/View/Cell/ArticlesCell.php
Expand Up @@ -76,7 +76,6 @@ public function customTemplate()
*/
public function customTemplateViewBuilder()
{
$this->template = 'derp';
$this->counter++;
$this->viewBuilder()->setTemplate('alternate_teaser_list');
}
Expand Down

0 comments on commit e298d36

Please sign in to comment.