Skip to content
Permalink
Browse files

bug #2129 changed the way we store template source in template classe…

…s (fabpot)

This PR was merged into the 1.x branch.

Discussion
----------

changed the way we store template source in template classes

In #1807 and #1813, we added a way to get the original template source code from a template. This PR greatly simplifies the approach by removing all the black magic :)

This should also fix #2011

ping @nicolas-grekas

Commits
-------

b55d907 changed the way we store template source in template classes
  • Loading branch information...
fabpot committed Sep 20, 2016
2 parents 746a804 + b55d907 commit 2486ea66999b057ae6ccacbadc1aa6f8b0347830
@@ -1,5 +1,6 @@
* 1.25.0 (2016-XX-XX)

* changed the way we store template source in template classes
* removed usage of realpath in cache keys
* fixed Twig cache sharing when used with different versions of PHP
* removed embed parent workaround for simple use cases
@@ -693,13 +693,7 @@ public function compile(Twig_NodeInterface $node)
public function compileSource($source, $name = null)
{
try {
$compiled = $this->compile($this->parse($this->tokenize($source, $name)));
if (isset($source[0])) {
$compiled .= '/* '.str_replace(array('*/', "\r\n", "\r", "\n"), array('*//* ', "\n", "\n", "*/\n/* "), $source)."*/\n";
}
return $compiled;
return $this->compile($this->parse($this->tokenize($source, $name)));
} catch (Twig_Error $e) {
$e->setTemplateFile($name);
throw $e;
@@ -136,7 +136,7 @@ public function tokenize($code, $filename = null)
mb_internal_encoding($mbEncoding);
}
return new Twig_TokenStream($this->tokens, $this->filename);
return new Twig_TokenStream($this->tokens, $this->filename, $code);
}
protected function lexData()
@@ -21,7 +21,7 @@
*/
class Twig_Node_Module extends Twig_Node
{
public function __construct(Twig_NodeInterface $body, Twig_Node_Expression $parent = null, Twig_NodeInterface $blocks, Twig_NodeInterface $macros, Twig_NodeInterface $traits, $embeddedTemplates, $filename)
public function __construct(Twig_NodeInterface $body, Twig_Node_Expression $parent = null, Twig_NodeInterface $blocks, Twig_NodeInterface $macros, Twig_NodeInterface $traits, $embeddedTemplates, $filename, $source = '')
{
$nodes = array(
'body' => $body,
@@ -40,6 +40,7 @@ public function __construct(Twig_NodeInterface $body, Twig_Node_Expression $pare
// embedded templates are set as attributes so that they are only visited once by the visitors
parent::__construct($nodes, array(
'source' => $source,
'filename' => $filename,
'index' => null,
'embedded_templates' => $embeddedTemplates,
@@ -93,6 +94,8 @@ protected function compileTemplate(Twig_Compiler $compiler)
$this->compileDebugInfo($compiler);
$this->compileGetSource($compiler);
$this->compileClassFooter($compiler);
}
@@ -386,6 +389,19 @@ protected function compileDebugInfo(Twig_Compiler $compiler)
->indent()
->write(sprintf("return %s;\n", str_replace("\n", '', var_export(array_reverse($compiler->getDebugInfo(), true), true))))
->outdent()
->write("}\n\n")
;
}
protected function compileGetSource(Twig_Compiler $compiler)
{
$compiler
->write("public function getSource()\n", "{\n")
->indent()
->write('return ')
->string($this->getAttribute('source'))
->raw(";\n")
->outdent()
->write("}\n")
;
}
@@ -114,7 +114,7 @@ public function parse(Twig_TokenStream $stream, $test = null, $dropNeedle = fals
throw $e;
}
$node = new Twig_Node_Module(new Twig_Node_Body(array($body)), $this->parent, new Twig_Node($this->blocks), new Twig_Node($this->macros), new Twig_Node($this->traits), $this->embeddedTemplates, $this->getFilename());
$node = new Twig_Node_Module(new Twig_Node_Body(array($body)), $this->parent, new Twig_Node($this->blocks), new Twig_Node($this->macros), new Twig_Node($this->traits), $this->embeddedTemplates, $this->getFilename(), $stream->getSource());
$traverser = new Twig_NodeTraverser($this->env, $this->visitors);
@@ -51,6 +51,13 @@ public function __construct(Twig_Environment $env)
*/
abstract public function getDebugInfo();
/**
* Returns the template source code.
*
* @return string The template source code
*/
abstract public function getSource();
/**
* @deprecated since 1.20 (to be removed in 2.0)
*/
@@ -329,33 +336,6 @@ public function getBlocks()
return $this->blocks;
}
/**
* Returns the template source code.
*
* @return string|null The template source code or null if it is not available
*/
public function getSource()
{
$reflector = new ReflectionClass($this);
$file = $reflector->getFileName();
if (!file_exists($file)) {
return;
}
$source = file($file, FILE_IGNORE_NEW_LINES);
array_splice($source, 0, $reflector->getEndLine());
$i = 0;
while (isset($source[$i]) && '/* */' === substr_replace($source[$i], '', 3, -2)) {
$source[$i] = str_replace('*//* ', '*/', substr($source[$i], 3, -2));
++$i;
}
array_splice($source, $i);
return implode("\n", $source);
}
/**
* {@inheritdoc}
*/
@@ -21,16 +21,20 @@ class Twig_TokenStream
protected $current = 0;
protected $filename;
private $source;
/**
* Constructor.
*
* @param array $tokens An array of tokens
* @param string $filename|null The name of the filename which tokens are associated with
* @param string $source|null The source code associated with the tokens
*/
public function __construct(array $tokens, $filename = null)
public function __construct(array $tokens, $filename = null, $source = null)
{
$this->tokens = $tokens;
$this->filename = $filename;
$this->source = $source ? $source : '';
}
/**
@@ -152,4 +156,14 @@ public function getFilename()
{
return $this->filename;
}
/**
* Gets the source code associated with this stream.
*
* @return string
*/
public function getSource()
{
return $this->source;
}
}
@@ -143,18 +143,6 @@ public function testGlobals()
*/
}
public function testCompileSourceInlinesSource()
{
$twig = new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock());
$source = "<? */*foo*/ ?>\r\nbar\n";
$expected = "/* <? *//* *foo*//* ?>*/\n/* bar*/\n/* */\n";
$compiled = $twig->compileSource($source, 'index');
$this->assertContains($expected, $compiled);
$this->assertNotContains('/**', $compiled);
}
public function testExtensionsAreNotInitializedWhenRenderingACompiledTemplate()
{
$cache = new Twig_Cache_Filesystem($dir = sys_get_temp_dir().'/twig');
@@ -73,6 +73,11 @@ public function getDebugInfo()
{
return array ( 19 => 1,);
}
public function getSource()
{
return "";
}
}
EOF
, $twig, true);
@@ -126,6 +131,11 @@ public function getDebugInfo()
{
return array ( 26 => 1, 24 => 2, 11 => 1,);
}
public function getSource()
{
return "";
}
}
EOF
, $twig, true);
@@ -139,7 +149,7 @@ public function getDebugInfo()
2
);
$node = new Twig_Node_Module($body, $extends, $blocks, $macros, $traits, new Twig_Node(array()), $filename);
$node = new Twig_Node_Module($body, $extends, $blocks, $macros, $traits, new Twig_Node(array()), $filename, '{{ foo }}');
$tests[] = array($node, <<<EOF
<?php
@@ -174,6 +184,11 @@ public function getDebugInfo()
{
return array ( 17 => 2, 15 => 4, 9 => 2,);
}
public function getSource()
{
return "{{ foo }}";
}
}
EOF
, $twig, true);
@@ -84,13 +84,6 @@ public function getAttributeExceptions()
return $tests;
}
public function testGetSource()
{
$template = new Twig_TemplateTest(new Twig_Environment($this->getMockBuilder('Twig_LoaderInterface')->getMock()), false);
$this->assertSame("<? */*bar*/ ?>\n", $template->getSource());
}
/**
* @dataProvider getGetAttributeWithSandbox
*/
@@ -453,6 +446,11 @@ public function getDebugInfo()
return array();
}
public function getSource()
{
return '';
}
protected function doGetParent(array $context)
{
}
@@ -470,8 +468,6 @@ public function getAttribute($object, $item, array $arguments = array(), $type =
}
}
}
/* <? *//* *bar*//* ?>*/
/* */
class Twig_TemplateArrayAccessObject implements ArrayAccess
{

0 comments on commit 2486ea6

Please sign in to comment.
You can’t perform that action at this time.