Skip to content

Commit

Permalink
Support for disabling layouts in LayoutControllers
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Jul 28, 2014
1 parent 8188e9b commit 7845c62
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/View/LayoutController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ abstract class LayoutController extends ControllerKernel
*/
protected $_layout;

/**
* Disable rendering of the layout, and return the content only
*
* @var bool
*/
protected $_disableLayout = false;

/**
* Name of the area in your layout to insert the view model response
*
Expand Down Expand Up @@ -52,6 +59,38 @@ public function __toString()
return $this->layout()->render();
}

/**
* Check to see if the layout will be rendered
*
* @return bool
*/
public function isLayoutDisabled()
{
return (bool)$this->_disableLayout;
}

/**
* Disable rendering of the layout
*
* @return $this
*/
public function disableLayout()
{
$this->_disableLayout = true;
return $this;
}

/**
* Re-enable layout rendering
*
* @return $this
*/
public function enableLayout()
{
$this->_disableLayout = false;
return $this;
}

/**
* Capture and view model responses and insert them into the layout
*
Expand All @@ -69,13 +108,23 @@ public function handleResponse($response, $capturedOutput)

if($response instanceof RenderableInterface)
{
if($this->isLayoutDisabled())
{
return $response;
}

$this->layout()->insert($this->_contentName, $response);
return new Response($this->layout());
}

//Convert captured responses into renderable content objects
if($response === null)
{
if($this->isLayoutDisabled())
{
return new Renderable($capturedOutput);
}

$this->layout()->insert(
$this->_contentName,
new Renderable($capturedOutput)
Expand All @@ -86,6 +135,11 @@ public function handleResponse($response, $capturedOutput)
//Scalars should be assumed as content, and converted to renderables
if(is_scalar($response))
{
if($this->isLayoutDisabled())
{
return new Renderable($response);
}

$this->layout()->insert(
$this->_contentName,
new Renderable($response)
Expand Down
36 changes: 36 additions & 0 deletions tests/Cubex/View/LayoutControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ public function testBasics()
$this->assertContains($output, (string)$controller);
}

public function testDisableLayout()
{
$controller = $this->getMockForAbstractClass(
'\Cubex\View\LayoutController'
);
/**
* @var $controller LayoutController
*/
$this->assertFalse($controller->isLayoutDisabled());
$controller->disableLayout();
$this->assertTrue($controller->isLayoutDisabled());
$controller->enableLayout();
$this->assertFalse($controller->isLayoutDisabled());
}

/**
* @param $route
* @param $expect
Expand All @@ -48,6 +63,27 @@ public function testResponses($route, $expect)
$this->assertContains($expect, $response->getContent());
}

/**
* @param $route
* @param $expect
*
* @dataProvider responseProvider
*/
public function testResponsesWithNoLayout($route, $expect)
{
$controller = new \namespaced\TestLayoutController();
$controller->setCubex(new Cubex());
$controller->disableLayout();
$response = $controller->executeRoute(
Route::create($route),
Request::createFromGlobals(),
HttpKernelInterface::MASTER_REQUEST,
false
);

$this->assertEquals($expect, $response->getContent());
}

public function responseProvider()
{
return [
Expand Down

0 comments on commit 7845c62

Please sign in to comment.