refactor render template process

This commit is contained in:
Manuel Raynaud
2013-11-06 13:48:32 +01:00
parent 2d291cf5a0
commit 6e5ae0f634
6 changed files with 34 additions and 44 deletions

View File

@@ -199,7 +199,7 @@ class BaseAdminController extends BaseController
$parser = $this->container->get("thelia.parser");
// Define the template thant shoud be used
$parser->addTemplateDir($template ?: ConfigQuery::read('base-admin-template', 'admin/default'), 'default');
$parser->setTemplate($template ?: ConfigQuery::read('base-admin-template', 'admin/default'), 'default');
return $parser;
}

View File

@@ -311,6 +311,11 @@ class BaseController extends ContainerAware
}
/**
*
* return an instance of SmartyParser
*
* Caution : maybe there is still not default template defined.
*
* @return ParserInterface instance parser
*/
protected function getParser()

View File

@@ -25,6 +25,7 @@ namespace Thelia\Controller\Front;
use Symfony\Component\Routing\Router;
use Thelia\Controller\BaseController;
use Thelia\Model\AddressQuery;
use Thelia\Model\ConfigQuery;
use Thelia\Model\ModuleQuery;
use Thelia\Tools\URL;
@@ -83,4 +84,16 @@ class BaseFrontController extends BaseController
$this->redirectToRoute("order.invoice");
}
}
/**
* @return ParserInterface instance parser
*/
protected function getParser()
{
$parser = $this->container->get("thelia.parser");
$parser->setTemplate(ConfigQuery::getActiveTemplate());
return $parser;
}
}

View File

@@ -23,15 +23,18 @@
namespace Thelia\Core\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Thelia\Core\HttpFoundation\Response;
use Symfony\Component\Routing\Router;
use Thelia\Core\HttpKernel\Exception\NotFountHttpException;
use Thelia\Core\Template\Exception\ResourceNotFoundException;
use Thelia\Core\Template\ParserInterface;
use Thelia\Exception\OrderException;
use Thelia\Model\ConfigQuery;
use Thelia\Tools\Redirect;
use Thelia\Tools\URL;
use Thelia\Core\Security\Exception\AuthenticationException;
@@ -74,9 +77,11 @@ class ViewListener implements EventSubscriberInterface
{
$parser = $this->container->get('thelia.parser');
$parser->setTemplate(ConfigQuery::getActiveTemplate());
$request = $this->container->get('request');
try {
$content = $parser->getContent();
$content = $parser->render($request->attributes->get('_view').".html");
if ($content instanceof Response) {
$response = $content;$event->setResponse($content);
@@ -94,7 +99,7 @@ class ViewListener implements EventSubscriberInterface
$event->setResponse($response);
} catch (ResourceNotFoundException $e) {
$event->setResponse(new Response($e->getMessage(), 404));
throw new NotFoundHttpException();
} catch (AuthenticationException $ex) {
// Redirect to the login template

View File

@@ -33,7 +33,7 @@ interface ParserInterface
/**
*
*/
public function getContent();
public function render($realTemplateName, array $parameters = array());
public function setContent($content);

View File

@@ -77,6 +77,8 @@ class SmartyParser extends Smarty implements ParserInterface
$this->setForceCompile(false);
}
//$this->enableSecurity();
// The default HTTP status
$this->status = 200;
@@ -103,7 +105,7 @@ class SmartyParser extends Smarty implements ParserInterface
{
$this->template = $template_path_from_template_base;
$this->setTemplateDir(THELIA_TEMPLATE_DIR.$this->template);
$this->addTemplateDir(THELIA_TEMPLATE_DIR.$this->template, 0);
$config_dir = THELIA_TEMPLATE_DIR.$this->template.'/configs';
@@ -124,6 +126,9 @@ class SmartyParser extends Smarty implements ParserInterface
*/
public function render($realTemplateName, array $parameters = array())
{
if(false === $this->templateExists($realTemplateName)) {
throw new ResourceNotFoundException();
}
// Assign the parserContext variables
foreach ($this->parserContext as $var => $value) {
$this->assign($var, $value);
@@ -131,23 +136,7 @@ class SmartyParser extends Smarty implements ParserInterface
$this->assign($parameters);
return $this->fetch($realTemplateName);
}
/**
*
* This method must return a Symfony\Component\HttpFoudation\Response instance or the content of the response
*
*/
public function getContent()
{
try {
$templateFile = $this->getTemplateFilePath();
} catch (\RuntimeException $e) {
return new Response($this->render(\Thelia\Model\ConfigQuery::getPageNotFoundView()), "404");
}
return $this->render($templateFile);
return $this->fetch(sprintf("file:%s", $realTemplateName));
}
/**
@@ -208,26 +197,4 @@ class SmartyParser extends Smarty implements ParserInterface
}
}
protected function getTemplateFilePath()
{
$file = $this->request->attributes->get('_view');
$fileName = THELIA_TEMPLATE_DIR . rtrim($this->template, "/") . "/" . $file;
$pathFileName = realpath(dirname(THELIA_TEMPLATE_DIR . rtrim($this->template, "/") . "/" . $file));
$templateDir = realpath(THELIA_TEMPLATE_DIR . rtrim($this->template, "/") . "/");
if (strpos($pathFileName, $templateDir) !== 0) {
throw new ResourceNotFoundException(sprintf("this view does not exists"));
}
if (!file_exists($fileName)) {
$fileName .= ".html";
if (!file_exists($fileName)) {
throw new ResourceNotFoundException(sprintf("file not found in %s template", $this->template));
}
}
return $fileName;
}
}