From 8b390f346f7d98dcad723dea5120534ed9fdc4c8 Mon Sep 17 00:00:00 2001 From: Yonel Ceruto Date: Tue, 16 Oct 2018 10:49:09 -0400 Subject: [PATCH] Deprecating support for legacy templates directories --- UPGRADE-4.2.md | 1 + UPGRADE-5.0.md | 1 + .../views => templates}/base.html.twig | 0 .../Bundle/SecurityBundle/composer.json | 3 +- src/Symfony/Bundle/TwigBundle/CHANGELOG.md | 5 +++ .../DependencyInjection/TwigExtension.php | 21 ++++++--- .../TwigBundle/views/layout.html.twig | 1 - .../Fixtures/Resources/views/layout.html.twig | 1 - .../DependencyInjection/TwigExtensionTest.php | 43 +++++++++++++++++-- .../TwigBundle/views/layout.html.twig | 1 + .../Functional/NoTemplatingEntryTest.php | 5 ++- .../views => templates}/index.html.twig | 0 .../Tests/Loader/FilesystemLoaderTest.php | 12 +++--- 13 files changed, 73 insertions(+), 21 deletions(-) rename src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/{Resources/views => templates}/base.html.twig (100%) delete mode 100644 src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/Resources/TwigBundle/views/layout.html.twig delete mode 100644 src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/Resources/views/layout.html.twig create mode 100644 src/Symfony/Bundle/TwigBundle/Tests/Fixtures/templates/Resources/TwigBundle/views/layout.html.twig rename src/Symfony/Bundle/TwigBundle/Tests/Functional/{Resources/views => templates}/index.html.twig (100%) diff --git a/UPGRADE-4.2.md b/UPGRADE-4.2.md index 9643f91085b80..4eebf8668c5de 100644 --- a/UPGRADE-4.2.md +++ b/UPGRADE-4.2.md @@ -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//views/`, use `templates/` and `templates/bundles//` instead. Validator --------- diff --git a/UPGRADE-5.0.md b/UPGRADE-5.0.md index db0fb3fa34a34..1993a37552d21 100644 --- a/UPGRADE-5.0.md +++ b/UPGRADE-5.0.md @@ -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//views/`, use `templates/` and `templates/bundles//` instead. Validator -------- diff --git a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/Resources/views/base.html.twig b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/templates/base.html.twig similarity index 100% rename from src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/Resources/views/base.html.twig rename to src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/templates/base.html.twig diff --git a/src/Symfony/Bundle/SecurityBundle/composer.json b/src/Symfony/Bundle/SecurityBundle/composer.json index 12efa8a97b46a..5a03f8f7fa846 100644 --- a/src/Symfony/Bundle/SecurityBundle/composer.json +++ b/src/Symfony/Bundle/SecurityBundle/composer.json @@ -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", @@ -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", diff --git a/src/Symfony/Bundle/TwigBundle/CHANGELOG.md b/src/Symfony/Bundle/TwigBundle/CHANGELOG.md index 78b289092ee70..1be455e4e8288 100644 --- a/src/Symfony/Bundle/TwigBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/TwigBundle/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +4.2.0 +----- + + * deprecated support for legacy templates directories `src/Resources/views/` and `src/Resources//views/`, use `templates/` and `templates/bundles//` instead. + 4.1.0 ----- diff --git a/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php b/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php index 404189c67c191..fbe05fd81845c 100644 --- a/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php +++ b/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php @@ -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']); @@ -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'); @@ -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; diff --git a/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/Resources/TwigBundle/views/layout.html.twig b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/Resources/TwigBundle/views/layout.html.twig deleted file mode 100644 index bb07ecfe55a36..0000000000000 --- a/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/Resources/TwigBundle/views/layout.html.twig +++ /dev/null @@ -1 +0,0 @@ -This is a layout diff --git a/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/Resources/views/layout.html.twig b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/Resources/views/layout.html.twig deleted file mode 100644 index bb07ecfe55a36..0000000000000 --- a/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/Resources/views/layout.html.twig +++ /dev/null @@ -1 +0,0 @@ -This is a layout diff --git a/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php index 1039363a3bbd6..3666ef6d339ae 100644 --- a/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php +++ b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php @@ -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); } @@ -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, diff --git a/src/Symfony/Bundle/TwigBundle/Tests/Fixtures/templates/Resources/TwigBundle/views/layout.html.twig b/src/Symfony/Bundle/TwigBundle/Tests/Fixtures/templates/Resources/TwigBundle/views/layout.html.twig new file mode 100644 index 0000000000000..1975b0e15986a --- /dev/null +++ b/src/Symfony/Bundle/TwigBundle/Tests/Fixtures/templates/Resources/TwigBundle/views/layout.html.twig @@ -0,0 +1 @@ +{# Twig template #} diff --git a/src/Symfony/Bundle/TwigBundle/Tests/Functional/NoTemplatingEntryTest.php b/src/Symfony/Bundle/TwigBundle/Tests/Functional/NoTemplatingEntryTest.php index ff3318780f7d4..8c0495028a8e5 100644 --- a/src/Symfony/Bundle/TwigBundle/Tests/Functional/NoTemplatingEntryTest.php +++ b/src/Symfony/Bundle/TwigBundle/Tests/Functional/NoTemplatingEntryTest.php @@ -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', )) ; }); diff --git a/src/Symfony/Bundle/TwigBundle/Tests/Functional/Resources/views/index.html.twig b/src/Symfony/Bundle/TwigBundle/Tests/Functional/templates/index.html.twig similarity index 100% rename from src/Symfony/Bundle/TwigBundle/Tests/Functional/Resources/views/index.html.twig rename to src/Symfony/Bundle/TwigBundle/Tests/Functional/templates/index.html.twig diff --git a/src/Symfony/Bundle/TwigBundle/Tests/Loader/FilesystemLoaderTest.php b/src/Symfony/Bundle/TwigBundle/Tests/Loader/FilesystemLoaderTest.php index b9294e35b3c46..e39313cd87ea1 100644 --- a/src/Symfony/Bundle/TwigBundle/Tests/Loader/FilesystemLoaderTest.php +++ b/src/Symfony/Bundle/TwigBundle/Tests/Loader/FilesystemLoaderTest.php @@ -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()); @@ -44,7 +44,7 @@ public function testExists() $locator ->expects($this->once()) ->method('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); @@ -101,7 +101,7 @@ 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() { @@ -109,7 +109,7 @@ public function testTwigErrorIfTemplateDoesNotExist() $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); @@ -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);