start creating new Parser

create new listener for checking view parameter
throw 404 status page if resource not found
This commit is contained in:
Manuel Raynaud
2013-02-13 19:01:51 +01:00
parent f8273b0e53
commit 106818510f
11 changed files with 153 additions and 54 deletions

View File

@@ -5,9 +5,10 @@
* @file
* Functions needed for Thelia bootstrap
*/
define('THELIA_ROOT', __DIR__ .'/../');
define('THELIA_ROOT', realpath(__DIR__ .'/../') . "/");
define('THELIA_CONF_DIR', THELIA_ROOT . '/local/config');
define('THELIA_PLUGIN_DIR', THELIA_ROOT . '/local/plugins');
define('THELIA_TEMPLATE_DIR', THELIA_ROOT . 'templates/');
$loader = require __DIR__ . "/vendor/autoload.php";

View File

@@ -61,4 +61,3 @@ class Cart extends BaseAction
}
}

View File

@@ -44,12 +44,13 @@ class DefaultController implements NullControllerInterface
*/
public function noAction(Request $request)
{
if ($request->query->has('view') === false) {
$fond = "index";
if (! $view = $request->query->get('view')) {
$view = "index";
if ($request->request->has('view')) {
$fond = $request->request->get('view');
$view = $request->request->get('view');
}
$request->query->set('view', $fond);
}
$request->attributes->set('_view', $view);
}
}

View File

@@ -91,7 +91,7 @@ class TheliaBundle extends Bundle
*/
$container->register('thelia.listener.view','Thelia\Core\EventListener\ViewListener')
->addArgument(new Reference('parser'))
->addArgument(new Reference('service_container'))
;

View File

@@ -40,4 +40,11 @@ final class TheliaEvents
* Send if no action are already present in Thelia action process ( see Thelia\Routing\Matcher\ActionMatcher)
*/
const ACTION = "thelia.action";
/**
* INCLUDE event
*
* Send before starting thelia inclusion
*/
const INCLUSION = "thelia.include";
}

View File

@@ -25,7 +25,10 @@ namespace Thelia\Core\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Thelia\Core\Template\ParserInterface;
/**
@@ -39,15 +42,19 @@ use Thelia\Core\Template\ParserInterface;
class ViewListener implements EventSubscriberInterface
{
private $parser;
/**
*
* @var Symfony\Component\DependencyInjection\ContainerInterface
*/
private $container;
/**
*
* @param \Thelia\Core\Template\ParserInterface $parser
*/
public function __construct(ParserInterface $parser)
public function __construct(ContainerInterface $container)
{
$this->parser = $parser;
$this->container = $container;
}
/**
@@ -60,13 +67,41 @@ class ViewListener implements EventSubscriberInterface
*/
public function onKernelView(GetResponseForControllerResultEvent $event)
{
$content = $this->parser->getContent();
$parser = $this->container->get('parser');
try {
$content = $parser->getContent();
if ($content instanceof Response) {
$event->setResponse($content);
} else {
$event->setResponse(new Response($content, $this->parser->getStatus() ?: 200));
$event->setResponse(new Response($content, $parser->getStatus() ?: 200));
}
} catch(ResourceNotFoundException $e) {
$event->setResponse(new Response($e->getMessage(), 404));
}
}
public function beforeKernelView(GetResponseForControllerResultEvent $event)
{
$request = $this->container->get('request');
if (!$view = $request->attributes->get('_view')) {
$request->attributes->set('_view', $this->findView($request));
}
}
public function findView(Request $request)
{
if (! $view = $request->query->get('view')) {
$view = "index";
if ($request->request->has('view')) {
$view = $request->request->get('view');
}
}
return $view;
}
@@ -79,7 +114,10 @@ class ViewListener implements EventSubscriberInterface
public static function getSubscribedEvents()
{
return array(
KernelEvents::VIEW => array('onKernelView'),
KernelEvents::VIEW =>array(
array('onKernelView', 0),
array('beforeKernelView', 5)
)
);
}
}

View File

@@ -25,6 +25,8 @@ namespace Thelia\Core\Template;
use Symfony\Component\HttpFoundation\Response;
use Thelia\Core\Template\ParserInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
/**
*
@@ -45,6 +47,7 @@ class Parser implements ParserInterface
const ALLOW_DEBUG = true;
const USE_CACHE = true;
/**
*
* @var Symfony\Component\DependencyInjection\ContainerInterface
@@ -54,6 +57,8 @@ class Parser implements ParserInterface
protected $content;
protected $status = 200;
protected $template = "default";
/**
*
* @param type $container
@@ -65,6 +70,24 @@ class Parser implements ParserInterface
$this->container = $container;
}
/**
*
* @return Symfony\Component\HttpFoundation\Request
*/
public function getRequest()
{
return $this->container->get('request');
}
/**
*
* @return Symfony\Component\EventDispatcher\EventDispatcher
*/
public function getDispatcher()
{
return $this->container->get('dispatcher');
}
/**
*
* This method must return a Symfony\Component\HttpFoudation\Response instance or the content of the response
@@ -75,7 +98,6 @@ class Parser implements ParserInterface
$this->loadParser();
echo \Thelia\Model\ConfigQuery::read("alfred", "dupont");
\Thelia\Log\Tlog::getInstance()->debug("tutu");
return $this->content;
}
@@ -111,8 +133,25 @@ class Parser implements ParserInterface
$this->status = $status;
}
/**
* Main parser function, load the parser
*/
public function loadParser()
{
$content = $this->openFile($this->getRequest());
}
public function openFile(Request $request)
{
$file = $request->attributes->get('_view');
$fileName = THELIA_TEMPLATE_DIR . rtrim($this->template, "/") . "/" . $file . ".html";
if (file_exists($fileName)) {
$content = file_get_contents($fileName);
} else {
throw new ResourceNotFoundException(sprintf("%s file not found in %s template", $file, $this->template));
}
}
}

View File

@@ -0,0 +1,14 @@
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div>TODO write content</div>
</body>
</html>