      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\Template;
     24:
     25: use Symfony\Component\HttpFoundation\Response;
     26: use Thelia\Core\Template\ParserInterface;
     27: use Symfony\Component\DependencyInjection\ContainerInterface;
     28: use Symfony\Component\HttpFoundation\Request;
     29: use Symfony\Component\Routing\Exception\ResourceNotFoundException;
     30: use Symfony\Component\Config\ConfigCache;
     31:
     32: use Thelia\Tpex\Tpex;
     33: use Thelia\Log\Tlog;
     34:
     35: /**
     36:  *
     37:  * Master class of Thelia's parser. The loop mechanism depends of this parser
     38:  *
     39:  * From this class all the parser is lunch
     40:  *
     41:  *
     42:  * @author Manuel Raynaud <mraynaud@openstudio.fr>
     43:  */
     44:
     45:
     46: class Parser implements ParserInterface
     47: {
     48:     const PREFIXE = 'prx';
     49:
     50:     const SHOW_TIME = true;
     51:     const ALLOW_DEBUG = true;
     52:     const USE_CACHE = true;
     53:
     54:
     55:     /**
     56:      *
     57:      * @var Symfony\Component\DependencyInjection\ContainerInterface
     58:      */
     59:     protected $container;
     60:
     61:     protected $content;
     62:     protected $status = 200;
     63:
     64:     /**
     65:      *
     66:      * @var Thelia\Tpex\Tpex
     67:      */
     68:     protected $tpex;
     69:
     70:     protected $template = "default";
     71:
     72:     /**
     73:      *
     74:      * @param type $container
     75:      *
     76:      * public function __construct(ContainerBuilder $container)
     77:      */
     78:     public function __construct(ContainerInterface $container)
     79:     {
     80:         $this->container = $container;
     81:     }
     82:
     83:     /**
     84:      *
     85:      * @return Symfony\Component\HttpFoundation\Request
     86:      */
     87:     public function getRequest()
     88:     {
     89:         return $this->container->get('request');
     90:     }
     91:
     92:     /**
     93:      *
     94:      * @return Symfony\Component\EventDispatcher\EventDispatcher
     95:      */
     96:     public function getDispatcher()
     97:     {
     98:         return $this->container->get('dispatcher');
     99:     }
    100:
    101:     /**
    102:      *
    103:      * This method must return a Symfony\Component\HttpFoudation\Response instance or the content of the response
    104:      *
    105:      */
    106:     public function getContent()
    107:     {
    108:        $this->loadParser();
    109:
    110:        return $this->content;
    111:     }
    112:
    113:     /**
    114:      *
    115:      * set $content with the body of the response or the Response object directly
    116:      *
    117:      * @param string|Symfony\Component\HttpFoundation\Response $content
    118:      */
    119:     public function setContent($content)
    120:     {
    121:         $this->content = $content;
    122:     }
    123:
    124:     /**
    125:      *
    126:      * @return type the status of the response
    127:      */
    128:     public function getStatus()
    129:     {
    130:         return $this->status;
    131:     }
    132:
    133:     /**
    134:      *
    135:      * status HTTP of the response
    136:      *
    137:      * @param int $status
    138:      */
    139:     public function setStatus($status)
    140:     {
    141:         $this->status = $status;
    142:     }
    143:
    144:     /**
    145:      * Main parser function, load the parser
    146:      */
    147:     public function loadParser()
    148:     {
    149:         $content = $this->openFile($this->getRequest());
    150:
    151:         $tpex = $this->container->get("template");
    152:
    153:         $tpex->setBaseDir(THELIA_TEMPLATE_DIR . rtrim($this->template, "/") . "/");
    154:         $tpex->setContent($content);
    155:
    156:         $this->setContent($tpex->execute());
    157:     }
    158:
    159:     protected function openFile(Request $request)
    160:     {
    161:         $file = $request->attributes->get('_view');
    162:         $fileName = THELIA_TEMPLATE_DIR . rtrim($this->template, "/") . "/" . $file . ".html";
    163:         if (file_exists($fileName)) {
    164:             $content = file_get_contents($fileName);
    165:         } else {
    166:             throw new ResourceNotFoundException(sprintf("%s file not found in %s template", $file, $this->template));
    167:         }
    168:
    169:         return $content;
    170:     }
    171: }
    172: