function testTreeGeneration()
    {
        $tpl = $this->newPHPTAL();
        $parser = new PHPTAL_Dom_SaxXmlParser($tpl->getEncoding());
        $treeGen = $parser->parseFile(new PHPTAL_Dom_PHPTALDocumentBuilder(), 'input/parser.01.xml')->getResult();
        $state = new PHPTAL_Php_State($tpl);
        $codewriter = new PHPTAL_Php_CodeWriter($state);
        $codewriter->doFunction('test', '$tpl');
        $treeGen->generateCode($codewriter);
        $codewriter->doEnd('function');
        $result = $codewriter->getResult();
        $expected = <<<EOS
<?php
function test(\$tpl) {
\$ctx->setXmlDeclaration('<?xml version="1.0"?>',false) ;?>
<html>
  <head>
    <title>test document</title>
  </head>
  <body>
    <h1>test document</h1>
    <a href="http://phptal.sf.net">phptal</a>
  </body>
</html><?php
}

 ?>
EOS;
        $result = normalize_phpsource($result, true);
        $expected = normalize_phpsource($expected, true);
        $this->assertEquals($expected, $result);
    }
Beispiel #2
0
 public function testIllegalElementNames2()
 {
     $parser = new PHPTAL_Dom_SaxXmlParser('UTF-8');
     try {
         $parser->parseString(new PHPTAL_Dom_PHPTALDocumentBuilder(), '<t><element~ /></t>');
         $this->fail("Accepted invalid element name")->getResult();
     } catch (PHPTAL_Exception $e) {
     }
 }
Beispiel #3
0
 public function testNotClosing()
 {
     $parser = new PHPTAL_Dom_SaxXmlParser('UTF-8');
     try {
         $parser->parseString(new PHPTAL_Dom_PHPTALDocumentBuilder(), "<element_a><element_b><element_x/><element_c><element_d><element_e>");
         $this->fail("Accepted invalid XML");
     } catch (PHPTAL_ParserException $e) {
         $this->assertNotContains("documentElement", $e->getMessage());
         $this->assertRegExp("/element_e.*element_d.*element_c.*element_b.*element_a/", $e->getMessage());
     }
 }
Beispiel #4
0
 /**
  * Parse currently set template, prefilter and generate PHP code.
  *
  * @return string (compiled PHP code)
  */
 protected function parse()
 {
     $data = $this->_source->getData();
     $prefilters = $this->getPreFilterInstances();
     foreach ($prefilters as $prefilter) {
         $data = $prefilter->filter($data);
     }
     $realpath = $this->_source->getRealPath();
     $parser = new PHPTAL_Dom_SaxXmlParser($this->_encoding);
     $builder = new PHPTAL_Dom_PHPTALDocumentBuilder();
     $tree = $parser->parseString($builder, $data, $realpath)->getResult();
     foreach ($prefilters as $prefilter) {
         if ($prefilter instanceof PHPTAL_PreFilter) {
             if ($prefilter->filterDOM($tree) !== NULL) {
                 throw new PHPTAL_ConfigurationException("Don't return value from filterDOM()");
             }
         }
     }
     $state = new PHPTAL_Php_State($this);
     $codewriter = new PHPTAL_Php_CodeWriter($state);
     $codewriter->doTemplateFile($this->getFunctionName(), $tree);
     return $codewriter->getResult();
 }
Beispiel #5
0
 /**
  * parse currently set template
  * @return string (compiled PHP code)
  */
 protected function parse()
 {
     self::setIncludePath();
     require_once 'PHPTAL/Dom/DocumentBuilder.php';
     require_once 'PHPTAL/Php/CodeGenerator.php';
     self::restoreIncludePath();
     // instantiate the PHPTAL source parser
     $parser = new PHPTAL_Dom_SaxXmlParser($this->_encoding);
     $builder = new PHPTAL_Dom_DocumentBuilder();
     $builder->stripComments($this->_stripComments);
     $data = $this->_source->getData();
     $realpath = $this->_source->getRealPath();
     if ($this->_prefilter) {
         $data = $this->_prefilter->filter($data);
     }
     $tree = $parser->parseString($builder, $data, $realpath)->getResult();
     $generator = new PHPTAL_Php_CodeGenerator($this->getFunctionName(), $this->_source->getRealPath(), $this->_encoding, $this->_outputMode, $this->getCodePath());
     $result = $generator->generateCode($tree);
     return $result;
 }
Beispiel #6
0
 /**
  * Parse currently set template, prefilter and generate PHP code.
  * 
  * @return string (compiled PHP code)
  */
 protected function parse()
 {
     self::setIncludePath();
     require_once 'PHPTAL/Dom/DocumentBuilder.php';
     require_once 'PHPTAL/Php/State.php';
     require_once 'PHPTAL/Php/CodeWriter.php';
     self::restoreIncludePath();
     // instantiate the PHPTAL source parser
     $data = $this->_source->getData();
     $prefilters = $this->getPreFilterInstances();
     foreach ($prefilters as $prefilter) {
         $data = $prefilter->filter($data);
     }
     $parser = new PHPTAL_Dom_SaxXmlParser($this->_encoding);
     $builder = new PHPTAL_Dom_DocumentBuilder();
     $realpath = $this->_source->getRealPath();
     $tree = $parser->parseString($builder, $data, $realpath)->getResult();
     foreach ($prefilters as $prefilter) {
         if ($prefilter instanceof PHPTAL_PreFilter) {
             if ($prefilter->filterDOM($tree)) {
                 throw new PHPTAL_ConfigurationException("Don't return value from filterDOM()");
             }
         }
     }
     $state = new PHPTAL_Php_State($this);
     $codewriter = new PHPTAL_Php_CodeWriter($state);
     $codewriter->doTemplateFile($this->getFunctionName(), $tree);
     return $codewriter->getResult();
 }