From de86850bf703bb0c72d1758b96b85fa3ec6842ca Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Fri, 11 Sep 2015 11:13:30 +0200 Subject: [PATCH] Add $template->getSource() --- lib/Twig/Environment.php | 2 +- lib/Twig/Template.php | 27 +++++++++++++++++++++++++++ test/Twig/Tests/EnvironmentTest.php | 4 ++-- test/Twig/Tests/TemplateTest.php | 9 +++++++++ 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index f0f6298a2..c4e1f1853 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -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; diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index e2ab05a24..a854138b6 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -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} */ diff --git a/test/Twig/Tests/EnvironmentTest.php b/test/Twig/Tests/EnvironmentTest.php index 77b29c899..c34903200 100644 --- a/test/Twig/Tests/EnvironmentTest.php +++ b/test/Twig/Tests/EnvironmentTest.php @@ -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 = "\nbar\n"; + $expected = "/* */\n/* bar*/\n/* */\n"; $this->assertContains($expected, $twig->compileSource($source, 'index')); } diff --git a/test/Twig/Tests/TemplateTest.php b/test/Twig/Tests/TemplateTest.php index 31a844bd7..eb27dc380 100644 --- a/test/Twig/Tests/TemplateTest.php +++ b/test/Twig/Tests/TemplateTest.php @@ -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("\n", $template->getSource()); + } + /** * @dataProvider getGetAttributeWithSandbox */ @@ -463,6 +470,8 @@ class Twig_TemplateTest extends Twig_Template } } } +/* */ +/* */ class Twig_TemplateArrayAccessObject implements ArrayAccess {