From 9390a8b3fcd86901cb509c1678337c90715d37f2 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Sat, 5 Oct 2013 11:52:13 +0200 Subject: [PATCH 01/15] start using symfony 2 reverse proxy --- .../Core/HttpKernel/HttpCache/HttpCache.php | 52 +++++++++++++++++++ core/lib/Thelia/Core/Thelia.php | 1 + web/index.php | 3 +- 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 core/lib/Thelia/Core/HttpKernel/HttpCache/HttpCache.php diff --git a/core/lib/Thelia/Core/HttpKernel/HttpCache/HttpCache.php b/core/lib/Thelia/Core/HttpKernel/HttpCache/HttpCache.php new file mode 100644 index 000000000..01ad22733 --- /dev/null +++ b/core/lib/Thelia/Core/HttpKernel/HttpCache/HttpCache.php @@ -0,0 +1,52 @@ +. */ +/* */ +/*************************************************************************************/ + +namespace Thelia\Core\HttpKernel\HttpCache; + +use Symfony\Component\HttpKernel\HttpCache\Esi; +use Symfony\Component\HttpKernel\HttpCache\HttpCache as BaseHttpCache; +use Symfony\Component\HttpKernel\HttpCache\Store; +use Symfony\Component\HttpKernel\HttpKernelInterface; + +/** + * Class HttpCache + * @package Thelia\Core\HttpKernel\HttpCache + * @author manuel raynaud + */ +class HttpCache extends BaseHttpCache +{ + + public function __construct(HttpKernelInterface $kernel, $options = array()) + { + parent::__construct( + $kernel, + new Store($kernel->getCacheDir().'/http_cache'), + new Esi(), + array_merge( + array('debug' => $kernel->isDebug()), + $options + ) + ); + } + +} \ No newline at end of file diff --git a/core/lib/Thelia/Core/Thelia.php b/core/lib/Thelia/Core/Thelia.php index 199a0bb0b..cb20bca3f 100755 --- a/core/lib/Thelia/Core/Thelia.php +++ b/core/lib/Thelia/Core/Thelia.php @@ -64,6 +64,7 @@ class Thelia extends Kernel if ($this->debug) { ini_set('display_errors', 1); } + ini_set('display_errors', 1); $this->initPropel(); } diff --git a/web/index.php b/web/index.php index 4d50cc633..378bf1430 100755 --- a/web/index.php +++ b/web/index.php @@ -1,4 +1,5 @@ handle($request)->prepare($request)->send(); $thelia->terminate($request, $response); From 64a8aeeb3303df5b9e0ca1a3586fbbfee4e46a59 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Sun, 6 Oct 2013 10:49:57 +0200 Subject: [PATCH 02/15] add httpcache into index_dev.php file --- local/cache/.gitkeep | 0 web/index_dev.php | 2 ++ 2 files changed, 2 insertions(+) delete mode 100755 local/cache/.gitkeep diff --git a/local/cache/.gitkeep b/local/cache/.gitkeep deleted file mode 100755 index e69de29bb..000000000 diff --git a/web/index_dev.php b/web/index_dev.php index f653270d6..a89edaf7a 100755 --- a/web/index_dev.php +++ b/web/index_dev.php @@ -25,6 +25,7 @@ use Symfony\Component\HttpFoundation\Response; use Thelia\Core\Thelia; use Thelia\Core\HttpFoundation\Request; +use Thelia\Core\HttpKernel\HttpCache\HttpCache; //use Symfony\Component\DependencyInjection; @@ -45,6 +46,7 @@ if ( false === in_array($request->getClientIp(), $trustedIp)) { $response = Response::create('Forbidden', 403)->send(); $thelia->terminate($request, $response); } else { + //$thelia = new HttpCache($thelia); $response = $thelia->handle($request)->prepare($request)->send(); $thelia->terminate($request, $response); } From c1ab54bd4e57639f802443261507736013e45f18 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Sun, 6 Oct 2013 19:30:58 +0200 Subject: [PATCH 03/15] tweak httpFoundation cache part for compatibility with Thelia\HttpFoundation part --- .../Thelia/Command/ModuleActivateCommand.php | 2 - core/lib/Thelia/Config/Resources/config.xml | 4 +- .../Core/EventListener/ResponseListener.php | 76 +++++++++++++++++++ .../Core/HttpKernel/HttpCache/HttpCache.php | 21 ++++- core/lib/Thelia/Core/TheliaHttpKernel.php | 42 +++++----- web/index_dev.php | 7 +- 6 files changed, 128 insertions(+), 24 deletions(-) create mode 100644 core/lib/Thelia/Core/EventListener/ResponseListener.php diff --git a/core/lib/Thelia/Command/ModuleActivateCommand.php b/core/lib/Thelia/Command/ModuleActivateCommand.php index d5ca8156d..cea8f8251 100755 --- a/core/lib/Thelia/Command/ModuleActivateCommand.php +++ b/core/lib/Thelia/Command/ModuleActivateCommand.php @@ -64,8 +64,6 @@ class ModuleActivateCommand extends BaseModuleGenerate } try { - new \TheliaDebugBar\TheliaDebugBar(); - $moduleReflection = new \ReflectionClass($module->getFullNamespace()); $moduleInstance = $moduleReflection->newInstance(); diff --git a/core/lib/Thelia/Config/Resources/config.xml b/core/lib/Thelia/Config/Resources/config.xml index d4d63ac77..305f20c70 100755 --- a/core/lib/Thelia/Config/Resources/config.xml +++ b/core/lib/Thelia/Config/Resources/config.xml @@ -319,7 +319,9 @@ - + + + diff --git a/core/lib/Thelia/Core/EventListener/ResponseListener.php b/core/lib/Thelia/Core/EventListener/ResponseListener.php new file mode 100644 index 000000000..eb03bde81 --- /dev/null +++ b/core/lib/Thelia/Core/EventListener/ResponseListener.php @@ -0,0 +1,76 @@ +. */ +/* */ +/*************************************************************************************/ + +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 + */ +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) + ); + } +} \ No newline at end of file diff --git a/core/lib/Thelia/Core/HttpKernel/HttpCache/HttpCache.php b/core/lib/Thelia/Core/HttpKernel/HttpCache/HttpCache.php index 01ad22733..b45f0f812 100644 --- a/core/lib/Thelia/Core/HttpKernel/HttpCache/HttpCache.php +++ b/core/lib/Thelia/Core/HttpKernel/HttpCache/HttpCache.php @@ -23,17 +23,20 @@ namespace Thelia\Core\HttpKernel\HttpCache; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\HttpCache\Esi; use Symfony\Component\HttpKernel\HttpCache\HttpCache as BaseHttpCache; use Symfony\Component\HttpKernel\HttpCache\Store; use Symfony\Component\HttpKernel\HttpKernelInterface; +use Thelia\Core\HttpFoundation\Request as TheliaRequest; + /** * Class HttpCache * @package Thelia\Core\HttpKernel\HttpCache * @author manuel raynaud */ -class HttpCache extends BaseHttpCache +class HttpCache extends BaseHttpCache implements HttpKernelInterface { 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); + } + } \ No newline at end of file diff --git a/core/lib/Thelia/Core/TheliaHttpKernel.php b/core/lib/Thelia/Core/TheliaHttpKernel.php index 07dc15eed..73f3a0c0b 100755 --- a/core/lib/Thelia/Core/TheliaHttpKernel.php +++ b/core/lib/Thelia/Core/TheliaHttpKernel.php @@ -43,6 +43,7 @@ use Thelia\Model; class TheliaHttpKernel extends HttpKernel { + protected static $session; protected $container; @@ -79,8 +80,11 @@ class TheliaHttpKernel extends HttpKernel public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true) { //$request->headers->set('X-Php-Ob-Level', ob_get_level()); - $request = $this->initSession($request); - $this->initParam($request); + if ($type == HttpKernelInterface::MASTER_REQUEST) { + $request = $this->initSession($request); + $this->initParam($request); + } + $this->container->enterScope('request'); $this->container->set('request', $request, 'request'); @@ -211,28 +215,32 @@ class TheliaHttpKernel extends HttpKernel 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")) { - $storage->setSaveHandler(new Session\Storage\Handler\NativeFileSessionHandler(Model\ConfigQuery::read("session_config.save_path", THELIA_ROOT . '/local/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"))); + } + + self::$session = $session = new \Thelia\Core\HttpFoundation\Session\Session($storage); } else { - $handlerString = Model\ConfigQuery::read("session_config.handlers", 'Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeFileSessionHandler'); - - $handler = new $handlerString; - - $storage->setSaveHandler($handler); + $session = self::$session; } - 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(); - $request->setSession($session); + $session->start(); + return $request; } } diff --git a/web/index_dev.php b/web/index_dev.php index a89edaf7a..bdbe56eef 100755 --- a/web/index_dev.php +++ b/web/index_dev.php @@ -46,9 +46,10 @@ if ( false === in_array($request->getClientIp(), $trustedIp)) { $response = Response::create('Forbidden', 403)->send(); $thelia->terminate($request, $response); } else { - //$thelia = new HttpCache($thelia); - $response = $thelia->handle($request)->prepare($request)->send(); - $thelia->terminate($request, $response); + $thelia = new HttpCache($thelia); + $response = $thelia->handle($request)->send(); + //$thelia->terminate($request, $response); + } From 48319eeddebe4b27bfa5efaca9c4897ed35b1b68 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Wed, 20 Nov 2013 10:09:12 +0100 Subject: [PATCH 04/15] add new route for testing esi tag --- core/lib/Thelia/Core/EventListener/ViewListener.php | 4 ++-- core/lib/Thelia/Core/Thelia.php | 1 - local/modules/Front/Config/front.xml | 5 +++++ local/modules/Front/Controller/CartController.php | 6 ++++++ templates/frontOffice/default/layout.tpl | 2 +- web/index.php | 2 +- web/index_dev.php | 3 +-- 7 files changed, 16 insertions(+), 7 deletions(-) diff --git a/core/lib/Thelia/Core/EventListener/ViewListener.php b/core/lib/Thelia/Core/EventListener/ViewListener.php index 4bdfd2195..4b1d3343a 100755 --- a/core/lib/Thelia/Core/EventListener/ViewListener.php +++ b/core/lib/Thelia/Core/EventListener/ViewListener.php @@ -90,13 +90,13 @@ class ViewListener implements EventSubscriberInterface $response = new Response($content, $parser->getStatus() ?: 200); } -/* $response->setCache(array( + $response->setCache(array( 'last_modified' => new \DateTime(), 'max_age' => 600, 's_maxage' => 600, 'private' => false, 'public' => true, - ));*/ + )); $event->setResponse($response); } catch (ResourceNotFoundException $e) { diff --git a/core/lib/Thelia/Core/Thelia.php b/core/lib/Thelia/Core/Thelia.php index a251e2ad6..49647f4c9 100755 --- a/core/lib/Thelia/Core/Thelia.php +++ b/core/lib/Thelia/Core/Thelia.php @@ -67,7 +67,6 @@ class Thelia extends Kernel if ($this->debug) { ini_set('display_errors', 1); } - ini_set('display_errors', 1); $this->initPropel(); } diff --git a/local/modules/Front/Config/front.xml b/local/modules/Front/Config/front.xml index 94234aa20..f3a384250 100755 --- a/local/modules/Front/Config/front.xml +++ b/local/modules/Front/Config/front.xml @@ -13,6 +13,11 @@ includes/addedToCart + + Front\Controller\CartController::renderMiniCart + includes/mini-cart + + diff --git a/local/modules/Front/Controller/CartController.php b/local/modules/Front/Controller/CartController.php index d3d24c3bd..9557eab52 100755 --- a/local/modules/Front/Controller/CartController.php +++ b/local/modules/Front/Controller/CartController.php @@ -142,4 +142,10 @@ class CartController extends BaseFrontController return $cartAdd; } + public function renderMiniCart() + { + $response = $this->render('includes/mini-cart'); + return $response; + } + } diff --git a/templates/frontOffice/default/layout.tpl b/templates/frontOffice/default/layout.tpl index d8433e848..c4db8e8ab 100644 --- a/templates/frontOffice/default/layout.tpl +++ b/templates/frontOffice/default/layout.tpl @@ -129,7 +129,7 @@ GNU General Public License : http://www.gnu.org/licenses/ {/elseloop} - {include file="includes/mini-cart.html" nocache} + {/nocache} {/nocache} {/nocache}