Skip to content

Commit

Permalink
Add setter and getter for autoRender flag.
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Pustułka authored and robertpustulka committed Nov 20, 2017
1 parent 4fdab63 commit 0b6e5ad
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
27 changes: 26 additions & 1 deletion src/Controller/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class Controller implements EventListenerInterface, EventDispatcherInterface
*
* @var bool
*/
public $autoRender = true;
protected $autoRender = true;

/**
* Instance of ComponentRegistry used to create Components
Expand Down Expand Up @@ -337,6 +337,7 @@ public function __get($name)
$deprecated = [
'name' => 'getName',
'plugin' => 'getPlugin',
'autoRender' => 'isAutoRenderEnabled',
];
if (isset($deprecated[$name])) {
$method = $deprecated[$name];
Expand Down Expand Up @@ -379,6 +380,7 @@ public function __set($name, $value)
$deprecated = [
'name' => 'setName',
'plugin' => 'setPlugin',
'autoRender' => 'enableAutoRender',
];
if (isset($deprecated[$name])) {
$method = $deprecated[$name];
Expand Down Expand Up @@ -452,6 +454,29 @@ public function setPlugin($plugin)
return $this;
}

/**
* Returns true if an action should be rendered automatically.
*
* @return bool
*/
public function isAutoRenderEnabled()
{
return $this->autoRender;
}

/**
* Enable or disbale automatic action rendering.
*
* @param bool $autoRender Flag.
* @return $this
*/
public function enableAutoRender($autoRender = true)
{
$this->autoRender = $autoRender;

return $this;
}

/**
* Gets the request instance.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Http/ActionDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ protected function _invoke(Controller $controller)
throw new LogicException('Controller actions can only return Cake\Http\Response or null.');
}

if (!$response && $controller->autoRender) {
if (!$response && $controller->isAutoRenderEnabled()) {
$controller->render();
}

Expand Down
17 changes: 17 additions & 0 deletions tests/TestCase/Controller/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1154,6 +1154,23 @@ public function testResponse()
$this->assertSame($response, $controller->getResponse());
}

/**
* Test autoRender getter and setter.
*
* @return void
*/
public function testAutoRender()
{
$controller = new PostsController();
$this->assertTrue($controller->isAutoRenderEnabled());

$this->assertSame($controller, $controller->enableAutoRender(false));
$this->assertFalse($controller->isAutoRenderEnabled());

$this->assertSame($controller, $controller->enableAutoRender());
$this->assertTrue($controller->isAutoRenderEnabled());
}

/**
* Tests deprecated view propertiyes work
*
Expand Down

0 comments on commit 0b6e5ad

Please sign in to comment.