début de la mise en place du parser

This commit is contained in:
Manuel Raynaud
2012-10-19 18:13:28 +02:00
parent e15a86b84b
commit 67fa4f6706
7 changed files with 95 additions and 20 deletions

View File

@@ -6,6 +6,8 @@ use Thelia\Controller\NullControllerInterface;
use Symfony\Component\HttpFoundation\Request;
/**
*
* Must be the last controller call. It fixes default values
*
* @author Manuel Raynaud <mraynadu@openstudio.fr>
*/

View File

@@ -0,0 +1,30 @@
<?php
namespace Thelia\Core\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\DependencyInjection\ContainerInterface;
class RequestListener implements EventSubscriberInterface {
protected $container;
public function __construct(ContainerInterface $container) {
$this->container = $container;
}
public function onKernelRequest(GetResponseEvent $event){
}
public static function getSubscribedEvents(){
return array(
KernelEvents::REQUEST => array('onKernelRequest', 30)
);
}
}
?>

View File

@@ -1,6 +1,6 @@
<?php
namespace Thelia\Core\EventSubscriber;
namespace Thelia\Core\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
@@ -12,12 +12,14 @@ use Thelia\Core\Template\ParserInterface;
/**
*
* ViewSubscriber Main class subscribing to view http response.
*
* @TODO Look if it's possible to block this definition in dependency-injection
*
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
class ViewSubscriber implements EventSubscriberInterface{
class ViewListener implements EventSubscriberInterface{
private $parser;
@@ -31,18 +33,21 @@ class ViewSubscriber implements EventSubscriberInterface{
/**
*
* Launch the parser defined on the constructor and get the result.
*
* The result is transform id needed into a Response object
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent $event
*/
public function onKernelView(GetResponseForControllerResultEvent $event){
$content = $this->parser->getContent();
if($content instanceof Response){
$event->setResponse($content);
}
else{
$event->setResponse(new Response($content, $this->parser->getStatus()));
$event->setResponse(new Response($content, $this->parser->getStatus() ?: 200));
}
}

View File

@@ -6,6 +6,7 @@ use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Thelia\Core\Template\ParserInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\EventDispatcher\EventDispatcher;
/**
*
@@ -20,11 +21,17 @@ use Symfony\Component\DependencyInjection\ContainerBuilder;
class Parser implements ParserInterface {
const PREFIXE = 'prx';
const SHOW_TIME = true;
const ALLOW_DEBUG = true;
const USE_CACHE = true;
protected $container;
protected $content;
protected $status;
protected $status = 200;
public function __construct(ContainerBuilder $container) {
$this->container = $container;
}
@@ -36,18 +43,34 @@ class Parser implements ParserInterface {
*/
public function getContent() {
$this->loadParser();
$this->content = "toto";
return $this->content;
}
/**
*
* set $content with the body of the response or the Response object directly
*
* @param string|Symfony\Component\HttpFoundation\Response $content
*/
public function setContent($content) {
$this->content = $content;
}
/**
*
* @return type the status of the response
*/
public function getStatus() {
return 200;
return $this->status;
}
/**
*
* status HTTP of the response
*
* @param int $status
*/
public function setStatus($status) {
$this->status = $status;
}

View File

@@ -47,24 +47,43 @@ class TheliaBundle extends Bundle {
$container->register('resolver', 'Symfony\Component\HttpKernel\Controller\ControllerResolver');
$container->register('parser','Thelia\Core\Template\Parser')
->addArgument(new Reference('service_container'));
->addArgument(new Reference('service_container'))
;
/**
* RouterListener implements EventSubscriberInterface and listen for kernel.request event
*/
$container->register('listener.router', 'Symfony\Component\HttpKernel\EventListener\RouterListener')
->setArguments(array(new Reference('matcher')));
->setArguments(array(new Reference('matcher')))
;
/**
* @TODO add an other listener on kernel.request for checking some params Like check if User is log in, set the language and other.
*
* $container->register()
*
*
* $container->register('listener.request', 'Thelia\Core\EventListener\RequestListener')
* ->addArgument(new Reference('');
* ;
*/
$container->register('listener.view','Thelia\Core\EventSubscriber\ViewSubscriber')
->addArgument(new Reference('parser'));
$container->register('thelia.listener.view','Thelia\Core\EventListener\ViewListener')
->addArgument(new Reference('parser'))
;
$container->register('http_kernel','Symfony\Component\HttpKernel\HttpKernel')
->addArgument(new Reference('dispatcher'))
->addArgument(new Reference('resolver'));
->addArgument(new Reference('resolver'))
;
$container->register('dispatcher','Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher')
->addArgument(new Reference('service_container'))
->addMethodCall('addSubscriber', array(new Reference('listener.router')))
->addMethodCall('addSubscriber', array(new Reference('listener.view')));
->addMethodCall('addSubscriber', array(new Reference('thelia.listener.view')))
;
// DEFINE DEFAULT PARAMETER LIKE
/**

View File

@@ -33,4 +33,4 @@ class DefaultMatcher implements RequestMatcherInterface{
}
?>
?>

View File

@@ -150,10 +150,6 @@ class TheliaMatcherCollection implements RequestMatcherInterface, RequestContext
*/
public function getContext(){
return $this->context;
}
}
}
?>
?>