Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit acae8bb

Browse files
committed
Deprecating support for legacy templates directories
1 parent 557f85d commit acae8bb

File tree

13 files changed

+72
-21
lines changed

13 files changed

+72
-21
lines changed

UPGRADE-4.2.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ TwigBundle
257257
----------
258258

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

261262
Validator
262263
---------

UPGRADE-5.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ TwigBundle
256256

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

260261
Validator
261262
--------

src/Symfony/Bundle/SecurityBundle/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"symfony/framework-bundle": "~4.2",
3838
"symfony/http-foundation": "~3.4|~4.0",
3939
"symfony/translation": "~3.4|~4.0",
40-
"symfony/twig-bundle": "~3.4|~4.0",
40+
"symfony/twig-bundle": "~4.2",
4141
"symfony/twig-bridge": "~3.4|~4.0",
4242
"symfony/process": "~3.4|~4.0",
4343
"symfony/validator": "~3.4|~4.0",
@@ -49,6 +49,7 @@
4949
},
5050
"conflict": {
5151
"symfony/browser-kit": "<4.2",
52+
"symfony/twig-bundle": "<4.2",
5253
"symfony/var-dumper": "<3.4",
5354
"symfony/event-dispatcher": "<3.4",
5455
"symfony/framework-bundle": "<4.2",

src/Symfony/Bundle/TwigBundle/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
4.2.0
5+
-----
6+
7+
* deprecated support for legacy templates directories `src/Resources/views/` and `src/Resources/<BundleName>/views/`, use `templates/` and `templates/bundles/<BundleName>/` instead.
8+
49
4.1.0
510
-----
611

src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public function load(array $configs, ContainerBuilder $container)
7474

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

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

117118
if (file_exists($dir = $container->getParameter('kernel.root_dir').'/Resources/views')) {
119+
if ($dir !== $defaultTwigPath) {
120+
@trigger_error(sprintf('Templates directory "%s" is deprecated since Symfony 4.2, use "%s" instead.', $dir, $defaultTwigPath), E_USER_DEPRECATED);
121+
}
122+
118123
$twigFilesystemLoaderDefinition->addMethodCall('addPath', array($dir));
119124
}
120125
$container->addResource(new FileExistenceResource($dir));
121126

122-
if (file_exists($dir = $container->getParameterBag()->resolveValue($config['default_path']))) {
123-
$twigFilesystemLoaderDefinition->addMethodCall('addPath', array($dir));
127+
if (file_exists($defaultTwigPath)) {
128+
$twigFilesystemLoaderDefinition->addMethodCall('addPath', array($defaultTwigPath));
124129
}
125-
$container->addResource(new FileExistenceResource($dir));
130+
$container->addResource(new FileExistenceResource($defaultTwigPath));
126131

127132
if (!empty($config['globals'])) {
128133
$def = $container->getDefinition('twig');
@@ -164,15 +169,19 @@ private function getBundleTemplatePaths(ContainerBuilder $container, array $conf
164169
{
165170
$bundleHierarchy = array();
166171
foreach ($container->getParameter('kernel.bundles_metadata') as $name => $bundle) {
172+
$defaultOverrideBundlePath = $container->getParameterBag()->resolveValue($config['default_path']).'/bundles/'.$name;
173+
167174
if (file_exists($dir = $container->getParameter('kernel.root_dir').'/Resources/'.$name.'/views')) {
175+
@trigger_error(sprintf('Templates directory "%s" is deprecated since Symfony 4.2, use "%s" instead.', $dir, $defaultOverrideBundlePath), E_USER_DEPRECATED);
176+
168177
$bundleHierarchy[$name][] = $dir;
169178
}
170179
$container->addResource(new FileExistenceResource($dir));
171180

172-
if (file_exists($dir = $container->getParameterBag()->resolveValue($config['default_path']).'/bundles/'.$name)) {
173-
$bundleHierarchy[$name][] = $dir;
181+
if (file_exists($defaultOverrideBundlePath)) {
182+
$bundleHierarchy[$name][] = $defaultOverrideBundlePath;
174183
}
175-
$container->addResource(new FileExistenceResource($dir));
184+
$container->addResource(new FileExistenceResource($defaultOverrideBundlePath));
176185

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

src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/Resources/TwigBundle/views/layout.html.twig

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/Fixtures/Resources/views/layout.html.twig

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,45 @@ public function testTwigLoaderPaths($format)
193193
array('namespaced_path1', 'namespace1'),
194194
array('namespaced_path2', 'namespace2'),
195195
array('namespaced_path3', 'namespace3'),
196-
array(__DIR__.'/Fixtures/Resources/TwigBundle/views', 'Twig'),
197196
array(__DIR__.'/Fixtures/templates/bundles/TwigBundle', 'Twig'),
198197
array(realpath(__DIR__.'/../..').'/Resources/views', 'Twig'),
199198
array(realpath(__DIR__.'/../..').'/Resources/views', '!Twig'),
200-
array(__DIR__.'/Fixtures/Resources/views'),
199+
array(__DIR__.'/Fixtures/templates'),
200+
), $paths);
201+
}
202+
203+
/**
204+
* @group legacy
205+
* @dataProvider getFormats
206+
* @expectedDeprecation Templates directory "%s" is deprecated since Symfony 4.2, use "%s" instead.
207+
*/
208+
public function testLegacyTwigLoaderPaths($format)
209+
{
210+
$container = $this->createContainer(__DIR__.'/../Fixtures/templates');
211+
$container->registerExtension(new TwigExtension());
212+
$this->loadFromFile($container, 'full', $format);
213+
$this->loadFromFile($container, 'extra', $format);
214+
$this->compileContainer($container);
215+
216+
$def = $container->getDefinition('twig.loader.native_filesystem');
217+
$paths = array();
218+
foreach ($def->getMethodCalls() as $call) {
219+
if ('addPath' === $call[0] && false === strpos($call[1][0], 'Form')) {
220+
$paths[] = $call[1];
221+
}
222+
}
223+
224+
$this->assertEquals(array(
225+
array('path1'),
226+
array('path2'),
227+
array('namespaced_path1', 'namespace1'),
228+
array('namespaced_path2', 'namespace2'),
229+
array('namespaced_path3', 'namespace3'),
230+
array(__DIR__.'/../Fixtures/templates/Resources/TwigBundle/views', 'Twig'),
231+
array(__DIR__.'/Fixtures/templates/bundles/TwigBundle', 'Twig'),
232+
array(realpath(__DIR__.'/../..').'/Resources/views', 'Twig'),
233+
array(realpath(__DIR__.'/../..').'/Resources/views', '!Twig'),
234+
array(__DIR__.'/../Fixtures/templates/Resources/views'),
201235
array(__DIR__.'/Fixtures/templates'),
202236
), $paths);
203237
}
@@ -271,11 +305,11 @@ public function testRuntimeLoader()
271305
$this->assertEquals('foo', $args['FooClass']->getValues()[0]);
272306
}
273307

274-
private function createContainer()
308+
private function createContainer(string $rootDir = __DIR__.'/Fixtures')
275309
{
276310
$container = new ContainerBuilder(new ParameterBag(array(
277311
'kernel.cache_dir' => __DIR__,
278-
'kernel.root_dir' => __DIR__.'/Fixtures',
312+
'kernel.root_dir' => $rootDir,
279313
'kernel.project_dir' => __DIR__,
280314
'kernel.charset' => 'UTF-8',
281315
'kernel.debug' => false,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{# Twig template #}

src/Symfony/Bundle/TwigBundle/Tests/Functional/NoTemplatingEntryTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ public function registerContainerConfiguration(LoaderInterface $loader)
6666
'secret' => '$ecret',
6767
'form' => array('enabled' => false),
6868
))
69-
->loadFromExtension('twig', array( // to be removed in 5.0 relying on default
70-
'strict_variables' => false,
69+
->loadFromExtension('twig', array(
70+
'strict_variables' => false, // to be removed in 5.0 relying on default
71+
'default_path' => __DIR__.'/templates',
7172
))
7273
;
7374
});

src/Symfony/Bundle/TwigBundle/Tests/Loader/FilesystemLoaderTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ public function testGetSourceContext()
2424
$locator
2525
->expects($this->once())
2626
->method('locate')
27-
->will($this->returnValue(__DIR__.'/../DependencyInjection/Fixtures/Resources/views/layout.html.twig'))
27+
->will($this->returnValue(__DIR__.'/../DependencyInjection/Fixtures/templates/layout.html.twig'))
2828
;
2929
$loader = new FilesystemLoader($locator, $parser);
30-
$loader->addPath(__DIR__.'/../DependencyInjection/Fixtures/Resources/views', 'namespace');
30+
$loader->addPath(__DIR__.'/../DependencyInjection/Fixtures/templates', 'namespace');
3131

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

@@ -101,15 +101,15 @@ public function testTwigErrorIfLocatorReturnsFalse()
101101

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

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

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

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

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

0 commit comments

Comments
 (0)