vendor/smarty/smarty/libs/sysplugins/smarty_template_compiled.php line 91

  1. <?php
  2. /**
  3.  * Smarty Resource Data Object
  4.  * Meta Data Container for Template Files
  5.  *
  6.  * @package    Smarty
  7.  * @subpackage TemplateResources
  8.  * @author     Rodney Rehm
  9.  * @property   string $content compiled content
  10.  */
  11. class Smarty_Template_Compiled extends Smarty_Template_Resource_Base
  12. {
  13.     /**
  14.      * nocache hash
  15.      *
  16.      * @var string|null
  17.      */
  18.     public $nocache_hash null;
  19.     /**
  20.      * get a Compiled Object of this source
  21.      *
  22.      * @param Smarty_Internal_Template $_template template object
  23.      *
  24.      * @return Smarty_Template_Compiled compiled object
  25.      */
  26.     public static function load($_template)
  27.     {
  28.         $compiled = new Smarty_Template_Compiled();
  29.         if ($_template->source->handler->hasCompiledHandler) {
  30.             $_template->source->handler->populateCompiledFilepath($compiled$_template);
  31.         } else {
  32.             $compiled->populateCompiledFilepath($_template);
  33.         }
  34.         return $compiled;
  35.     }
  36.     /**
  37.      * populate Compiled Object with compiled filepath
  38.      *
  39.      * @param Smarty_Internal_Template $_template template object
  40.      **/
  41.     public function populateCompiledFilepath(Smarty_Internal_Template $_template)
  42.     {
  43.         $source = &$_template->source;
  44.         $smarty = &$_template->smarty;
  45.         $this->filepath $smarty->getCompileDir();
  46.         if (isset($_template->compile_id)) {
  47.             $this->filepath .= preg_replace('![^\w]+!''_'$_template->compile_id) .
  48.                                ($smarty->use_sub_dirs DIRECTORY_SEPARATOR '^');
  49.         }
  50.         // if use_sub_dirs, break file into directories
  51.         if ($smarty->use_sub_dirs) {
  52.             $this->filepath .= $source->uid] . $source->uid] . DIRECTORY_SEPARATOR $source->uid] .
  53.                                $source->uid] . DIRECTORY_SEPARATOR $source->uid] . $source->uid] .
  54.                                DIRECTORY_SEPARATOR;
  55.         }
  56.         $this->filepath .= $source->uid '_';
  57.         if ($source->isConfig) {
  58.             $this->filepath .= (int)$smarty->config_read_hidden + (int)$smarty->config_booleanize +
  59.                                (int)$smarty->config_overwrite 4;
  60.         } else {
  61.             $this->filepath .= (int)$smarty->merge_compiled_includes + (int)$smarty->escape_html +
  62.                                (($smarty->merge_compiled_includes && $source->type === 'extends') ?
  63.                                    (int)$smarty->extends_recursion 0);
  64.         }
  65.         $this->filepath .= '.' $source->type;
  66.         $basename $source->handler->getBasename($source);
  67.         if (!empty($basename)) {
  68.             $this->filepath .= '.' $basename;
  69.         }
  70.         if ($_template->caching) {
  71.             $this->filepath .= '.cache';
  72.         }
  73.         $this->filepath .= '.php';
  74.         $this->timestamp $this->exists is_file($this->filepath);
  75.         if ($this->exists) {
  76.             $this->timestamp filemtime($this->filepath);
  77.         }
  78.     }
  79.     /**
  80.      * render compiled template code
  81.      *
  82.      * @param Smarty_Internal_Template $_template
  83.      *
  84.      * @return string
  85.      * @throws Exception
  86.      */
  87.     public function render(Smarty_Internal_Template $_template)
  88.     {
  89.         // checks if template exists
  90.         if (!$_template->source->exists) {
  91.             $type $_template->source->isConfig 'config' 'template';
  92.             throw new SmartyException("Unable to load {$type} '{$_template->source->type}:{$_template->source->name}'");
  93.         }
  94.         if ($_template->smarty->debugging) {
  95.             if (!isset($_template->smarty->_debug)) {
  96.                 $_template->smarty->_debug = new Smarty_Internal_Debug();
  97.             }
  98.             $_template->smarty->_debug->start_render($_template);
  99.         }
  100.         if (!$this->processed) {
  101.             $this->process($_template);
  102.         }
  103.         if (isset($_template->cached)) {
  104.             $_template->cached->file_dependency =
  105.                 array_merge($_template->cached->file_dependency$this->file_dependency);
  106.         }
  107.         if ($_template->source->handler->uncompiled) {
  108.             $_template->source->handler->renderUncompiled($_template->source$_template);
  109.         } else {
  110.             $this->getRenderedTemplateCode($_template);
  111.         }
  112.         if ($_template->caching && $this->has_nocache_code) {
  113.             $_template->cached->hashes$this->nocache_hash ] = true;
  114.         }
  115.         if ($_template->smarty->debugging) {
  116.             $_template->smarty->_debug->end_render($_template);
  117.         }
  118.     }
  119.     /**
  120.      * load compiled template or compile from source
  121.      *
  122.      * @param Smarty_Internal_Template $_smarty_tpl do not change variable name, is used by compiled template
  123.      *
  124.      * @throws Exception
  125.      */
  126.     public function process(Smarty_Internal_Template $_smarty_tpl)
  127.     {
  128.         $source = &$_smarty_tpl->source;
  129.         $smarty = &$_smarty_tpl->smarty;
  130.         if ($source->handler->recompiled) {
  131.             $source->handler->process($_smarty_tpl);
  132.         } elseif (!$source->handler->uncompiled) {
  133.             if (!$this->exists || $smarty->force_compile
  134.                 || ($_smarty_tpl->compile_check && $source->getTimeStamp() > $this->getTimeStamp())
  135.             ) {
  136.                 $this->compileTemplateSource($_smarty_tpl);
  137.                 $compileCheck $_smarty_tpl->compile_check;
  138.                 $_smarty_tpl->compile_check Smarty::COMPILECHECK_OFF;
  139.                 $this->loadCompiledTemplate($_smarty_tpl);
  140.                 $_smarty_tpl->compile_check $compileCheck;
  141.             } else {
  142.                 $_smarty_tpl->mustCompile true;
  143.                 @include $this->filepath;
  144.                 if ($_smarty_tpl->mustCompile) {
  145.                     $this->compileTemplateSource($_smarty_tpl);
  146.                     $compileCheck $_smarty_tpl->compile_check;
  147.                     $_smarty_tpl->compile_check Smarty::COMPILECHECK_OFF;
  148.                     $this->loadCompiledTemplate($_smarty_tpl);
  149.                     $_smarty_tpl->compile_check $compileCheck;
  150.                 }
  151.             }
  152.             $_smarty_tpl->_subTemplateRegister();
  153.             $this->processed true;
  154.         }
  155.     }
  156.     /**
  157.      * compile template from source
  158.      *
  159.      * @param Smarty_Internal_Template $_template
  160.      *
  161.      * @throws Exception
  162.      */
  163.     public function compileTemplateSource(Smarty_Internal_Template $_template)
  164.     {
  165.         $this->file_dependency = array();
  166.         $this->includes = array();
  167.         $this->nocache_hash null;
  168.         $this->unifunc null;
  169.         // compile locking
  170.         if ($saved_timestamp = (!$_template->source->handler->recompiled && is_file($this->filepath))) {
  171.             $saved_timestamp $this->getTimeStamp();
  172.             touch($this->filepath);
  173.         }
  174.         // compile locking
  175.         try {
  176.             // call compiler
  177.             $_template->loadCompiler();
  178.             $this->write($_template$_template->compiler->compileTemplate($_template));
  179.         } catch (Exception $e) {
  180.             // restore old timestamp in case of error
  181.             if ($saved_timestamp && is_file($this->filepath)) {
  182.                 touch($this->filepath$saved_timestamp);
  183.             }
  184.             unset($_template->compiler);
  185.             throw $e;
  186.         }
  187.         // release compiler object to free memory
  188.         unset($_template->compiler);
  189.     }
  190.     /**
  191.      * Write compiled code by handler
  192.      *
  193.      * @param Smarty_Internal_Template $_template template object
  194.      * @param string                   $code      compiled code
  195.      *
  196.      * @return bool success
  197.      * @throws \SmartyException
  198.      */
  199.     public function write(Smarty_Internal_Template $_template$code)
  200.     {
  201.         if (!$_template->source->handler->recompiled) {
  202.             if ($_template->smarty->ext->_writeFile->writeFile($this->filepath$code$_template->smarty) === true) {
  203.                 $this->timestamp $this->exists is_file($this->filepath);
  204.                 if ($this->exists) {
  205.                     $this->timestamp filemtime($this->filepath);
  206.                     return true;
  207.                 }
  208.             }
  209.             return false;
  210.         }
  211.         return true;
  212.     }
  213.     /**
  214.      * Read compiled content from handler
  215.      *
  216.      * @param Smarty_Internal_Template $_template template object
  217.      *
  218.      * @return string content
  219.      */
  220.     public function read(Smarty_Internal_Template $_template)
  221.     {
  222.         if (!$_template->source->handler->recompiled) {
  223.             return file_get_contents($this->filepath);
  224.         }
  225.         return isset($this->content) ? $this->content false;
  226.     }
  227.     /**
  228.      * Load fresh compiled template by including the PHP file
  229.      * HHVM requires a work around because of a PHP incompatibility
  230.      *
  231.      * @param \Smarty_Internal_Template $_smarty_tpl do not change variable name, is used by compiled template
  232.      */
  233.     private function loadCompiledTemplate(Smarty_Internal_Template $_smarty_tpl)
  234.     {
  235.         if (function_exists('opcache_invalidate')
  236.             && (!function_exists('ini_get') || strlen(ini_get("opcache.restrict_api")) < 1)
  237.         ) {
  238.             opcache_invalidate($this->filepathtrue);
  239.         } elseif (function_exists('apc_compile_file')) {
  240.             apc_compile_file($this->filepath);
  241.         }
  242.         if (defined('HHVM_VERSION')) {
  243.             eval('?>' file_get_contents($this->filepath));
  244.         } else {
  245.             include $this->filepath;
  246.         }
  247.     }
  248. }