/**
  * Check if the path to the compiled file is correct with findCompiled().
  * It runs tests for multiple template files which the path is known beforehand.
  */
 public function testFindCompiled()
 {
     $templates = array(array('zhadum.ezt', $this->templateCompiledPath . "/compiled_templates" . '/xhtml-updqr0/zhadum-d282602de179f8e7b6a24c6d96eb525d.php'), array('pagelayout.ezt', $this->templateCompiledPath . "/compiled_templates" . '/xhtml-updqr0/pagelayout-ab7f51e3c7e7dab104986ad939256599.php'));
     foreach ($templates as $templateData) {
         $template = $templateData[0];
         $path = $templateData[1];
         $context = new ezcTemplateXhtmlContext();
         $t = new ezcTemplate();
         $t->configuration = new ezcTemplateConfiguration($this->templatePath, $this->templateCompiledPath);
         $code = ezcTemplateCompiledCode::findCompiled($template, $context, $t);
         self::assertSame($path, $code->path, "Total path element does not match expected value.");
     }
 }
Esempio n. 2
0
 /**
  * Processes the specified template source and returns the output string.
  *
  * Note: The first time a template is accessed it needs to be compiled so the
  * execution time will be higher than subsequent calls.
  *
  * @param string $location The path to the template file to process, can be a PHP stream.
  * @param ezcTemplateConfiguration $config Optional configuration object which overrides
  *                                         the default one defined in this object ($configuration).
  * @return string
  *
  * @apichange Remove the test for ezcTemplateLocationInterface as it's deprecated.
  *
  * @throws ezcTemplateParserException
  *         If the template could not be compiled.
  * @throws ezcTemplateFileNotWriteableException
  *         If the directory could not be created.
  */
 public function process($location, ezcTemplateConfiguration $config = null)
 {
     if ($config === null) {
         $config = $this->configuration;
     }
     $this->properties["usedConfiguration"] = $config;
     $this->properties["tstTree"] = false;
     $this->properties["astTree"] = false;
     $this->properties["stream"] = $location;
     if ($location instanceof ezcTemplateLocation || $location instanceof ezcTemplateLocationInterface) {
         $this->properties["file"] = $location;
         $this->properties["stream"] = $location->getPath();
     } elseif ($config->locator) {
         $this->properties["stream"] = $config->locator->translatePath($this->properties["stream"]);
     }
     if (strlen($this->properties["stream"]) > 0 && !ezcBaseFile::isAbsolutepath($this->properties["stream"])) {
         $this->properties["stream"] = $config->templatePath . DIRECTORY_SEPARATOR . $this->properties["stream"];
     }
     $this->properties["streamStack"][] = $this->properties["stream"];
     // lookup compiled code here
     $compiled = ezcTemplateCompiledCode::findCompiled($this->properties["stream"], $config->context, $this);
     $this->properties["compiledTemplatePath"] = $compiled->path;
     $counter = 0;
     while (true) {
         ++$counter;
         if ($counter > 3) {
             // @todo fix exception
             throw new ezcTemplateCompilationFailedException("Failed to create and execute compiled code after " . ($counter - 1) . " tries.");
         }
         if (file_exists($compiled->path) && (!$config->checkModifiedTemplates || filemtime($this->properties["stream"]) <= filemtime($compiled->path))) {
             if (!$config->executeTemplate) {
                 $this->properties["output"] = "";
                 return "";
             }
             try {
                 // execute compiled code here
                 $this->properties["output"] = $compiled->execute();
                 return $this->properties["output"];
             } catch (ezcTemplateOutdatedCompilationException $e) {
                 // The compiled file cannot be used so we need to recompile it
             }
         }
         $this->createDirectory(dirname($compiled->path));
         // get the compiled path.
         // use parser here
         $source = new ezcTemplateSourceCode($this->properties["stream"], $this->properties["stream"]);
         $source->load();
         $parser = new ezcTemplateParser($source, $this);
         $this->properties["tstTree"] = $parser->parseIntoNodeTree();
         if ($parser->hasCacheBlocks && !$config->disableCache) {
             $fetchCacheInfo = new ezcTemplateFetchCacheInformation();
             $this->properties["tstTree"]->accept($fetchCacheInfo);
             $tstToAst = new ezcTemplateTstToAstCachedTransformer($parser, $fetchCacheInfo->cacheTst);
         } else {
             $tstToAst = new ezcTemplateTstToAstTransformer($parser);
         }
         $this->properties["tstTree"]->accept($tstToAst);
         $this->properties["astTree"] = $tstToAst->programNode;
         $astToAst = new ezcTemplateAstToAstContextAppender($config->context);
         $tstToAst->programNode->accept($astToAst);
         // Extra optimization.
         $astToAst = new ezcTemplateAstToAstAssignmentOptimizer();
         $tstToAst->programNode->accept($astToAst);
         $g = new ezcTemplateAstToPhpGenerator($compiled->path, $config);
         // Write to the file.
         $tstToAst->programNode->accept($g);
         // Add to the cache system.
         if ($config->cacheManager) {
             $config->cacheManager->includeTemplate($this, $this->properties["stream"]);
         }
     }
     // execute compiled code here
     throw new ezcTemplateInternalException("Compilation or execution failed");
 }