Merge branch 'modules'

Conflicts:
	core/lib/Thelia/Controller/Admin/BaseAdminController.php
	core/lib/Thelia/Core/Template/Smarty/SmartyParser.php
	core/lib/Thelia/Core/Thelia.php
This commit is contained in:
Manuel Raynaud
2013-11-06 14:37:31 +01:00
9 changed files with 74 additions and 69 deletions

View File

@@ -42,7 +42,8 @@ abstract class BaseModuleGenerate extends ContainerAwareCommand
'Config',
'Model',
'Loop',
'AdminIncludes'
'AdminIncludes',
'templates',
);
protected function verifyExistingModule()

View File

@@ -50,7 +50,6 @@
<argument type="service" id="request" />
<argument type="service" id="event_dispatcher"/>
<argument type="service" id="thelia.parser.context"/>
<argument >false</argument> <!-- Template name, or false -->
<argument >%kernel.environment%</argument>
<argument >%kernel.debug%</argument>
</service>

View File

@@ -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;

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

@@ -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;
}
}

View File

@@ -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