Skip to content

Commit e298d36

Browse files
committed
Remove Cell::$template. Use cell's view builder's methods instead.
1 parent 2a1b223 commit e298d36

File tree

4 files changed

+75
-29
lines changed

4 files changed

+75
-29
lines changed

src/View/Cell.php

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,6 @@ abstract class Cell
5050
*/
5151
public $View;
5252

53-
/**
54-
* Name of the template that will be rendered.
55-
* This property is inflected from the action name that was invoked.
56-
*
57-
* @var string
58-
*/
59-
public $template;
60-
6153
/**
6254
* Automatically set to the name of a plugin.
6355
*
@@ -205,19 +197,17 @@ public function render($template = null)
205197
));
206198
}
207199

208-
$builder = $this->viewBuilder();
200+
$builder = $this->viewBuilder()->setLayout(false);
209201

210202
if ($template !== null &&
211203
strpos($template, '/') === false &&
212204
strpos($template, '.') === false
213205
) {
214206
$template = Inflector::underscore($template);
215207
}
216-
if ($template === null) {
217-
$template = $builder->getTemplate() ?: $this->template;
208+
if ($template !== null) {
209+
$builder->setTemplate($template);
218210
}
219-
$builder->setLayout(false)
220-
->setTemplate($template);
221211

222212
$className = get_class($this);
223213
$namePrefix = '\View\Cell\\';
@@ -294,6 +284,58 @@ public function __toString()
294284
}
295285
}
296286

287+
/**
288+
* Magic accessor for removed properties.
289+
*
290+
* @param string $name Property name
291+
* @return mixed
292+
*/
293+
public function __get($name)
294+
{
295+
$deprecated = [
296+
'template' => 'getTemplate',
297+
];
298+
if (isset($deprecated[$name])) {
299+
$method = $deprecated[$name];
300+
deprecationWarning(sprintf(
301+
'Cell::$%s is deprecated. Use $cell->viewBuilder()->%s() instead.',
302+
$name,
303+
$method
304+
));
305+
306+
return $this->viewBuilder()->{$method}();
307+
}
308+
309+
return $this->{$name};
310+
}
311+
312+
/**
313+
* Magic setter for removed properties.
314+
*
315+
* @param string $name Property name.
316+
* @param mixed $value Value to set.
317+
* @return void
318+
*/
319+
public function __set($name, $value)
320+
{
321+
$deprecated = [
322+
'template' => 'setTemplate',
323+
];
324+
if (isset($deprecated[$name])) {
325+
$method = $deprecated[$name];
326+
deprecationWarning(sprintf(
327+
'Cell::$%s is deprecated. Use $cell->viewBuilder()->%s() instead.',
328+
$name,
329+
$method
330+
));
331+
$this->viewBuilder()->{$method}($value);
332+
333+
return;
334+
}
335+
336+
$this->{$name} = $value;
337+
}
338+
297339
/**
298340
* Debug info.
299341
*
@@ -305,10 +347,10 @@ public function __debugInfo()
305347
'plugin' => $this->plugin,
306348
'action' => $this->action,
307349
'args' => $this->args,
308-
'template' => $this->template,
309350
'viewClass' => $this->viewClass,
310351
'request' => $this->request,
311352
'response' => $this->response,
353+
'viewBuilder' => $this->viewBuilder(),
312354
];
313355
}
314356
}

src/View/CellTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,10 @@ protected function _createCell($className, $action, $plugin, $options)
9494
{
9595
/* @var \Cake\View\Cell $instance */
9696
$instance = new $className($this->request, $this->response, $this->getEventManager(), $options);
97-
$instance->template = Inflector::underscore($action);
9897

9998
$builder = $instance->viewBuilder();
99+
$builder->setTemplate(Inflector::underscore($action));
100+
100101
if (!empty($plugin)) {
101102
$builder->setPlugin($plugin);
102103
}

tests/TestCase/View/CellTest.php

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function testCellRender()
7474
$cell = $this->View->cell('Articles::teaserList');
7575
$render = "{$cell}";
7676

77-
$this->assertEquals('teaser_list', $cell->template);
77+
$this->assertEquals('teaser_list', $cell->viewBuilder()->getTemplate());
7878
$this->assertContains('<h2>Lorem ipsum</h2>', $render);
7979
$this->assertContains('<h2>Usectetur adipiscing eli</h2>', $render);
8080
$this->assertContains('<h2>Topis semper blandit eu non</h2>', $render);
@@ -100,7 +100,7 @@ public function testCellImplictRenderWithError()
100100
set_error_handler($capture);
101101

102102
$cell = $this->View->cell('Articles::teaserList');
103-
$cell->template = 'nope';
103+
$cell->viewBuilder()->setTemplate('nope');
104104
$result = "{$cell}";
105105
}
106106

@@ -128,12 +128,12 @@ public function testDefaultCellAction()
128128
{
129129
$appCell = $this->View->cell('Articles');
130130

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

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

139139
/**
@@ -143,11 +143,13 @@ public function testDefaultCellAction()
143143
*/
144144
public function testSettingCellTemplateFromAction()
145145
{
146-
$appCell = $this->View->cell('Articles::customTemplate');
146+
$this->deprecated(function () {
147+
$appCell = $this->View->cell('Articles::customTemplate');
147148

148-
$this->assertContains('This is the alternate template', "{$appCell}");
149-
$this->assertEquals('alternate_teaser_list', $appCell->template);
150-
$this->assertEquals('alternate_teaser_list', $appCell->viewBuilder()->getTemplate());
149+
$this->assertContains('This is the alternate template', "{$appCell}");
150+
$this->assertEquals('alternate_teaser_list', $appCell->viewBuilder()->getTemplate());
151+
$this->assertEquals('alternate_teaser_list', $appCell->template);
152+
});
151153
}
152154

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

162164
$this->assertContains('Articles subdir custom_template_path template', "{$appCell}");
163-
$this->assertEquals('custom_template_path', $appCell->template);
165+
$this->assertEquals('custom_template_path', $appCell->viewBuilder()->getTemplate());
164166
$this->assertEquals('Cell/Articles/Subdir', $appCell->viewBuilder()->getTemplatePath());
165167
}
166168

@@ -287,7 +289,7 @@ public function testPluginNamespacedCell()
287289
public function testPluginCellAlternateTemplate()
288290
{
289291
$cell = $this->View->cell('TestPlugin.Dummy::echoThis', ['msg' => 'hello world!']);
290-
$cell->template = '../../Element/translate';
292+
$cell->viewBuilder()->setTemplate('../../Element/translate');
291293
$this->assertContains('This is a translatable string', "{$cell}");
292294
}
293295

@@ -479,9 +481,11 @@ public function testCachedRenderSimpleCustomTemplate()
479481
->with('cell_test_app_view_cell_articles_cell_customTemplate_default', "<h1>This is the alternate template</h1>\n");
480482
Cache::setConfig('default', $mock);
481483

482-
$cell = $this->View->cell('Articles::customTemplate', [], ['cache' => true]);
483-
$result = $cell->render();
484-
$this->assertContains('This is the alternate template', $result);
484+
$this->deprecated(function () {
485+
$cell = $this->View->cell('Articles::customTemplate', [], ['cache' => true]);
486+
$result = $cell->render();
487+
$this->assertContains('This is the alternate template', $result);
488+
});
485489

486490
Cache::drop('default');
487491
}

tests/test_app/TestApp/View/Cell/ArticlesCell.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ public function customTemplate()
7676
*/
7777
public function customTemplateViewBuilder()
7878
{
79-
$this->template = 'derp';
8079
$this->counter++;
8180
$this->viewBuilder()->setTemplate('alternate_teaser_list');
8281
}

0 commit comments

Comments
 (0)