8000 [TwigBundle] Deprecating support for legacy templates directories by yceruto · Pull Request #28891 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[TwigBundle] Deprecating support for legacy templates directories #28891

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions UPGRADE-4.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ TwigBundle
----------

* The `transchoice` tag and filter have been deprecated, use the `trans` ones instead with a `%count%` parameter.
* Deprecated support for legacy templates directories `src/Resources/views/` and `src/Resources/<BundleName>/views/`, use `templates/` and `templates/bundles/<BundleName>/` instead.

Validator
---------
Expand Down
1 change: 1 addition & 0 deletions UPGRADE-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ TwigBundle

* The default value (`false`) of the `twig.strict_variables` configuration option has been changed to `%kernel.debug%`.
* The `transchoice` tag and filter have been removed, use the `trans` ones instead with a `%count%` parameter.
* Removed support for legacy templates directories `src/Resources/views/` and `src/Resources/<BundleName>/views/`, use `templates/` and `templates/bundles/<BundleName>/` instead.

Validator
--------
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Bundle/SecurityBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"symfony/framework-bundle": "~4.2",
"symfony/http-foundation": "~3.4|~4.0",
"symfony/translation": "~3.4|~4.0",
"symfony/twig-bundle": "~3.4|~4.0",
"symfony/twig-bundle": "~4.2",
"symfony/twig-bridge": "~3.4|~4.0",
"symfony/process": "~3.4|~4.0",
"symfony/validator": "~3.4|~4.0",
Expand All @@ -49,6 +49,7 @@
},
"conflict": {
"symfony/browser-kit": "<4.2",
"symfony/twig-bundle": "<4.2",
"symfony/var-dumper": "<3.4",
"symfony/event-dispatcher": "<3.4",
"symfony/framework-bundle": "<4.2",
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Bundle/TwigBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

4.2.0
-----

* deprecated support for legacy templates directories `src/Resources/views/` and `src/Resources/<BundleName>/views/`, use `templates/` and `templates/bundles/<BundleName>/` inst EDBE ead.

4.1.0
-----

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public function load(array $configs, ContainerBuilder $container)

$container->setParameter('twig.form.resources', $config['form_themes']);
$container->setParameter('twig.default_path', $config['default_path']);
$defaultTwigPath = $container->getParameterBag()->resolveValue($config['default_path']);

$envConfiguratorDefinition = $container->getDefinition('twig.configurator.environment');
$envConfiguratorDefinition->replaceArgument(0, $config['date']['format']);
Expand Down Expand Up @@ -115,14 +116,18 @@ public function load(array $configs, ContainerBuilder $container)
}

if (file_exists($dir = $container->getParameter('kernel.root_dir').'/Resources/views')) {
if ($dir !== $defaultTwigPath) {
@trigger_error(sprintf('Templates directory "%s" is deprecated since Symfony 4.2, use "%s" instead.', $dir, $defaultTwigPath), E_USER_DEPRECATED);
}

$twigFilesystemLoaderDefinition->addMethodCall('addPath', array($dir));
}
$container->addResource(new FileExistenceResource($dir));

if (file_exists($dir = $container->getParameterBag()->resolveValue($config['default_path']))) {
$twigFilesystemLoaderDefinition->addMethodCall('addPath', array($dir));
if (file_exists($defaultTwigPath)) {
$twigFilesystemLoaderDefinition->addMethodCall('addPath', array($defaultTwigPath));
}
$container->addResource(new FileExistenceResource($dir));
$container->addResource(new FileExistenceResource($defaultTwigPath));

if (!empty($config['globals'])) {
$def = $container->getDefinition('twig');
Expand Down Expand Up @@ -164,15 +169,19 @@ private function getBundleTemplatePaths(ContainerBuilder $container, array $conf
{
$bundleHierarchy = array();
foreach ($container->getParameter('kernel.bundles_metadata') as $name => $bundle) {
$defaultOverrideBundlePath = $container->getParameterBag()->resolveValue($config['default_path']).'/bundles/'.$name;

if (file_exists($dir = $container->getParameter('kernel.root_dir').'/Resources/'.$name.'/views')) {
@trigger_error(sprintf('Templates directory "%s" is deprecated since Symfony 4.2, use "%s" instead.', $dir, $defaultOverrideBundlePath), E_USER_DEPRECATED);

$bundleHierarchy[$name][] = $dir;
}
$container->addResource(new FileExistenceResource($dir));

if (file_exists($dir = $container->getParameterBag()->resolveValue($config['default_path']).'/bundles/'.$name)) {
$bundleHierarchy[$name][] = $dir;
if (file_exists($defaultOverrideBundlePath)) {
$bundleHierarchy[$name][] = $defaultOverrideBundlePath;
}
$container->addResource(new FileExistenceResource($dir));
$container->addResource(new FileExistenceResource($defaultOverrideBundlePath));

if (file_exists($dir = $bundle['path'].'/Resources/views')) {
$bundleHierarchy[$name][] = $dir;
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,46 @@ public function testTwigLoaderPaths($format)
array('namespaced_path1', 'namespace1'),
array('namespaced_path2', 'namespace2'),
array('namespaced_path3', 'namespace3'),
array(__DIR__.'/Fixtures/Resources/TwigBundle/views', 'Twig'),
array(__DIR__.'/Fixtures/templates/bundles/TwigBundle', 'Twig'),
array(realpath(__DIR__.'/../..').'/Resources/views', 'Twig'),
array(realpath(__DIR__.'/../..').'/Resources/views', '!Twig'),
array(__DIR__.'/Fixtures/Resources/views'),
array(__DIR__.'/Fixtures/templates'),
), $paths);
}

/**
* @group legacy
* @dataProvider getFormats
* @expectedDeprecation Templates directory "%s/Resources/TwigBundle/views" is deprecated since Symfony 4.2, use "%s/templates/bundles/TwigBundle" instead.
* @expectedDeprecation Templates directory "%s/Resources/views" is deprecated since Symfony 4.2, use "%s/templates" instead.
*/
public function testLegacyTwigLoaderPaths($format)
{
$container = $this->createContainer(__DIR__.'/../Fixtures/templates');
$container->registerExtension(new TwigExtension());
$this->loadFromFile($container, 'full', $format);
$this->loadFromFile($container, 'extra', $format);
$this->compileContainer($container);

$def = $container->getDefinition('twig.loader.native_filesystem');
$paths = array();
foreach ($def->getMethodCalls() as $call) {
if ('addPath' === $call[0] && false === strpos($call[1][0], 'Form')) {
$paths[] = $call[1];
}
}

$this->assertEquals(array(
array('path1'),
array('path2'),
array('namespaced_path1', 'namespace1'),
array('namespaced_path2', 'namespace2'),
array('namespaced_path3', 'namespace3'),
array(__DIR__.'/../Fixtures/templates/Resources/TwigBundle/views', 'Twig'),
array(__DIR__.'/Fixtures/templates/bundles/TwigBundle', 'Twig'),
array(realpath(__DIR__.'/../..').'/Resources/views', 'Twig'),
array(realpath(__DIR__.'/../..').'/Resources/views', '!Twig'),
array(__DIR__.'/../Fixtures/templates/Resources/views'),
array(__DIR__.'/Fixtures/templates'),
), $paths);
}
Expand Down Expand Up @@ -271,11 +306,11 @@ public function testRuntimeLoader()
$this->assertEquals('foo', $args['FooClass']->getValues()[0]);
}

private function createContainer()
private function createContainer(string $rootDir = __DIR__.'/Fixtures')
{
$container = new ContainerBuilder(new ParameterBag(array(
'kernel.cache_dir' => __DIR__,
'kernel.root_dir' => __DIR__.'/Fixtures',
'kernel.root_dir' => $rootDir,
'kernel.project_dir' => __DIR__,
'kernel.charset' => 'UTF-8',
'kernel.debug' => false,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{# Twig template #}
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ public function registerContainerConfiguration(LoaderInterface $loader)
'secret' => '$ecret',
'form' => array('enabled' => false),
))
->loadFromExtension('twig', array( // to be removed in 5.0 relying on default
'strict_variables' => false,
->loadFromExtension('twig', array(
'strict_variables' => false, // to be removed in 5.0 relying on default
'default_path' => __DIR__.'/templates',
))
;
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public function testGetSourceContext()
$locator
->expects($this->once())
->method('locate')
->will($this->returnValue(__DIR__.'/../DependencyInjection/Fixtures/Resources/views/layout.html.twig'))
->will($this->returnValue(__DIR__.'/../DependencyInjection/Fixtures/templates/layout.html.twig'))
;
$loader = new FilesystemLoader($locator, $parser);
$loader->addPath(__DIR__.'/../DependencyInjection/Fixtures/Resources/views', 'namespace');
$loader->addPath(__DIR__.'/../DependencyInjection/Fixtures/templates', 'namespace');

// Twig-style
$this->assertEquals("This is a layout\n", $loader->getSourceContext('@namespace/layout.html.twig')->getCode());
Expand All @@ -44,7 +44,7 @@ public function testExists()
$locator
->expects($this->once())
->method( A7B2 9;locate')
->will($this->returnValue($template = __DIR__.'/../DependencyInjection/Fixtures/Resources/views/layout.html.twig'))
->will($this->returnValue($template = __DIR__.'/../DependencyInjection/Fixtures/templates/layout.html.twig'))
;
$loader = new FilesystemLoader($locator, $parser);

Expand Down Expand Up @@ -101,15 +101,15 @@ public function testTwigErrorIfLocatorReturnsFalse()

/**
* @expectedException \Twig\Error\LoaderError
* @expectedExceptionMessageRegExp /Unable to find template "name\.format\.engine" \(looked into: .*Tests.Loader.\.\..DependencyInjection.Fixtures.Resources.views\)/
* @expectedExceptionMessageRegExp /Unable to find template "name\.format\.engine" \(looked into: .*Tests.Loader.\.\..DependencyInjection.Fixtures.templates\)/
*/
public function testTwigErrorIfTemplateDoesNotExist()
{
$parser = $this->getMockBuilder('Symfony\Component\Templating\TemplateNameParserInterface')->getMock();
$locator = $this->getMockBuilder('Symfony\Component\Config\FileLocatorInterface')->getMock();

$loader = new FilesystemLoader($locator, $parser);
$loader->addPath(__DIR__.'/../DependencyInjection/Fixtures/Resources/views');
$loader->addPath(__DIR__.'/../DependencyInjection/Fixtures/templates');

$method = new \ReflectionMethod('Symfony\Bundle\TwigBundle\Loader\FilesystemLoader', 'findTemplate');
$method->setAccessible(true);
Expand All @@ -122,7 +122,7 @@ public function testTwigSoftErrorIfTemplateDoesNotExist()
$locator = $this->getMockBuilder('Symfony\Component\Config\FileLocatorInterface')->getMock();

$loader = new FilesystemLoader($locator, $parser);
$loader->addPath(__DIR__.'/../DependencyInjection/Fixtures/Resources/views');
$loader->addPath(__DIR__.'/../DependencyInjection/Fixtures/templates');

$method = new \ReflectionMethod('Symfony\Bundle\TwigBundle\Loader\FilesystemLoader', 'findTemplate');
$method->setAccessible(true);
Expand Down
0