Removed capability to load Twig template from legacy directories

This commit is contained in:
Yonel Ceruto 2019-05-28 17:10:04 -04:00
parent 0dd0679362
commit 4b6752bf3e
6 changed files with 3 additions and 112 deletions

View File

@ -38,15 +38,10 @@ class DebugCommand extends Command
private $projectDir;
private $bundlesMetadata;
private $twigDefaultPath;
private $rootDir;
private $filesystemLoaders;
private $fileLinkFormatter;
/**
* @param FileLinkFormatter|null $fileLinkFormatter
* @param string|null $rootDir
*/
public function __construct(Environment $twig, string $projectDir = null, array $bundlesMetadata = [], string $twigDefaultPath = null, $fileLinkFormatter = null, $rootDir = null)
public function __construct(Environment $twig, string $projectDir = null, array $bundlesMetadata = [], string $twigDefaultPath = null, FileLinkFormatter $fileLinkFormatter = null)
{
parent::__construct();
@ -54,16 +49,7 @@ class DebugCommand extends Command
$this->projectDir = $projectDir;
$this->bundlesMetadata = $bundlesMetadata;
$this->twigDefaultPath = $twigDefaultPath;
if (\is_string($fileLinkFormatter) || $rootDir instanceof FileLinkFormatter) {
@trigger_error(sprintf('Passing a string as "$fileLinkFormatter" 5th argument or an instance of FileLinkFormatter as "$rootDir" 6th argument of the "%s()" method is deprecated since Symfony 4.4, swap the variables position.', __METHOD__), E_USER_DEPRECATED);
$this->rootDir = $fileLinkFormatter;
$this->fileLinkFormatter = $rootDir;
} else {
$this->fileLinkFormatter = $fileLinkFormatter;
$this->rootDir = $rootDir;
}
$this->fileLinkFormatter = $fileLinkFormatter;
}
protected function configure()
@ -397,22 +383,6 @@ EOF
$alternatives = [];
$bundleNames = [];
if ($this->rootDir && $this->projectDir) {
$folders = glob($this->rootDir.'/Resources/*/views', GLOB_ONLYDIR);
$relativePath = ltrim(substr($this->rootDir.\DIRECTORY_SEPARATOR.'Resources/', \strlen($this->projectDir)), \DIRECTORY_SEPARATOR);
$bundleNames = array_reduce($folders, function ($carry, $absolutePath) use ($relativePath) {
if (0 === strpos($absolutePath, $this->projectDir)) {
$name = basename(\dirname($absolutePath));
$path = ltrim($relativePath.$name, \DIRECTORY_SEPARATOR);
$carry[$name] = $path;
@trigger_error(sprintf('Loading Twig templates from the "%s" directory is deprecated since Symfony 4.2, use "%s" instead.', $absolutePath, $this->twigDefaultPath.'/bundles/'.$name), E_USER_DEPRECATED);
}
return $carry;
}, $bundleNames);
}
if ($this->twigDefaultPath && $this->projectDir) {
$folders = glob($this->twigDefaultPath.'/bundles/*', GLOB_ONLYDIR);
$relativePath = ltrim(substr($this->twigDefaultPath.'/bundles/', \strlen($this->projectDir)), \DIRECTORY_SEPARATOR);

View File

@ -63,32 +63,6 @@ class DebugCommandTest extends TestCase
$this->assertEquals($expected, json_decode($tester->getDisplay(true), true));
}
/**
* @group legacy
* @expectedDeprecation Loading Twig templates from the "%sResources/BarBundle/views" directory is deprecated since Symfony 4.2, use "%stemplates/bundles/BarBundle" instead.
*/
public function testDeprecationForWrongBundleOverridingInLegacyPath()
{
$bundleMetadata = [
'TwigBundle' => 'vendor/twig-bundle/',
'WebProfilerBundle' => 'vendor/web-profiler-bundle/',
];
$defaultPath = \dirname(__DIR__).\DIRECTORY_SEPARATOR.'Fixtures'.\DIRECTORY_SEPARATOR.'templates';
$rootDir = \dirname(__DIR__).\DIRECTORY_SEPARATOR.'Fixtures';
$tester = $this->createCommandTester([], $bundleMetadata, $defaultPath, $rootDir);
$ret = $tester->execute(['--filter' => 'unknown', '--format' => 'json'], ['decorated' => false]);
$expected = ['warnings' => [
'Path "Resources/BarBundle" not matching any bundle found',
'Path "templates/bundles/UnknownBundle" not matching any bundle found',
'Path "templates/bundles/WebProfileBundle" not matching any bundle found, did you mean "WebProfilerBundle"?',
]];
$this->assertEquals(0, $ret, 'Returns 0 in case of success');
$this->assertEquals($expected, json_decode($tester->getDisplay(true), true));
}
/**
* @expectedException \Symfony\Component\Console\Exception\InvalidArgumentException
* @expectedExceptionMessage Malformed namespaced template name "@foo" (expecting "@namespace/template_name").

View File

@ -5,6 +5,7 @@ CHANGELOG
-----
* updated default value for the `strict_variables` option to `%kernel.debug%` parameter
* removed support to load templates from the legacy directories `src/Resources/views/` and `src/Resources/<BundleName>/views/`
4.2.0
-----

View File

@ -120,15 +120,6 @@ class TwigExtension extends Extension
}
}
if (file_exists($dir = $container->getParameter('kernel.root_dir').'/Resources/views')) {
if ($dir !== $defaultTwigPath) {
@trigger_error(sprintf('Loading Twig templates from the "%s" directory is deprecated since Symfony 4.2, use "%s" instead.', $dir, $defaultTwigPath), E_USER_DEPRECATED);
}
$twigFilesystemLoaderDefinition->addMethodCall('addPath', [$dir]);
}
$container->addResource(new FileExistenceResource($dir));
if (file_exists($defaultTwigPath)) {
$twigFilesystemLoaderDefinition->addMethodCall('addPath', [$defaultTwigPath]);
}
@ -176,13 +167,6 @@ class TwigExtension extends Extension
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('Loading Twig templates for "%s" from the "%s" directory is deprecated since Symfony 4.2, use "%s" instead.', $name, $dir, $defaultOverrideBundlePath), E_USER_DEPRECATED);
$bundleHierarchy[$name][] = $dir;
}
$container->addResource(new FileExistenceResource($dir));
if (file_exists($defaultOverrideBundlePath)) {
$bundleHierarchy[$name][] = $defaultOverrideBundlePath;
}

View File

@ -197,43 +197,6 @@ class TwigExtensionTest extends TestCase
], $paths);
}
/**
* @group legacy
* @dataProvider getFormats
* @expectedDeprecation Loading Twig templates for "TwigBundle" from the "%s/Resources/TwigBundle/views" directory is deprecated since Symfony 4.2, use "%s/templates/bundles/TwigBundle" instead.
* @expectedDeprecation Loading Twig templates from the "%s/Resources/views" directory 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 = [];
foreach ($def->getMethodCalls() as $call) {
if ('addPath' === $call[0] && false === strpos($call[1][0], 'Form')) {
$paths[] = $call[1];
}
}
$this->assertEquals([
['path1'],
['path2'],
['namespaced_path1', 'namespace1'],
['namespaced_path2', 'namespace2'],
['namespaced_path3', 'namespace3'],
[__DIR__.'/../Fixtures/templates/Resources/TwigBundle/views', 'Twig'],
[__DIR__.'/Fixtures/templates/bundles/TwigBundle', 'Twig'],
[realpath(__DIR__.'/../..').'/Resources/views', 'Twig'],
[realpath(__DIR__.'/../..').'/Resources/views', '!Twig'],
[__DIR__.'/../Fixtures/templates/Resources/views'],
[__DIR__.'/Fixtures/templates'],
], $paths);
}
public function getFormats()
{
return [