mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-14 11:27:00 +00:00
Add $template->getSource()
This commit is contained in:
@@ -596,7 +596,7 @@ class Twig_Environment
|
||||
$compiled = $this->compile($this->parse($this->tokenize($source, $name)), $source);
|
||||
|
||||
if (isset($source[0])) {
|
||||
$compiled .= '// '.str_replace(array("\r\n", "\r", "\n"), "\n// ", $source)."\n";
|
||||
$compiled .= '/* '.str_replace(array("\r\n", "\r", "\n"), "*/\n/* ", str_replace('*/', '*//*', $source))."*/\n";
|
||||
}
|
||||
|
||||
return $compiled;
|
||||
|
||||
@@ -304,6 +304,33 @@ abstract class Twig_Template implements Twig_TemplateInterface
|
||||
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}
|
||||
*/
|
||||
|
||||
@@ -142,8 +142,8 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
$twig = new Twig_Environment($this->getMock('Twig_LoaderInterface'));
|
||||
|
||||
$source = "{{ foo }}\n{{ bar }}\n";
|
||||
$expected = "// {{ foo }}\n// {{ bar }}\n// \n";
|
||||
$source = "<? /*foo*/ ?>\nbar\n";
|
||||
$expected = "/* <? /*foo*//* ?>*/\n/* bar*/\n/* */\n";
|
||||
|
||||
$this->assertContains($expected, $twig->compileSource($source, 'index'));
|
||||
}
|
||||
|
||||
@@ -84,6 +84,13 @@ class Twig_Tests_TemplateTest extends PHPUnit_Framework_TestCase
|
||||
return $tests;
|
||||
}
|
||||
|
||||
public function testGetSource()
|
||||
{
|
||||
$template = new Twig_TemplateTest(new Twig_Environment($this->getMock('Twig_LoaderInterface')), false);
|
||||
|
||||
$this->assertSame("<? /*bar*/ ?>\n", $template->getSource());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getGetAttributeWithSandbox
|
||||
*/
|
||||
@@ -463,6 +470,8 @@ class Twig_TemplateTest extends Twig_Template
|
||||
}
|
||||
}
|
||||
}
|
||||
/* <? /*bar*//* ?>*/
|
||||
/* */
|
||||
|
||||
class Twig_TemplateArrayAccessObject implements ArrayAccess
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user