Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feature #18034 [FrameworkBundle] Deprecate absolute template paths (j…
…akzal)

This PR was merged into the 3.1-dev branch.

Discussion
----------

[FrameworkBundle] Deprecate absolute template paths

| Q             | A
| ------------- | ---
| Branch        | master
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | yes
| Tests pass?   | yes
| Fixed tickets | #17999
| License       | MIT
| Doc PR        | -

Commits
-------

85a9d67 [FrameworkBundle] Deprecate absolute template paths
  • Loading branch information
fabpot committed Mar 15, 2016
2 parents 98ba4b7 + 85a9d67 commit a8591fb
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 7 deletions.
6 changes: 6 additions & 0 deletions UPGRADE-3.1.md
Expand Up @@ -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
----------

Expand Down
5 changes: 5 additions & 0 deletions UPGRADE-4.0.md
Expand Up @@ -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
----------

Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Expand Up @@ -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
-----
Expand Down
Expand Up @@ -55,7 +55,7 @@ public function parse($name)
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);
}

Expand All @@ -74,6 +74,12 @@ public function parse($name)

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;
}
}
Expand Up @@ -68,11 +68,6 @@ public function parseProvider()
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')),
Expand All @@ -86,4 +81,41 @@ public function testParseValidNameWithNotFoundBundle()
{
$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')),
);
}
}

0 comments on commit a8591fb

Please sign in to comment.