diff --git a/core/lib/Thelia/Command/BaseModuleGenerate.php b/core/lib/Thelia/Command/BaseModuleGenerate.php index 7d487d4cc..b3286c874 100755 --- a/core/lib/Thelia/Command/BaseModuleGenerate.php +++ b/core/lib/Thelia/Command/BaseModuleGenerate.php @@ -42,7 +42,8 @@ abstract class BaseModuleGenerate extends ContainerAwareCommand 'Config', 'Model', 'Loop', - 'AdminIncludes' + 'AdminIncludes', + 'templates', ); protected function verifyExistingModule() diff --git a/core/lib/Thelia/Config/Resources/config.xml b/core/lib/Thelia/Config/Resources/config.xml index 198f9f999..5a3548a9c 100755 --- a/core/lib/Thelia/Config/Resources/config.xml +++ b/core/lib/Thelia/Config/Resources/config.xml @@ -50,7 +50,6 @@ - false %kernel.environment% %kernel.debug% diff --git a/core/lib/Thelia/Controller/Admin/BaseAdminController.php b/core/lib/Thelia/Controller/Admin/BaseAdminController.php index 9eeedbbb7..a36318232 100755 --- a/core/lib/Thelia/Controller/Admin/BaseAdminController.php +++ b/core/lib/Thelia/Controller/Admin/BaseAdminController.php @@ -199,7 +199,7 @@ class BaseAdminController extends BaseController { $parser = $this->container->get("thelia.parser"); - // Define the template thant shoud be used + // Define the template that should be used $parser->setTemplate($template ?: TemplateHelper::getInstance()->getActiveAdminTemplate()->getPath()); return $parser; diff --git a/core/lib/Thelia/Controller/BaseController.php b/core/lib/Thelia/Controller/BaseController.php index d5868b8d2..450f98e74 100755 --- a/core/lib/Thelia/Controller/BaseController.php +++ b/core/lib/Thelia/Controller/BaseController.php @@ -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() diff --git a/core/lib/Thelia/Controller/Front/BaseFrontController.php b/core/lib/Thelia/Controller/Front/BaseFrontController.php index 369006aeb..60628bbc1 100755 --- a/core/lib/Thelia/Controller/Front/BaseFrontController.php +++ b/core/lib/Thelia/Controller/Front/BaseFrontController.php @@ -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; + } } diff --git a/core/lib/Thelia/Core/EventListener/ViewListener.php b/core/lib/Thelia/Core/EventListener/ViewListener.php index 667a3d42c..722dcbd67 100755 --- a/core/lib/Thelia/Core/EventListener/ViewListener.php +++ b/core/lib/Thelia/Core/EventListener/ViewListener.php @@ -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 diff --git a/core/lib/Thelia/Core/Template/ParserInterface.php b/core/lib/Thelia/Core/Template/ParserInterface.php index bbf926812..a0886a203 100755 --- a/core/lib/Thelia/Core/Template/ParserInterface.php +++ b/core/lib/Thelia/Core/Template/ParserInterface.php @@ -33,7 +33,7 @@ interface ParserInterface /** * */ - public function getContent(); + public function render($realTemplateName, array $parameters = array()); public function setContent($content); diff --git a/core/lib/Thelia/Core/Template/Smarty/SmartyParser.php b/core/lib/Thelia/Core/Template/Smarty/SmartyParser.php index 6b8e77d9e..586fc7a05 100755 --- a/core/lib/Thelia/Core/Template/Smarty/SmartyParser.php +++ b/core/lib/Thelia/Core/Template/Smarty/SmartyParser.php @@ -38,13 +38,12 @@ class SmartyParser extends Smarty implements ParserInterface * @param Request $request * @param EventDispatcherInterface $dispatcher * @param ParserContext $parserContext - * @param bool $template * @param string $env * @param bool $debug */ public function __construct( Request $request, EventDispatcherInterface $dispatcher, ParserContext $parserContext, - $template = false, $env = "prod", $debug = false) + $env = "prod", $debug = false) { parent::__construct(); @@ -63,7 +62,6 @@ class SmartyParser extends Smarty implements ParserInterface $this->setCompileDir($compile_dir); $this->setCacheDir($cache_dir); - $this->setTemplate($template ?: TemplateHelper::getInstance()->getActiveFrontTemplate()->getPath()); $this->debugging = $debug; @@ -79,6 +77,8 @@ class SmartyParser extends Smarty implements ParserInterface $this->setForceCompile(false); } + //$this->enableSecurity(); + // The default HTTP status $this->status = 200; @@ -105,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'; @@ -126,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); @@ -133,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)); } /** @@ -210,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; - } } diff --git a/core/lib/Thelia/Core/Thelia.php b/core/lib/Thelia/Core/Thelia.php index 50688b53b..c8cc02820 100755 --- a/core/lib/Thelia/Core/Thelia.php +++ b/core/lib/Thelia/Core/Thelia.php @@ -124,8 +124,10 @@ class Thelia extends Kernel if (defined("THELIA_INSTALL_MODE") === false) { $modules = \Thelia\Model\ModuleQuery::getActivated(); - $translator = $container->getDefinition('thelia.translator'); - $dirs = array(); + + $translationDirs = array(); + $templateDirs = array(); + $parser = $container->getDefinition('thelia.parser'); foreach ($modules as $module) { try { @@ -139,50 +141,65 @@ class Thelia extends Kernel $defintion ); - $loader = new XmlFileLoader($container, new FileLocator(THELIA_MODULE_DIR . $module->getConfigPath())); + + $code = ucfirst($module->getCode()); + + $loader = new XmlFileLoader($container, new FileLocator(THELIA_MODULE_DIR . "/" . $code . "/Config")); $loader->load("config.xml"); - if (is_dir($dir = THELIA_MODULE_DIR . $module->getI18nPath())) { - $dirs[] = $dir; + if (is_dir($dir = THELIA_MODULE_DIR . "/" . $code . "/I18n")) { + $translationDirs[] = $dir; + } + + if (is_dir($dir = THELIA_MODULE_DIR . "/" . $code . "/templates")) { + //$templateDirs[$code] = $dir; + $parser->addMethodCall('addTemplateDir', array($dir, $code)); } } catch (\InvalidArgumentException $e) { - // FIXME: process module configuration exception + // TODO: process module configuration exception } } // Load translation from templates //core translation - $dirs[] = THELIA_ROOT . "core/lib/Thelia/Config/I18n"; + $translationDirs[] = THELIA_ROOT . "core/lib/Thelia/Config/I18n"; // admin template if (is_dir($dir = THELIA_TEMPLATE_DIR . TemplateHelper::getInstance()->getActiveAdminTemplate()->getI18nPath())) { - $dirs[] = $dir; + $translationDirs[] = $dir; } // front template if (is_dir($dir = THELIA_TEMPLATE_DIR . TemplateHelper::getInstance()->getActiveFrontTemplate()->getI18nPath())) { - $dirs[] = $dir; + $translationDirs[] = $dir; } // PDF template if (is_dir($dir = THELIA_TEMPLATE_DIR . TemplateHelper::getInstance()->getActivePdfTemplate()->getI18nPath())) { - $dirs[] = $dir; + $translationDirs[] = $dir; } - if ($dirs) { - $finder = Finder::create() - ->files() - ->depth(0) - ->in($dirs); - - foreach ($finder as $file) { - list($locale, $format) = explode('.', $file->getBaseName(), 2); - $translator->addMethodCall('addResource', array($format, (string) $file, $locale)); - } + if ($translationDirs) { + $this->loadTranslation($container, $translationDirs); } } } + private function loadTranslation(ContainerBuilder $container, array $dirs) + { + $translator = $container->getDefinition('thelia.translator'); + + $finder = Finder::create() + ->files() + ->depth(0) + ->in($dirs); + + foreach ($finder as $file) { + list($locale, $format) = explode('.', $file->getBaseName(), 2); + $translator->addMethodCall('addResource', array($format, (string) $file, $locale)); + } + } + /** * * initialize session in Request object