tweak httpFoundation cache part for compatibility with

Thelia\HttpFoundation part
This commit is contained in:
Manuel Raynaud
2013-10-06 19:30:58 +02:00
parent 6a80dde83b
commit 64c5991838
6 changed files with 126 additions and 23 deletions

View File

@@ -63,8 +63,6 @@ class ModuleActivateCommand extends BaseModuleGenerate
} }
try { try {
new \TheliaDebugBar\TheliaDebugBar();
$moduleReflection = new \ReflectionClass($module->getFullNamespace()); $moduleReflection = new \ReflectionClass($module->getFullNamespace());
$moduleInstance = $moduleReflection->newInstance(); $moduleInstance = $moduleReflection->newInstance();

View File

@@ -71,7 +71,9 @@
<argument type="service" id="event_dispatcher"/> <argument type="service" id="event_dispatcher"/>
</service> </service>
<service id="thelia.response.listener" class="Thelia\Core\EventListener\ResponseListener">
<tag name="kernel.event_subscriber"/>
</service>
</services> </services>
</config> </config>

View File

@@ -0,0 +1,76 @@
<?php
/*************************************************************************************/
/* */
/* Thelia */
/* */
/* Copyright (c) OpenStudio */
/* email : info@thelia.net */
/* web : http://www.thelia.net */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 3 of the License */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
namespace Thelia\Core\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* Class ResponseListener
* @package Thelia\Core\EventListener
* @author manuel raynaud <mraynaud@openstudio.fr>
*/
class ResponseListener implements EventSubscriberInterface
{
public function onResponse(FilterResponseEvent $event)
{
$request = $event->getRequest();
if($request->headers->has('Surrogate-Capability'))
{
$response = $event->getResponse();
$response->headers->set('Surrogate-Control', 'content="ESI/1.0"');
$event->setResponse($response);
}
}
/**
* Returns an array of event names this subscriber wants to listen to.
*
* The array keys are event names and the value can be:
*
* * The method name to call (priority defaults to 0)
* * An array composed of the method name to call and the priority
* * An array of arrays composed of the method names to call and respective
* priorities, or 0 if unset
*
* For instance:
*
* * array('eventName' => 'methodName')
* * array('eventName' => array('methodName', $priority))
* * array('eventName' => array(array('methodName1', $priority), array('methodName2'))
*
* @return array The event names to listen to
*
* @api
*/
public static function getSubscribedEvents()
{
return array(
KernelEvents::RESPONSE => array('onResponse', 0)
);
}
}

View File

@@ -23,17 +23,20 @@
namespace Thelia\Core\HttpKernel\HttpCache; namespace Thelia\Core\HttpKernel\HttpCache;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpCache\Esi; use Symfony\Component\HttpKernel\HttpCache\Esi;
use Symfony\Component\HttpKernel\HttpCache\HttpCache as BaseHttpCache; use Symfony\Component\HttpKernel\HttpCache\HttpCache as BaseHttpCache;
use Symfony\Component\HttpKernel\HttpCache\Store; use Symfony\Component\HttpKernel\HttpCache\Store;
use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\HttpKernel\HttpKernelInterface;
use Thelia\Core\HttpFoundation\Request as TheliaRequest;
/** /**
* Class HttpCache * Class HttpCache
* @package Thelia\Core\HttpKernel\HttpCache * @package Thelia\Core\HttpKernel\HttpCache
* @author manuel raynaud <mraynaud@openstudio.fr> * @author manuel raynaud <mraynaud@openstudio.fr>
*/ */
class HttpCache extends BaseHttpCache class HttpCache extends BaseHttpCache implements HttpKernelInterface
{ {
public function __construct(HttpKernelInterface $kernel, $options = array()) public function __construct(HttpKernelInterface $kernel, $options = array())
@@ -49,4 +52,20 @@ class HttpCache extends BaseHttpCache
); );
} }
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
{
if (!($request instanceof \Thelia\Core\HttpFoundation\Request)) {
$request = TheliaRequest::create(
$request->getUri(),
$request->getMethod(),
$request->getMethod() == 'GET' ? $request->query->all() : $request->request->all(),
$request->cookies->all(),
$request->files->all(),
$request->server->all(),
$request->getContent()
);
}
return parent::handle($request, $type, $catch);
}
} }

View File

@@ -43,6 +43,7 @@ use Thelia\Model;
class TheliaHttpKernel extends HttpKernel class TheliaHttpKernel extends HttpKernel
{ {
protected static $session;
protected $container; protected $container;
@@ -79,8 +80,11 @@ class TheliaHttpKernel extends HttpKernel
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true) public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
{ {
//$request->headers->set('X-Php-Ob-Level', ob_get_level()); //$request->headers->set('X-Php-Ob-Level', ob_get_level());
$request = $this->initSession($request); if ($type == HttpKernelInterface::MASTER_REQUEST) {
$this->initParam($request); $request = $this->initSession($request);
$this->initParam($request);
}
$this->container->enterScope('request'); $this->container->enterScope('request');
$this->container->set('request', $request, 'request'); $this->container->set('request', $request, 'request');
@@ -211,26 +215,29 @@ class TheliaHttpKernel extends HttpKernel
protected function initSession(Request $request) protected function initSession(Request $request)
{ {
if(null === self::$session) {
$storage = new Session\Storage\NativeSessionStorage();
$storage = new Session\Storage\NativeSessionStorage(); if (Model\ConfigQuery::read("session_config.default")) {
$storage->setSaveHandler(new Session\Storage\Handler\NativeFileSessionHandler(Model\ConfigQuery::read("session_config.save_path", THELIA_ROOT . '/local/session/')));
} else {
$handlerString = Model\ConfigQuery::read("session_config.handlers", 'Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeFileSessionHandler');
if (Model\ConfigQuery::read("session_config.default")) { $handler = new $handlerString;
$storage->setSaveHandler(new Session\Storage\Handler\NativeFileSessionHandler(Model\ConfigQuery::read("session_config.save_path", THELIA_LOCAL_DIR . 'session/')));
$storage->setSaveHandler($handler);
}
if (Model\ConfigQuery::read("session_config.config", null)) {
$storage->setOptions(json_decode(Model\ConfigQuery::read("session_config.config")));
}
self::$session = $session = new \Thelia\Core\HttpFoundation\Session\Session($storage);
} else { } else {
$handlerString = Model\ConfigQuery::read("session_config.handlers", 'Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeFileSessionHandler'); $session = self::$session;
$handler = new $handlerString;
$storage->setSaveHandler($handler);
} }
if (Model\ConfigQuery::read("session_config.config", null)) {
$storage->setOptions(json_decode(Model\ConfigQuery::read("session_config.config")));
}
$session = new \Thelia\Core\HttpFoundation\Session\Session($storage);
$session->start(); $session->start();
$request->setSession($session); $request->setSession($session);
return $request; return $request;

View File

@@ -44,9 +44,10 @@ if ( false === in_array($request->getClientIp(), $trustedIp)) {
$response = Response::create('Forbidden', 403)->send(); $response = Response::create('Forbidden', 403)->send();
$thelia->terminate($request, $response); $thelia->terminate($request, $response);
} else { } else {
//$thelia = new HttpCache($thelia); $thelia = new HttpCache($thelia);
$response = $thelia->handle($request)->prepare($request)->send(); $response = $thelia->handle($request)->send();
$thelia->terminate($request, $response); //$thelia->terminate($request, $response);
} }