Пример #1
0
 function testExtracterWithQuotes()
 {
     $file = dirname(__FILE__) . '/test_files/test-quotes.ezt';
     $source = new ezcTemplateSourceCode($file, $file);
     $source->load();
     $parser = new ezcTemplateParser($source, new ezcTemplate());
     $tst = $parser->parseIntoNodeTree();
     $et = new ezcTemplateTranslationStringExtracter($parser);
     $eted = $tst->accept($et);
     $tr = $et->getStrings();
     self::assertEquals(array('Test quotes: \'test\'.', 'Test quotes: "test".', 'Test quotes: \'test\' "test".', 'Test quotes: "test" \'test\'.'), array_keys($tr['un']));
 }
Пример #2
0
 /**
  * Creates the TST tree structure from the source code.
  *
  * @return void
  */
 public function parseIntoNodeTree()
 {
     if (!$this->source->hasCode()) {
         throw new ezcTemplateException(ezcTemplateSourceToTstErrorMessages::MSG_NO_SOURCE_CODE);
     }
     $sourceText = $this->source->code;
     $cursor = new ezcTemplateCursor($sourceText);
     $this->textElements = array();
     $parser = new ezcTemplateProgramSourceToTstParser($this, null, null);
     $parser->setAllCursors($cursor);
     if (!$parser->parse()) {
         $currentParser = $parser->getFailingParser();
     }
     // Trim starting/trailing whitespace
     if ($this->trimWhitespace) {
         $this->whitespaceRemoval->trimProgram($parser->program);
     }
     return $parser->program;
 }
Пример #3
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");
 }
Пример #4
0
 /**
  * Test deleting a temporary template file
  */
 public function testDelete()
 {
     copy($this->templatePath . "zhadum.ezt", $this->templateStorePath . "zhadum.ezt");
     $src = new ezcTemplateSourceCode($this->templateStorePath . "zhadum.ezt", "planet:zhadum.ezt", "The abyss.");
     self::assertTrue(file_exists($this->templateStorePath . "zhadum.ezt"), 'Temporary template file <' . $this->templateStorePath . 'zhadum.ezt> does not exists, copy failed.');
     $src->delete();
     self::assertSame($this->templateStorePath . "zhadum.ezt", $src->stream, 'Property <stream> does not return correct value.');
     self::assertSame("planet:zhadum.ezt", $src->resource, 'Property <resource> does not return correct value.');
     self::assertSame("The abyss.", $src->code, 'Property <code> does not return correct value.');
     self::assertSame(false, file_exists($this->templateStorePath . "zhadum.ezt"), 'Temporary template file <' . $this->templateStorePath . 'zhadum.ezt> still exist, was supposed to be unlinked.');
     self::assertSame(false, $src->isAvailable(), 'Temporary template file <' . $this->templateStorePath . 'zhadum.ezt> does not return false in isAvailable().');
     //        self::assertSame( null, $src->context, 'Property <context> does not return correct value.' );
 }
Пример #5
0
 /**
  * Get translation array with translation data for each context from the
  * template.
  *
  * @param string $filename
  * @return array(string=>array(ezcTranslationData))
  */
 function getTranslationsFromTemplate($filename)
 {
     $source = new ezcTemplateSourceCode($filename, $filename);
     $source->load();
     $parser = new ezcTemplateParser($source, new ezcTemplate());
     $tst = $parser->parseIntoNodeTree();
     $et = new ezcTemplateTranslationStringExtracter($parser);
     $eted = $tst->accept($et);
     return $et->getStrings();
 }