From 85a9d67e93621e320423f7d046faf6ea9ab1b12a Mon Sep 17 00:00:00 2001 From: Jakub Zalas Date: Sun, 6 Mar 2016 18:36:57 +0000 Subject: [PATCH] [FrameworkBundle] Deprecate absolute template paths --- UPGRADE-3.1.md | 6 +++ UPGRADE-4.0.md | 5 +++ .../Bundle/FrameworkBundle/CHANGELOG.md | 1 + .../Templating/TemplateNameParser.php | 10 ++++- .../Templating/TemplateNameParserTest.php | 42 ++++++++++++++++--- 5 files changed, 57 insertions(+), 7 deletions(-) diff --git a/UPGRADE-3.1.md b/UPGRADE-3.1.md index 8c59795bd2..2bf81288c6 100644 --- a/UPGRADE-3.1.md +++ b/UPGRADE-3.1.md @@ -19,6 +19,12 @@ Form in `ResizeFormListener::preSubmit` method has been deprecated and will be removed in Symfony 4.0. +FrameworkBundle +--------------- + + * As it was never an officially supported feature, the support for absolute + template paths has been deprecated and will be removed in Symfony 4.0. + HttpKernel ---------- diff --git a/UPGRADE-4.0.md b/UPGRADE-4.0.md index 1f594037bd..897d130800 100644 --- a/UPGRADE-4.0.md +++ b/UPGRADE-4.0.md @@ -16,6 +16,11 @@ Form * Support for data objects that implements both `Traversable` and `ArrayAccess` in `ResizeFormListener::preSubmit` method has been removed +FrameworkBundle +--------------- + + * Support for absolute template paths has been removed from the template name parser. + HttpKernel ---------- diff --git a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md index 193303ce6e..580ba8f593 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md @@ -5,6 +5,7 @@ CHANGELOG ----- * Added `Controller::json` to simplify creating JSON responses when using the Serializer component + * Deprecated absolute template paths support in the template name parser 3.0.0 ----- diff --git a/src/Symfony/Bundle/FrameworkBundle/Templating/TemplateNameParser.php b/src/Symfony/Bundle/FrameworkBundle/Templating/TemplateNameParser.php index c50541fa27..c16365ff18 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Templating/TemplateNameParser.php +++ b/src/Symfony/Bundle/FrameworkBundle/Templating/TemplateNameParser.php @@ -55,7 +55,7 @@ class TemplateNameParser extends BaseTemplateNameParser throw new \RuntimeException(sprintf('Template name "%s" contains invalid characters.', $name)); } - if (!preg_match('/^(?:([^:]*):([^:]*):)?(.+)\.([^\.]+)\.([^\.]+)$/', $name, $matches) || $this->isAbsolutePath($name) || 0 === strpos($name, '@')) { + if ($this->isAbsolutePath($name) || !preg_match('/^(?:([^:]*):([^:]*):)?(.+)\.([^\.]+)\.([^\.]+)$/', $name, $matches) || 0 === strpos($name, '@')) { return parent::parse($name); } @@ -74,6 +74,12 @@ class TemplateNameParser extends BaseTemplateNameParser private function isAbsolutePath($file) { - return (bool) preg_match('#^(?:/|[a-zA-Z]:)#', $file); + $isAbsolute = (bool) preg_match('#^(?:/|[a-zA-Z]:)#', $file); + + if ($isAbsolute) { + @trigger_error('Absolute template path support is deprecated since Symfony 3.1 and will be removed in 4.0.', E_USER_DEPRECATED); + } + + return $isAbsolute; } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/TemplateNameParserTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/TemplateNameParserTest.php index 479475732c..31453c2780 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/TemplateNameParserTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Templating/TemplateNameParserTest.php @@ -68,11 +68,6 @@ class TemplateNameParserTest extends TestCase array('FooBundle:Post:foo.bar.index.html.php', 'FooBundle:Post:foo.bar.index.html.php', '@FooBundle/Resources/views/Post/foo.bar.index.html.php', new TemplateReference('FooBundle', 'Post', 'foo.bar.index', 'html', 'php')), array('@FooBundle/Resources/views/layout.html.twig', '@FooBundle/Resources/views/layout.html.twig', '@FooBundle/Resources/views/layout.html.twig', new BaseTemplateReference('@FooBundle/Resources/views/layout.html.twig', 'twig')), array('@FooBundle/Foo/layout.html.twig', '@FooBundle/Foo/layout.html.twig', '@FooBundle/Foo/layout.html.twig', new BaseTemplateReference('@FooBundle/Foo/layout.html.twig', 'twig')), - array('/path/to/section/index.html.php', '/path/to/section/index.html.php', '/path/to/section/index.html.php', new BaseTemplateReference('/path/to/section/index.html.php', 'php')), - array('C:\\path\\to\\section\\name.html.php', 'C:path/to/section/name.html.php', 'C:path/to/section/name.html.php', new BaseTemplateReference('C:path/to/section/name.html.php', 'php')), - array('C:\\path\\to\\section\\name:foo.html.php', 'C:path/to/section/name:foo.html.php', 'C:path/to/section/name:foo.html.php', new BaseTemplateReference('C:path/to/section/name:foo.html.php', 'php')), - array('\\path\\to\\section\\name.html.php', '/path/to/section/name.html.php', '/path/to/section/name.html.php', new BaseTemplateReference('/path/to/section/name.html.php', 'php')), - array('/path/to/section/name.php', '/path/to/section/name.php', '/path/to/section/name.php', new BaseTemplateReference('/path/to/section/name.php', 'php')), array('name.twig', 'name.twig', 'name.twig', new BaseTemplateReference('name.twig', 'twig')), array('name', 'name', 'name', new BaseTemplateReference('name')), array('default/index.html.php', '::default/index.html.php', 'views/default/index.html.php', new TemplateReference(null, null, 'default/index', 'html', 'php')), @@ -86,4 +81,41 @@ class TemplateNameParserTest extends TestCase { $this->parser->parse('BarBundle:Post:index.html.php'); } + + /** + * @group legacy + * @dataProvider provideAbsolutePaths + */ + public function testAbsolutePathsAreDeprecated($name, $logicalName, $path, $ref) + { + $deprecations = array(); + set_error_handler(function ($type, $msg) use (&$deprecations) { + if (E_USER_DEPRECATED !== $type) { + throw new \LogicException(sprintf('Unexpected error: "%s".', $msg)); + } + + $deprecations[] = $msg; + }); + + $template = $this->parser->parse($name); + + restore_error_handler(); + + $this->assertSame($ref->getLogicalName(), $template->getLogicalName()); + $this->assertSame($logicalName, $template->getLogicalName()); + $this->assertSame($path, $template->getPath()); + $this->assertCount(1, $deprecations); + $this->assertContains('Absolute template path support is deprecated since Symfony 3.1 and will be removed in 4.0.', $deprecations[0]); + } + + public function provideAbsolutePaths() + { + return array( + array('/path/to/section/index.html.php', '/path/to/section/index.html.php', '/path/to/section/index.html.php', new BaseTemplateReference('/path/to/section/index.html.php', 'php')), + array('C:\\path\\to\\section\\name.html.php', 'C:path/to/section/name.html.php', 'C:path/to/section/name.html.php', new BaseTemplateReference('C:path/to/section/name.html.php', 'php')), + array('C:\\path\\to\\section\\name:foo.html.php', 'C:path/to/section/name:foo.html.php', 'C:path/to/section/name:foo.html.php', new BaseTemplateReference('C:path/to/section/name:foo.html.php', 'php')), + array('\\path\\to\\section\\name.html.php', '/path/to/section/name.html.php', '/path/to/section/name.html.php', new BaseTemplateReference('/path/to/section/name.html.php', 'php')), + array('/path/to/section/name.php', '/path/to/section/name.php', '/path/to/section/name.php', new BaseTemplateReference('/path/to/section/name.php', 'php')), + ); + } }