# File core/lib/Thelia/Core/EventListener/ViewListener.php | thelia

      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\EventListener;
     24:
     25: use Symfony\Component\EventDispatcher\EventSubscriberInterface;
     26: use Symfony\Component\HttpKernel\KernelEvents;
     27: use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
     28: use Symfony\Component\DependencyInjection\ContainerInterface;
     29: use Symfony\Component\HttpFoundation\Request;
     30: use Symfony\Component\HttpFoundation\Response;
     31: use Symfony\Component\Routing\Exception\ResourceNotFoundException;
     32: use Thelia\Core\Template\ParserInterface;
     33:
     34: /**
     35:  *
     36:  * ViewSubscriber Main class subscribing to view http response.
     37:  *
     38:  * @TODO Look if it's possible to block this definition in dependency-injection
     39:  *
     40:  * @author Manuel Raynaud <mraynaud@openstudio.fr>
     41:  */
     42:
     43: class ViewListener implements EventSubscriberInterface
     44: {
     45:     /**
     46:      *
     47:      * @var Symfony\Component\DependencyInjection\ContainerInterface
     48:      */
     49:     private $container;
     50:
     51:     /**
     52:      *
     53:      * @param \Thelia\Core\Template\ParserInterface $parser
     54:      */
     55:     public function __construct(ContainerInterface $container)
     56:     {
     57:         $this->container = $container;
     58:     }
     59:
     60:     /**
     61:      *
     62:      * Launch the parser defined on the constructor and get the result.
     63:      *
     64:      * The result is transform id needed into a Response object
     65:      *
     66:      * @param \Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent $event
     67:      */
     68:     public function onKernelView(GetResponseForControllerResultEvent $event)
     69:     {
     70:         $parser = $this->container->get('parser');
     71:         try {
     72:             $content = $parser->getContent();
     73:
     74:             if ($content instanceof Response) {
     75:                 $event->setResponse($content);
     76:             } else {
     77:                 $event->setResponse(new Response($content, $parser->getStatus() ?: 200));
     78:             }
     79:         } catch(ResourceNotFoundException $e) {
     80:             $event->setResponse(new Response($e->getMessage(), 404));
     81:         }
     82:
     83:     }
     84:
     85:     public function beforeKernelView(GetResponseForControllerResultEvent $event)
     86:     {
     87:         $request = $this->container->get('request');
     88:
     89:         if (!$view = $request->attributes->get('_view')) {
     90:             $request->attributes->set('_view', $this->findView($request));
     91:         }
     92:
     93:     }
     94:
     95:     public function findView(Request $request)
     96:     {
     97:         if (! $view = $request->query->get('view')) {
     98:             $view = "index";
     99:             if ($request->request->has('view')) {
    100:                 $view = $request->request->get('view');
    101:             }
    102:         }
    103:
    104:         return $view;
    105:     }
    106:
    107:
    108:     /**
    109:      *
    110:      * Register the method to execute in this class for a specific event (here the kernel.view event)
    111:      *
    112:      * @return array The event names to listen to
    113:      */
    114:     public static function getSubscribedEvents()
    115:     {
    116:         return array(
    117:             KernelEvents::VIEW =>array(
    118:                 array('onKernelView', 0),
    119:                 array('beforeKernelView', 5)
    120:             )
    121:         );
    122:     }
    123: }
    124: