Exemplo n.º 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']));
 }
Exemplo 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");
 }
Exemplo n.º 3
0
 /**
  * Test loading code from template file which does not exist.
  * In this case an exception is expected to be thrown.
  */
 public function testLoadNonReadable()
 {
     // If running as root you can always write, so this test should be
     // skipped when running as root.
     if (!ezcBaseFeatures::hasFunction("posix_getuid") || posix_getuid() == 0) {
         return;
     }
     copy($this->templatePath . "zhadum.ezt", $this->templateStorePath . "zhadum.ezt");
     // This only works on Linux/Unix, what to do here on other platforms?
     $old = umask(0);
     chmod($this->templateStorePath . "zhadum.ezt", 0222);
     umask($old);
     $src = new ezcTemplateSourceCode($this->templateStorePath . "zhadum.ezt", "planet:zhadum.ezt", "definitely not the source from the file zhadum.ezt");
     try {
         $src->load();
         self::fail("No exception thrown for non-readable file");
     } catch (ezcTemplateFileNotReadableException $e) {
     }
     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("definitely not the source from the file zhadum.ezt", $src->code, 'Property <code> does not return correct value.');
     //        self::assertSame( null, $src->context, 'Property <context> does not return correct value.' );
 }
Exemplo n.º 4
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();
 }