Skip to content

Commit

Permalink
Merge pull request #11 from bearsunday/204
Browse files Browse the repository at this point in the history
204 No Content
  • Loading branch information
koriym committed May 19, 2015
2 parents 497052e + 3af9683 commit 680d13c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
41 changes: 27 additions & 14 deletions src/TwigRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Madapaja\TwigModule;

use BEAR\Resource\Code;
use BEAR\Resource\RenderInterface;
use BEAR\Resource\ResourceObject;
use Ray\Aop\WeavedInterface;
Expand Down Expand Up @@ -44,33 +45,45 @@ public function render(ResourceObject $ro)
if (!isset($ro->headers['content-type'])) {
$ro->headers['content-type'] = 'text/html; charset=utf-8';
}
$template = $this->loadTemplate($ro);
if ($ro->code === Code::NO_CONTENT) {
$ro->view = '';
}
if ($ro->view === '') {
return '';
}
try {
$template = $this->loadTemplate($ro);
} catch (\Twig_Error_Loader $e) {
if ($ro->code !== 200) {
$ro->view = '';

return '';
}
throw new Exception\TemplateNotFound($e->getMessage(), 500, $e);
}
$ro->view = $template->render($ro->body);

return $ro->view;
}


/**
* @param ResourceObject $ro
* @return \Twig_TemplateInterface
*/
private function loadTemplate(ResourceObject $ro)
{
try {
$loader = $this->twig->getLoader();
if ($loader instanceof \Twig_Loader_Filesystem) {
list($file, $dir) = $this->getTemplate($ro, $loader->getPaths());
if ($dir) {
// if the file not in paths, register the directory
$loader->prependPath($dir);
}

return $this->twig->loadTemplate($file);
$loader = $this->twig->getLoader();
if ($loader instanceof \Twig_Loader_Filesystem) {
list($file, $dir) = $this->getTemplate($ro, $loader->getPaths());
if ($dir) {
// if the file not in paths, register the directory
$loader->prependPath($dir);
}
return $this->twig->loadTemplate($this->getReflection($ro)->name . self::EXT);
} catch (\Twig_Error_Loader $e) {
throw new Exception\TemplateNotFound($e->getMessage());

return $this->twig->loadTemplate($file);
}
return $this->twig->loadTemplate($this->getReflection($ro)->name . self::EXT);
}

/**
Expand Down
10 changes: 10 additions & 0 deletions tests/Resource/Page/FileLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ public function testTemplateNotFoundException()
$prop->getValue($ro)->render($ro);
}

public function testNoViewWhenCode301()
{
$ro = $this->injector->getInstance(NoTemplate::class);
$ro->code = 303;
$prop = (new \ReflectionClass($ro))->getProperty('renderer');
$prop->setAccessible(true);
$view = $prop->getValue($ro)->render($ro);
$this->assertSame('', $view);
}

public function testPage()
{
$ro = $this->injector->getInstance(Page::class);
Expand Down

0 comments on commit 680d13c

Please sign in to comment.