      1: <?php
      2: /*************************************************************************************/
      3: /*                                                                                   */
      4: /*      Thelia                                                                       */
      5: /*                                                                                   */
      6: /*      Copyright (c) OpenStudio                                                     */
      7: /*  email : info@thelia.net                                                      */
      8: /*      web : http://www.thelia.net                                                  */
      9: /*                                                                                   */
     10: /*      This program is free software; you can redistribute it and/or modify         */
     11: /*      it under the terms of the GNU General Public License as published by         */
     12: /*      the Free Software Foundation; either version 3 of the License                */
     13: /*                                                                                   */
     14: /*      This program is distributed in the hope that it will be useful,              */
     15: /*      but WITHOUT ANY WARRANTY; without even the implied warranty of               */
     16: /*      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                */
     17: /*      GNU General Public License for more details.                                 */
     18: /*                                                                                   */
     19: /*      You should have received a copy of the GNU General Public License            */
     20: /*      along with this program. If not, see <http://www.gnu.org/licenses/>.     */
     21: /*                                                                                   */
     22: /*************************************************************************************/
     23: namespace Thelia\Core;
     24:
     25: use Symfony\Component\HttpKernel\HttpKernel;
     26: use Symfony\Component\HttpKernel\HttpKernelInterface;
     27: use Symfony\Component\HttpFoundation\Request;
     28: use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
     29: use Symfony\Component\DependencyInjection\ContainerInterface;
     30: use Symfony\Component\EventDispatcher\EventDispatcherInterface;
     31: use Symfony\Component\HttpFoundation\Session;
     32:
     33: use Thelia\Model;
     34:
     35: /**
     36:  *
     37:  * @author Manuel Raynaud <mraynaud@openstudio.fr>
     38:  */
     39:
     40: class TheliaHttpKernel extends HttpKernel
     41: {
     42:     public function __construct(EventDispatcherInterface $dispatcher, ContainerInterface $container, ControllerResolverInterface $controllerResolver)
     43:     {
     44:         parent::__construct($dispatcher, $controllerResolver);
     45:
     46:         $this->container = $container;
     47:     }
     48:
     49:     /**
     50:      * Handles a Request to convert it to a Response.
     51:      *
     52:      * When $catch is true, the implementation must catch all exceptions
     53:      * and do its best to convert them to a Response instance.
     54:      *
     55:      * @param Request $request A Request instance
     56:      * @param integer $type    The type of the request
     57:      *                          (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST)
     58:      * @param Boolean $catch Whether to catch exceptions or not
     59:      *
     60:      * @return Response A Response instance
     61:      *
     62:      * @throws \Exception When an Exception occurs during processing
     63:      *
     64:      * @api
     65:      */
     66:     public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
     67:     {
     68:         //$request->headers->set('X-Php-Ob-Level', ob_get_level());
     69:         $request = $this->initSession($request);
     70:         $this->container->enterScope('request');
     71:         $this->container->set('request', $request, 'request');
     72:
     73:         try {
     74:             $response = parent::handle($request, $type, $catch);
     75:         } catch (\Exception $e) {
     76:             $this->container->leaveScope('request');
     77:
     78:             throw $e;
     79:         }
     80:
     81:         $this->container->leaveScope('request');
     82:
     83:         return $response;
     84:     }
     85:
     86:     protected function initSession(Request $request)
     87:     {
     88:
     89:         $storage = new Session\Storage\NativeSessionStorage();
     90:
     91:         if (Model\ConfigQuery::read("session_config.default")) {
     92:             $storage->setSaveHandler(new Session\Storage\Handler\NativeFileSessionHandler(Model\ConfigQuery::read("session_config.save_path", THELIA_ROOT . '/local/session/')));
     93:         } else {
     94:             $handlerString = Model\ConfigQuery::read("session_config.handlers");
     95:
     96:             $handler = new $handlerString;
     97:
     98:             $storage->setSaveHandler($handler);
     99:         }
    100:
    101:         if (Model\ConfigQuery::read("session_config.config", null)) {
    102:            $storage->setOptions(json_decode(Model\ConfigQuery::read("session_config.config")));
    103:         }
    104:
    105:
    106:         $session = new Session\Session($storage);
    107:         $session->start();
    108:
    109:         $request->setSession($session);
    110:
    111:         return $request;
    112:     }
    113: }
    114: