Example #1
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");
 }
 public function testExecute()
 {
     $conf = new ezcTemplateCompiledCode('14862b79ceaf01443626bd5d564c53e2', $this->templateCompiledPath . 'full-14862b79ceaf01443626bd5d564c53e2.php');
     $conf->template = new ezcTemplate();
     $conf->context = new ezcTemplateXhtmlContext();
     $conf->execute();
 }