Merge pull request #110 from thelia/http_caching

Http caching
This commit is contained in:
Manuel Raynaud
2013-11-22 01:18:01 -08:00
12 changed files with 209 additions and 41 deletions

View File

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

View File

@@ -4,12 +4,6 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://thelia.net/schema/dic/config http://thelia.net/schema/dic/config/thelia-1.0.xsd">
<parameters>
<parameter key="thelia.actionEvent" type="collection">
</parameter>
</parameters>
<services>
<service id="thelia.action.cart" class="Thelia\Action\Cart">

View File

@@ -4,11 +4,15 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://thelia.net/schema/dic/config http://thelia.net/schema/dic/config/thelia-1.0.xsd">
<parameters>
<parameter key="esi.class">Symfony\Component\HttpKernel\HttpCache\Esi</parameter>
<parameter key="esi_listener.class">Symfony\Component\HttpKernel\EventListener\EsiListener</parameter>
<parameter key="fragment.renderer.esi.class">Symfony\Component\HttpKernel\Fragment\EsiFragmentRenderer</parameter>
<parameter key="fragment.renderer.inline.class">Symfony\Component\HttpKernel\Fragment\InlineFragmentRenderer</parameter>
</parameters>
<services>
<!-- URL maganement -->
<service id="thelia.url.manager" class="Thelia\Tools\URL">
<argument type="service" id="service_container" />
</service>
@@ -17,6 +21,20 @@
<argument type="service" id="service_container" />
</service>
<service id="esi" class="%esi.class%" />
<service id="esi_listener" class="%esi_listener.class%">
<tag name="kernel.event_subscriber" />
<argument type="service" id="esi" on-invalid="ignore" />
</service>
<service id="fragment.renderer.inline" class="%fragment.renderer.inline.class%">
<argument type="service" id="http_kernel" />
</service>
<service id="fragment.renderer.esi" class="%fragment.renderer.esi.class%">
<argument type="service" id="esi" />
<argument type="service" id="fragment.renderer.inline" />
</service>
<!--
A ControllerResolver that supports "a:b:c", "service:method" and class::method" notations,
thus allowing the definition of controllers as service (see http://symfony.com/fr/doc/current/cookbook/controller/service.html)
@@ -70,8 +88,6 @@
<service id="mailer" class="Thelia\Mailer\MailerFactory">
<argument type="service" id="event_dispatcher"/>
</service>
</services>
</config>

View File

@@ -91,7 +91,11 @@
<argument type="service" id="request" />
</service>
<service id="smarty.plugin.esi" class="Thelia\Core\Template\Smarty\Plugins\Esi" scope="request">
<tag name="thelia.parser.register_plugin"/>
<argument type="service" id="fragment.renderer.esi" />
<argument type="service" id="request" />
</service>
</services>

View File

@@ -22,6 +22,7 @@
/*************************************************************************************/
namespace Thelia\Core\Bundle;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Scope;
@@ -61,7 +62,7 @@ class TheliaBundle extends Bundle
$container
->addCompilerPass(new TranslatorPass())
->addCompilerPass(new RegisterListenersPass())
->addCompilerPass(new RegisterListenersPass(), PassConfig::TYPE_AFTER_REMOVING)
->addCompilerPass(new RegisterParserPluginPass())
->addCompilerPass(new RegisterRouterPass())
->addCompilerPass(new RegisterCouponPass())

View File

@@ -85,19 +85,11 @@ class ViewListener implements EventSubscriberInterface
$content = $parser->render($request->attributes->get('_view').".html");
if ($content instanceof Response) {
$response = $content;$event->setResponse($content);
$response = $content;
} else {
$response = new Response($content, $parser->getStatus() ?: 200);
}
/* $response->setCache(array(
'last_modified' => new \DateTime(),
'max_age' => 600,
's_maxage' => 600,
'private' => false,
'public' => true,
));*/
$event->setResponse($response);
} catch (ResourceNotFoundException $e) {
throw new NotFoundHttpException();

View File

@@ -0,0 +1,71 @@
<?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\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 <mraynaud@openstudio.fr>
*/
class HttpCache extends BaseHttpCache implements HttpKernelInterface
{
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
)
);
}
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

@@ -0,0 +1,82 @@
<?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\Template\Smarty\Plugins;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Fragment\EsiFragmentRenderer;
use Thelia\Core\Template\Smarty\AbstractSmartyPlugin;
use Thelia\Core\Template\Smarty\an;
use Thelia\Core\Template\Smarty\SmartyPluginDescriptor;
/**
* Class Esi
* @package Thelia\Core\Template\Smarty\Plugins
* @author Manuel Raynaud <mraynaud@openstudio.fr>
*/
class Esi extends AbstractSmartyPlugin
{
protected $esiFragmentRender;
protected $request;
public function __construct(EsiFragmentRenderer $esiFragmentRenderer, Request $request)
{
$this->esiFragmentRender = $esiFragmentRenderer;
$this->request = $request;
}
public function renderEsi($params, $template = null)
{
$path = $this->getParam($params, 'path');
$alt = $this->getParam($params, 'alt');
$ignore_errors = $this->getParam($params, 'ignore_errors');
$comment = $this->getParam($params, 'comment');
if(null === $path) {
return;
}
$response = $this->esiFragmentRender->render($path, $this->request, array(
'alt' => $alt,
'ignore_errors' => $ignore_errors,
'comment' => $comment
));
if (!$response->isSuccessful()) {
return null;
}
return $response->getContent();
}
/**
* @return an array of SmartyPluginDescriptor
*/
public function getPluginDescriptors()
{
return array(
new SmartyPluginDescriptor('function', 'render_esi', $this, 'renderEsi')
);
}
}

View File

@@ -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,26 +215,29 @@ 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_LOCAL_DIR . '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);
return $request;

View File

@@ -8,12 +8,12 @@
<default key="_controller">Thelia\Controller\Front\DefaultController::noAction</default>
<default key="_view">includes/mini-cart</default>
</route>
<route id="ajax.addCartMessage" path="/ajax/addCartMessage">
<default key="_controller">Thelia\Controller\Front\DefaultController::noAction</default>
<default key="_view">includes/addedToCart</default>
</route>
<!-- Register -->
<route id="customer.create.process" path="/register" methods="post">
<default key="_controller">Front\Controller\CustomerController::createAction</default>

View File

@@ -1,4 +1,5 @@
<?php
use Thelia\Core\HttpKernel\HttpCache\HttpCache;
use Thelia\Core\Thelia;
use Thelia\Core\HttpFoundation\Request;
@@ -10,7 +11,7 @@ require __DIR__ . '/../core/bootstrap.php';
$request = Request::createFromGlobals();
$thelia = new Thelia("prod", false);
//$thelia = new HttpCache($thelia);
$response = $thelia->handle($request)->prepare($request)->send();
$thelia->terminate($request, $response);

View File

@@ -24,6 +24,7 @@
use Thelia\Core\Thelia;
use Thelia\Core\HttpFoundation\Response;
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)) {
} else {
$response = $thelia->handle($request)->prepare($request)->send();
$thelia->terminate($request, $response);
}