add custom httpKernel for Thelia Core.

This commit is contained in:
Manuel Raynaud
2012-12-20 10:08:13 +01:00
parent 65291cf471
commit ab179a637c
4 changed files with 131 additions and 22 deletions

View File

@@ -4,7 +4,7 @@ namespace Thelia\Core\Template;
use Symfony\Component\HttpFoundation\Response;
use Thelia\Core\Template\ParserInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
*
@@ -25,12 +25,22 @@ class Parser implements ParserInterface
const ALLOW_DEBUG = true;
const USE_CACHE = true;
/**
*
* @var Symfony\Component\DependencyInjection\ContainerInterface
*/
protected $container;
protected $content;
protected $status = 200;
public function __construct(ContainerBuilder $container)
/**
*
* @param type $container
*
* public function __construct(ContainerBuilder $container)
*/
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
@@ -43,7 +53,10 @@ class Parser implements ParserInterface
public function getContent()
{
$this->loadParser();
$this->content = "toto";
$request = $this->container->get('request');
$this->content = $request->get("test");
return $this->content;
}

View File

@@ -25,11 +25,46 @@ class Thelia extends Kernel
* The cached version of the service container is used when fresh, otherwise the
* container is built.
*/
protected function initializeContainer()
{
$this->container = $this->buildContainer();
$this->container->set('kernel', $this);
// protected function initializeContainer()
// {
// $this->container = $this->buildContainer();
// $this->container->set('kernel', $this);
//
// }
/**
* Gets the cache directory.
*
* @return string The cache directory
*
* @api
*/
public function getCacheDir()
{
if(defined('THELIA_ROOT'))
{
return THELIA_ROOT.'cache/'.$this->environment;
} else {
return parent::getCacheDir();
}
}
/**
* Gets the log directory.
*
* @return string The log directory
*
* @api
*/
public function getLogDir()
{
if(defined('THELIA_ROOT'))
{
return THELIA_ROOT.'log/';
} else {
return parent::getLogDir();
}
}
/**
@@ -37,16 +72,16 @@ class Thelia extends Kernel
*
* @return ContainerBuilder The compiled service container
*/
protected function buildContainer()
{
$container = $this->getContainerBuilder();
foreach ($this->bundles as $bundle) {
$bundle->build($container);
}
return $container;
}
// protected function buildContainer()
// {
// $container = $this->getContainerBuilder();
//
// foreach ($this->bundles as $bundle) {
// $bundle->build($container);
// }
//
// return $container;
// }
/**
* return available bundle

View File

@@ -71,17 +71,20 @@ class TheliaBundle extends Bundle
->addArgument(new Reference('parser'))
;
$container->register('http_kernel','Symfony\Component\HttpKernel\HttpKernel')
->addArgument(new Reference('dispatcher'))
->addArgument(new Reference('resolver'))
;
$container->register('dispatcher','Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher')
$container->register('dispatcher','Symfony\Component\EventDispatcher\EventDispatcher')
->addArgument(new Reference('service_container'))
->addMethodCall('addSubscriber', array(new Reference('listener.router')))
->addMethodCall('addSubscriber', array(new Reference('thelia.listener.view')))
;
$container->register('http_kernel','Thelia\Core\TheliaHttpKernel')
->addArgument(new Reference('dispatcher'))
->addArgument(new Reference('service_container'))
->addArgument(new Reference('resolver'))
;
// DEFINE DEFAULT PARAMETER LIKE
/**

View File

@@ -0,0 +1,58 @@
<?php
namespace Thelia\Core;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class TheliaHttpKernel extends HttpKernel
{
public function __construct(EventDispatcherInterface $dispatcher, ContainerInterface $container, ControllerResolverInterface $controllerResolver)
{
parent::__construct($dispatcher, $controllerResolver);
$this->container = $container;
}
/**
* Handles a Request to convert it to a Response.
*
* When $catch is true, the implementation must catch all exceptions
* and do its best to convert them to a Response instance.
*
* @param Request $request A Request instance
* @param integer $type The type of the request
* (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)
* @param Boolean $catch Whether to catch exceptions or not
*
* @return Response A Response instance
*
* @throws \Exception When an Exception occurs during processing
*
* @api
*/
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
{
//$request->headers->set('X-Php-Ob-Level', ob_get_level());
$this->container->enterScope('request');
$this->container->set('request', $request, 'request');
try {
$response = parent::handle($request, $type, $catch);
} catch (\Exception $e) {
$this->container->leaveScope('request');
throw $e;
}
$this->container->leaveScope('request');
return $response;
}
}