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

@@ -26,34 +26,34 @@ namespace Thelia\Action;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
*
*
*
*
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
abstract class BaseAction
abstract class BaseAction
{
/**
*
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
protected $dispatcher;
/**
*
*
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher
*/
public function __construct(EventDispatcherInterface $dispatcher)
public function __construct(EventDispatcherInterface $dispatcher)
{
$this->dispatcher = $dispatcher;
}
/**
*
*
* @return \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
public function getDispatcher()
{
return $this->dispatcher;
}
}
}

View File

@@ -29,36 +29,35 @@ use Thelia\Action\BaseAction;
class Cart extends BaseAction
{
/**
*
*
* add an article to cart
*
*
* @param \Symfony\Component\HttpFoundation\Request $request
*/
public function addCart(Request $request)
{
}
/**
*
*
* Delete specify article present into cart
*
*
* @param \Symfony\Component\HttpFoundation\Request $request
*/
public function deleteArticle(Request $request)
{
}
/**
*
*
* Modify article's quantity
*
*
* @param \Symfony\Component\HttpFoundation\Request $request
*/
public function modifyArticle(Request $request)
{
}
}

View File

@@ -27,19 +27,19 @@ use Symfony\Component\HttpFoundation\Request;
class Customer
{
public function create(Request $request)
{
}
public function modify(Request $request)
{
}
public function modifyPassword(Request $request)
{
}
}

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));
if ($content instanceof Response) {
$event->setResponse($content);
} else {
$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;
/**
*
@@ -44,15 +46,18 @@ class Parser implements ParserInterface
const SHOW_TIME = true;
const ALLOW_DEBUG = true;
const USE_CACHE = true;
/**
*
* @var Symfony\Component\DependencyInjection\ContainerInterface
*/
protected $container;
protected $content;
protected $status = 200;
protected $template = "default";
/**
*
@@ -64,6 +69,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');
}
/**
*
@@ -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

@@ -34,7 +34,7 @@ use Thelia\Action\Cart;
/**
* Matcher using action param in get or post method to perform actions. For exemple index.php?action=addCart will find the good controller that can perform this action.
*
*
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
@@ -42,25 +42,25 @@ class ActionMatcher implements RequestMatcherInterface
{
/**
*
* @var Symfony\Component\EventDispatcher\EventDispatcherInterface
* @var Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
protected $dispatcher;
public function setDispatcher(EventDispatcherInterface $dispatcher)
{
$this->dispatcher = $dispatcher;
}
public function matchRequest(Request $request)
{
if (false !== $action = $request->get("action")) {
//search corresponding action
return $this->dispatchAction($request, $action);
}
throw new ResourceNotFoundException("No action parameter found");
}
protected function dispatchAction(Request $request, $action)
{
$controller = null;
@@ -71,7 +71,7 @@ class ActionMatcher implements RequestMatcherInterface
"addCart"
);
break;
default :
default :
$event = new ActionEvent($request, $action);
$this->dispatcher->dispatch(TheliaEvents::ACTION, $event);
if ($event->hasController()) {
@@ -79,14 +79,14 @@ class ActionMatcher implements RequestMatcherInterface
}
break;
}
if ($controller) {
return array(
'_controller' => $controller
);
}
throw new ResourceNotFoundException("No action parameter found");
}
}

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>