/*!
Compiles the template file \a $file and returns \c true if the compilation was OK.
\param $returnResourceData If \c true then the returned value will be the resourcedata structure
\sa validateTemplateFile(), fetch()
197252
function compileTemplateFile( $file, $returnResourceData = false )
    {
        $this->resetErrorLog();

        if ( !file_exists( $file ) )
            return false;
        $resourceHandler = $this->resourceFor( $file, $resourceName, $templateName );
        if ( !$resourceHandler )
            return false;
        $resourceData = $this->resourceData( $resourceHandler, $file, $resourceName, $templateName );
        $resourceData['key-data'] = "file:" . $file;
        $key = md5( $resourceData['key-data'] );
        $extraParameters = array();
        $resourceHandler->handleResource( $this, $resourceData, eZTemplate::RESOURCE_FETCH, $extraParameters );

        $isCompiled = false;
        if ( isset( $resourceData['compiled-template'] ) )
            $isCompiled = $resourceData['compiled-template'];

        if ( !$isCompiled )
        {
            $root =& $resourceData['root-node'];
            $root = array( eZTemplate::NODE_ROOT, false );
            $templateText = $resourceData["text"];
            $rootNamespace = '';
            $this->parse( $templateText, $root, $rootNamespace, $resourceData );
            if ( eZTemplate::isDebugEnabled() )
            {
                $this->appendDebugNodes( $root, $resourceData );
            }

            $result = eZTemplateCompiler::compileTemplate( $this, $key, $resourceData );
        }
        else
        {
            $result = true;
        }

        if ( $returnResourceData )
        {
            $resourceData['result'] = $result;
            return $resourceData;
        }
        return $result;
    }
928191
protected function shouldNotRenderTemplate($controller, $returnValue)
    {
        if ($controller instanceof Controller) {
            if ($controller->getHeaders()->has('location')) {
                return true;
            }
        }

        return $returnValue === false;
    }
246928
public function getFragmentFile($template, $content)
	{
        $tplCompiledFile = static::getCompiledPath() . '/fragment/' . ltrim($template, '/') . '.php';
        
        if (!$this->options['isCompiled'] || !file_exists($tplCompiledFile) ) {
            $tplCompiledPath = dirname($tplCompiledFile);
            if (!file_exists($tplCompiledPath)) {
                Dir::create($tplCompiledPath, 0755);
            }
            
            $content = $this->parse($content);
            $strlen = file_put_contents($tplCompiledFile, $content);
            chmod ($tplCompiledFile, 0755);
        }
        
        return $tplCompiledFile;
	}
25146
public function populateCompiledFilepath(Smarty_Template_Compiled $compiled, Smarty_Internal_Template $_template)
    {
        $_compile_id = isset($_template->compile_id) ? preg_replace('![^\w\|]+!', '_', $_template->compile_id) : null;
        $_filepath = $compiled->source->uid;
        // if use_sub_dirs, break file into directories
        if ($_template->smarty->use_sub_dirs) {
            $_filepath = substr($_filepath, 0, 2) . DS
             . substr($_filepath, 2, 2) . DS
             . substr($_filepath, 4, 2) . DS
             . $_filepath;
        }
        $_compile_dir_sep = $_template->smarty->use_sub_dirs ? DS : '^';
        if (isset($_compile_id)) {
            $_filepath = $_compile_id . $_compile_dir_sep . $_filepath;
        }
        // caching token
        if ($_template->caching) {
            $_cache = '.cache';
        } else {
            $_cache = '';
        }
        $_compile_dir = $_template->smarty->getCompileDir();
        // set basename if not specified
        $_basename = $this->getBasename($compiled->source);
        if ($_basename === null) {
            $_basename = basename( preg_replace('![^\w\/]+!', '_', $compiled->source->name) );
        }
        // separate (optional) basename by dot
        if ($_basename) {
            $_basename = '.' . $_basename;
        }

        $compiled->filepath = $_compile_dir . $_filepath . '.' . $compiled->source->type . $_basename . $_cache . '.php';
    }
865649
protected function canCreateView($file, $force, Resource $resource)
    {
        $viewName = $this->getViewName();

        if ($resource->isProtected($viewName)) {
            $this->warn('The ' . $viewName . ' is protected and cannot be regenerated. To regenerate the file, unprotect it from the resource file.');

            return false;
        }

        if ($this->alreadyExists($file) && !$force) {
            $this->error($this->getViewNameFromFile($file) . ' view already exists.');

            return false;
        }

        if (!$resource->hasFields()) {
            $this->error('You must provide at least one field to generate the views!');

            return false;
        }

        if (!$resource->hasPrimaryField()) {
            $this->error('None of the fields is set primary! You must assign on of the fields to be a primary field.');

            return false;
        }

        return true;
    }
972557
public function make($templatePath, $templateData, $filePathToGenerate)
    {
        // We first need to compile the template,
        // according to the data that we provide.
        $template = $this->compile($templatePath, $templateData, new TemplateCompiler);

        // Now that we have the compiled template,
        // we can actually generate the file.
        $this->file->make($filePathToGenerate, $template);
    }
922697
public function getCompiled(File $file, $pretty = true)
    {
        $output = $this->template;
        $output = str_replace('{ext}', $file->getExt(), $output);
        $output = str_replace('{filename}', $this->sluggify($file->getFilename()), $output);

        $filePath = str_replace('\\', '/', $file->getPath());
        if (substr($filePath, 0, 1) === '/') {
            $filePath = substr($filePath, 1);
        }
        if (substr($filePath, -1, 1) === '/') {
            $filePath = substr($filePath, 0, -1);
        }
        $filePath = preg_replace('!/+!', '/', $filePath);
        $output = str_replace('{path}', $filePath, $output);

        /** @var \DateTime $date */
        if ($date = $file->getData('date')) {
            $output = str_replace('{year}', $file->getData('year', $date->format('Y')), $output);
            $output = str_replace('{month}', $file->getData('month', $date->format('m')), $output);
            $output = str_replace('{day}', $file->getData('day', $date->format('d')), $output);
        }

        /** @var Pagination $pagination */
        if ($pagination = $file->getData('pagination')) {
            if ($pagination instanceof Pagination) {
                if ($pagination->currentPage == 1) {
                    $page = 'index';
                } else {
                    $page = $pagination->currentPage;
                }
                $output = str_replace('{page}', $page, $output);
            }
        }

        // Regex: {category(,[0-9]+)?}

        if (preg_match('{category(,)?([0-9]+)?}', $output, $categoryMatches) > 0 && $categories = $file->getData('categories')) {
            $categoryText = '';
            if (is_array($categories)) {
                sort($categories, SORT_NATURAL | SORT_FLAG_CASE);
                $limit = (count($categoryMatches) === 3) ? $categoryMatches[2] : count($categories);
                for ($i = 0; $i < $limit; $i++) {
                    $categoryText .= $this->sluggify($categories[$i]).'/';
                }
            } else {
                $categoryText = $this->sluggify($categories).'/';
            }
            if (substr($categoryText, -1, 1) === '/') {
                $categoryText = substr($categoryText, 0, -1);
            }

            $output = preg_replace('({category(,[0-9]+)?})', $categoryText, $output);
            if (is_null($output)) {
                throw new \Exception('Error occurred while replacing category permalink string.');
            }
        }

        $output = str_replace('{slug}', $file->getData('slug', $this->sluggify($file->getData('title', $file->getFilename()))), $output);

        if (substr($output, 0, 1) !== '/') {
            $output = '/'.$output;
        }

        // Ensure valid slashes for url
        $output = str_replace('\\', '/', $output);

        if ($pretty === true && $file->getData('pretty_permalink', true)) {
            return $this->prettify($output);
        }

        return $output;
    }
97924
protected function validateReturnTypeNotEmpty(
        File $file,
        $functionStart,
        $returnType,
        array $suggestedReturnTypes
    ) {
        $filteredReturnTypes = array_filter($suggestedReturnTypes);
        if (empty($returnType)
            && count($suggestedReturnTypes) == 1
            && count($filteredReturnTypes) == 1
        ) {
            $file->addWarning(
                sprintf(
                    'Method should have a return type in PHP7, use %s',
                    reset($filteredReturnTypes)
                ),
                $functionStart,
                'MissingReturnType'
            );
        }
    }
1334138
function compileTemplate( $tpl, $keyData, $uri, $res, $templatePath, &$extraParameters, &$resourceData )
    {
        $key = $this->cacheKey( $keyData, $res, $templatePath, $extraParameters );
        return eZTemplateCompiler::compileTemplate( $tpl, $key, $resourceData );
    }
264201
function (f, data, context) {
    try {
        return _.template(data)(context);
    } catch(e) {
        log('error', 'could not parse template file', f);
        throw(e);
    }
}
746516
public function isExpired($viewData)
    {

        // adds ability to force template recompile
        if ($this->forceTemplateRecompile) {
            return true;
        }

        $compiled = $this->getCompiledPath($viewData);

        // If the compiled file doesn't exist we will indicate that the view is expired
        // so that it can be re-compiled. Else, we will verify the last modification
        // of the views is less than the modification times of the compiled views.
        if ( ! $this->cachePath || ! $this->files->exists($compiled))
        {
            return true;
        }

        // If set to 0, then return cache has expired
        if (property_exists($viewData, 'secondsTemplateCacheExpires')) {
            if ($viewData->secondsTemplateCacheExpires == 0) {
                return true;
            }
        } else {
            $viewData->secondsTemplateCacheExpires = 0;
        }

        // Note: The lastModified time for a file on homestead will use the time from the host system.
        //       This means the vm time could be off, so setting the timeout to seconds may not work as expected.

        return time() >= ($this->files->lastModified($compiled) + $viewData->secondsTemplateCacheExpires) ;
    }
1123045
static function canRestoreCache( $key, $timestamp, $templateFilepath )
    {
        if ( !eZTemplateTreeCache::isCacheEnabled() )
            return false;

        $templateCache =& eZTemplateTreeCache::cacheTable();
        $key = eZTemplateTreeCache::internalKey( $key );
        if ( isset( $templateCache[$key] ) )
        {
            return false;
        }
        $cacheFileName = eZTemplateTreeCache::treeCacheFilename( $key, $templateFilepath );

        $php = new eZPHPCreator( eZTemplateTreeCache::cacheDirectory(), $cacheFileName );
        return $php->canRestore( $timestamp );
    }
268381
private function getTemplate($sub_directories, $controller, $is_resource = false)
    {
        $file = __DIR__ . "/../templates/controller/";
        $file .= $is_resource ? "controller-with-resource.php.dist" : "controller.php.dist";

        if (file_exists($file))
        {
            $template = file_get_contents($file);

            return strtr($template, [
                '{{namespace}}' => get_app_namespace(),
                '{{sub_directories}}' => $sub_directories,
                '{{controller}}' => $controller
            ]);
        }

        return false;
    }
1287884
public function compileTemplateSource()
    {
        if (!$this->source->recompiled) {
            $this->properties['file_dependency'] = array();
            if ($this->source->components) {
                // for the extends resource the compiler will fill it
                // uses real resource for file dependency
                // $source = end($this->source->components);
                // $this->properties['file_dependency'][$this->source->uid] = array($this->source->filepath, $this->source->timestamp, $source->type);
            } else {
                $this->properties['file_dependency'][$this->source->uid] = array($this->source->filepath, $this->source->timestamp, $this->source->type);
            }
        }
        // compile locking
        if ($this->smarty->compile_locking && !$this->source->recompiled) {
            if ($saved_timestamp = $this->compiled->timestamp) {
                touch($this->compiled->filepath);
            }
        }
        // call compiler
        try {
            $code = $this->compiler->compileTemplate($this);
        }
        catch (Exception $e) {
            // restore old timestamp in case of error
            if ($this->smarty->compile_locking && !$this->source->recompiled && $saved_timestamp) {
                touch($this->compiled->filepath, $saved_timestamp);
            }
            throw $e;
        }
        // compiling succeded
        if (!$this->source->recompiled && $this->compiler->write_compiled_code) {
            // write compiled template
            $_filepath = $this->compiled->filepath;
            if ($_filepath === false) {
                throw new SmartyException('getCompiledFilepath() did not return a destination to save the compiled template to');
            }
            Smarty_Internal_Write_File::writeFile($_filepath, $code, $this->smarty);
            $this->compiled->exists = true;
            $this->compiled->isCompiled = true;
        }
        // release compiler object to free memory
        unset($this->compiler);
    }
1282767
protected function validateReturnTypeValue(
        File $file,
        $functionStart,
        $returnType,
        array $suggestedReturnTypes
    ) {
        $filteredReturnTypes = array_filter($suggestedReturnTypes);
        if (!empty($returnType)
            && count($filteredReturnTypes)
            && !in_array($returnType, $filteredReturnTypes)
        ) {
            $file->addWarning(
                sprintf(
                    'Method return type should be one of (%s)',
                    implode('|', $suggestedReturnTypes)
                ),
                $functionStart,
                'WrongReturnType'
            );
        }
    }
558823
public function compileTemplateSource()
    {
        $source = $this->getSource();
        $this->addDependency($source);
        return $this->getCompiler()->compileTemplate();
    }
746146
protected function compiled($template)
    {
        $template = ltrim($template, '/');
        if (strrchr($template, '.') == $this->extension)
            $template = substr($template, 0, -strlen($this->extension));
        $file = $this->dir . DIRECTORY_SEPARATOR . $template . $this->extension;
        if (!is_file($file))
            throw new \RuntimeException("template not found:$file!");

        $modified = filemtime($file);
        $compiled = $this->temp . DIRECTORY_SEPARATOR . $template . ".{$modified}.php";
        if (!$this->debug && is_file($compiled) && filemtime($compiled) >= $modified)
            return $compiled;

        $content = self::compile(file_get_contents($file), $this->left, $this->right);
        self::writeFile($compiled, $content, $this->temp);

        return $compiled;
    }