/** * Check if the template exist and compile it if necessary * * @param string $template Name of the file of the template * @param string|null $parentTemplateFilePath (Optional) Parent template file path (that template which is including this one) * @param int|null|numeric $parentTemplateLine (Optional) Line from parent template that called this method * @param int|null|numeric $parentTemplateOffset (Optional) Offset of parent template where is this function called * * @throws Tpl\Exception * @throws libs\view\Tpl\NotFoundException * * @return string Compiled template absolute path */ protected function checkTemplate($template, $parentTemplateFilePath = null, $parentTemplateLine = null, $parentTemplateOffset = null) { $originalTemplate = $template; $extension = ''; $path = self::resolveTemplatePath($template, $this->config['tpl_dir'], $parentTemplateFilePath, $this->config['tpl_ext']); // normalize path $path = str_replace(array('//', '//'), '/', $path); $parsedTemplateFilepath = $this->config['cache_dir'] . basename($originalTemplate) . "." . md5(dirname($path) . serialize($this->config['checksum']) . $originalTemplate) . '.rtpl.php'; // if the template doesn't exsist throw an error if (!$path) { $traceString = ''; if ($parentTemplateFilePath && $parentTemplateLine && $parentTemplateOffset) { $traceString = ', included from "' . $parentTemplateFilePath . '" on line ' . $parentTemplateLine . ', offset ' . $parentTemplateOffset; } $e = new Tpl\NotFoundException('Template ' . $originalTemplate . ' not found' . $traceString); throw $e->templateFile($originalTemplate); } /** * Check if there is an already compiled version * * @config bool allow_compile */ if (!$this->config['allow_compile']) { // check if there is a compiled version if (!is_file($parsedTemplateFilepath)) { // allow first compilation of file if (!$this->config['allow_compile_once']) { throw new NotFoundException('Template cache file "' . $parsedTemplateFilepath . '" is missing and "allow_compile", "allow_compile_once" are disabled in configuration'); } } else { return $parsedTemplateFilepath; } } /** * Run the parser if file was not updated since last compilation time */ if ($this->config['debug'] or !file_exists($parsedTemplateFilepath) or filemtime($parsedTemplateFilepath) < filemtime($path)) { $parser = new Tpl\Parser($this); $parser->compileFile($path, $parsedTemplateFilepath); } return $parsedTemplateFilepath; }
/** * Check if the template exist and compile it if necessary * * @param string $template: name of the file of the template * * @throw \Rain\Tpl\NotFoundException the file doesn't exists * @return string: full filepath that php must use to include */ protected function checkTemplate($template) { // set filename $templateName = basename($template); $templateBasedir = strpos($template, '/') !== false ? dirname($template) . '/' : null; $templateDirectory = null; $templateFilepath = null; $parsedTemplateFilepath = null; // Make directories to array for multiple template directory $templateDirectories = $this->config['tpl_dir']; if (!is_array($templateDirectories)) { $templateDirectories = array($templateDirectories); } $isFileNotExist = true; // absolute path if ($template[0] == '/') { $templateDirectory = $templateBasedir; $templateFilepath = $templateDirectory . $templateName . '.' . $this->config['tpl_ext']; $parsedTemplateFilepath = $this->config['cache_dir'] . $templateName . "." . md5($templateDirectory . serialize($this->config['checksum'])) . '.rtpl.php'; // For check templates are exists if (file_exists($templateFilepath)) { $isFileNotExist = false; } } else { foreach ($templateDirectories as $templateDirectory) { $templateDirectory .= $templateBasedir; $templateFilepath = $templateDirectory . $templateName . '.' . $this->config['tpl_ext']; $parsedTemplateFilepath = $this->config['cache_dir'] . $templateName . "." . md5($templateDirectory . serialize($this->config['checksum'])) . '.rtpl.php'; // For check templates are exists if (file_exists($templateFilepath)) { $isFileNotExist = false; break; } } } // if the template doesn't exsist throw an error if ($isFileNotExist === true) { $e = new Tpl\NotFoundException('Template ' . $templateName . ' not found!'); throw $e->templateFile($templateFilepath); } // Compile the template if the original has been updated if ($this->config['debug'] || !file_exists($parsedTemplateFilepath) || filemtime($parsedTemplateFilepath) < filemtime($templateFilepath)) { $parser = new Tpl\Parser($this->config, static::$plugins, static::$registered_tags); $parser->compileFile($templateName, $templateBasedir, $templateDirectory, $templateFilepath, $parsedTemplateFilepath); } return $parsedTemplateFilepath; }
/** * Check if the template exist and compile it if necessary * * @param string $template: name of the file of the template * * @throw \Rain\Tpl\NotFoundException the file doesn't exists * @return string: full filepath that php must use to include */ protected function checkTemplate($template) { // fix dummy windows when include template with a sub folder $template = str_replace(array("\\b", "\f", "\n", "\\p", "\r"), array(DIRECTORY_SEPARATOR . 'b', DIRECTORY_SEPARATOR . 'f', DIRECTORY_SEPARATOR . 'n', DIRECTORY_SEPARATOR . 'p', DIRECTORY_SEPARATOR . 'r'), $template); // set filename $templateName = basename($template); $templateBasedir = strpos($template, DIRECTORY_SEPARATOR) ? dirname($template) . DIRECTORY_SEPARATOR : null; $templateDirectory = null; $templateFilepath = null; $parsedTemplateFilepath = null; // Make directories to array for multiple template directory $templateDirectories = $this->config['tpl_dir']; if (!is_array($templateDirectories)) { $templateDirectories = array($templateDirectories); } $isFileNotExist = true; foreach ($templateDirectories as $templateDirectory) { $templateDirectory .= $templateBasedir; $templateFilepath = $templateDirectory . $templateName . '.' . $this->config['tpl_ext']; $parsedTemplateFilepath = $this->config['cache_dir'] . $templateName . "." . md5($templateDirectory . serialize($this->config['checksum'])) . '.rtpl.php'; // For check templates are exists if (file_exists($templateFilepath)) { $isFileNotExist = false; break; } } // if the template doesn't exsist throw an error if ($isFileNotExist === true) { $e = new Tpl\NotFoundException('Template ' . $templateName . ' not found!'); throw $e->templateFile($templateFilepath); } // Compile the template if the original has been updated if ($this->config['debug'] || !file_exists($parsedTemplateFilepath) || filemtime($parsedTemplateFilepath) < filemtime($templateFilepath)) { $parser = new Tpl\Parser($this->config, static::$plugins, static::$registered_tags); $parser->compileFile($templateName, $templateBasedir, $templateDirectory, $templateFilepath, $parsedTemplateFilepath); } return $parsedTemplateFilepath; }