[TwigBundle] remove legacy template dirs from iterator

This commit is contained in:
Tobias Schultze 2019-06-03 17:58:55 +02:00
parent ebd8f21ced
commit d1137f96c8
12 changed files with 13 additions and 33 deletions

View File

@ -46,9 +46,9 @@ class ExtensionPass implements CompilerPassInterface
$coreThemePath = \dirname(\dirname($reflClass->getFileName())).'/Resources/views/Form';
$container->getDefinition('twig.loader.native_filesystem')->addMethodCall('addPath', [$coreThemePath]);
$paths = $container->getDefinition('twig.template_iterator')->getArgument(2);
$paths = $container->getDefinition('twig.template_iterator')->getArgument(1);
$paths[$coreThemePath] = null;
$container->getDefinition('twig.template_iterator')->replaceArgument(2, $paths);
$container->getDefinition('twig.template_iterator')->replaceArgument(1, $paths);
}
if ($container->has('router')) {

View File

@ -11,7 +11,6 @@
namespace Symfony\Bundle\TwigBundle\DependencyInjection;
use Symfony\Bundle\TwigBundle\Loader\NativeFilesystemLoader;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Resource\FileExistenceResource;
use Symfony\Component\Console\Application;
@ -97,7 +96,7 @@ class TwigExtension extends Extension
}
// paths are modified in ExtensionPass if forms are enabled
$container->getDefinition('twig.template_iterator')->replaceArgument(2, $config['paths']);
$container->getDefinition('twig.template_iterator')->replaceArgument(1, $config['paths']);
foreach ($this->getBundleTemplatePaths($container, $config) as $name => $paths) {
$namespace = $this->normalizeBundleName($name);

View File

@ -31,7 +31,6 @@
<service id="twig.template_iterator" class="Symfony\Bundle\TwigBundle\TemplateIterator">
<argument type="service" id="kernel" />
<argument>%kernel.root_dir%</argument>
<argument type="collection" /> <!-- Twig paths -->
<argument>%twig.default_path%</argument>
</service>

View File

@ -24,21 +24,18 @@ use Symfony\Component\HttpKernel\KernelInterface;
class TemplateIterator implements \IteratorAggregate
{
private $kernel;
private $rootDir;
private $templates;
private $paths;
private $defaultPath;
/**
* @param KernelInterface $kernel A KernelInterface instance
* @param string $rootDir The directory where global templates can be stored
* @param array $paths Additional Twig paths to warm
* @param string|null $defaultPath The directory where global templates can be stored
*/
public function __construct(KernelInterface $kernel, string $rootDir, array $paths = [], string $defaultPath = null)
public function __construct(KernelInterface $kernel, array $paths = [], string $defaultPath = null)
{
$this->kernel = $kernel;
$this->rootDir = $rootDir;
$this->paths = $paths;
$this->defaultPath = $defaultPath;
}
@ -52,14 +49,8 @@ class TemplateIterator implements \IteratorAggregate
return $this->templates;
}
$templates = $this->findTemplatesInDirectory($this->rootDir.'/Resources/views');
$templates = null !== $this->defaultPath ? $this->findTemplatesInDirectory($this->defaultPath, null, ['bundles']) : [];
if (null !== $this->defaultPath) {
$templates = array_merge(
$templates,
$this->findTemplatesInDirectory($this->defaultPath, null, ['bundles'])
);
}
foreach ($this->kernel->getBundles() as $bundle) {
$name = $bundle->getName();
if ('Bundle' === substr($name, -6)) {
@ -69,14 +60,8 @@ class TemplateIterator implements \IteratorAggregate
$templates = array_merge(
$templates,
$this->findTemplatesInDirectory($bundle->getPath().'/Resources/views', $name),
$this->findTemplatesInDirectory($this->rootDir.'/Resources/'.$bundle->getName().'/views', $name)
null !== $this->defaultPath ? $this->findTemplatesInDirectory($this->defaultPath.'/bundles/'.$bundle->getName(), $name) : []
);
if (null !== $this->defaultPath) {
$templates = array_merge(
$templates,
$this->findTemplatesInDirectory($this->defaultPath.'/bundles/'.$bundle->getName(), $name)
);
}
}
foreach ($this->paths as $dir => $namespace) {

View File

@ -25,18 +25,16 @@ class TemplateIteratorTest extends TestCase
$kernel->expects($this->any())->method('getBundles')->willReturn([
$bundle,
]);
$iterator = new TemplateIterator($kernel, __DIR__.'/Fixtures/templates', [__DIR__.'/Fixtures/templates/Foo' => 'Foo'], __DIR__.'/DependencyInjection/Fixtures/templates');
$iterator = new TemplateIterator($kernel, [__DIR__.'/Fixtures/templates/Foo' => 'Foo'], __DIR__.'/DependencyInjection/Fixtures/templates');
$sorted = iterator_to_array($iterator);
sort($sorted);
$this->assertEquals(
[
'@Bar/base.html.twig',
'@Bar/index.html.twig',
'@Bar/layout.html.twig',
'@Foo/index.html.twig',
'layout.html.twig',
'sub/sub.html.twig',
],
$sorted
);

View File

@ -32,7 +32,7 @@
"symfony/finder": "^4.4|^5.0",
"symfony/form": "^4.4|^5.0",
"symfony/routing": "^4.4|^5.0",
"symfony/translation": "^4.4|^5.0",
"symfony/translation": "^5.0",
"symfony/yaml": "^4.4|^5.0",
"symfony/framework-bundle": "^5.0",
"symfony/web-link": "^4.4|^5.0",
@ -42,7 +42,7 @@
"conflict": {
"symfony/dependency-injection": "<4.4",
"symfony/framework-bundle": "<5.0",
"symfony/translation": "<4.4"
"symfony/translation": "<5.0"
},
"autoload": {
"psr-4": { "Symfony\\Bundle\\TwigBundle\\": "" },

View File

@ -68,7 +68,7 @@ class TranslatorPass implements CompilerPassInterface
return;
}
$paths = array_keys($container->getDefinition('twig.template_iterator')->getArgument(2));
$paths = array_keys($container->getDefinition('twig.template_iterator')->getArgument(1));
if ($container->hasDefinition($this->debugCommandServiceId)) {
$definition = $container->getDefinition($this->debugCommandServiceId);
$definition->replaceArgument(4, $container->getParameter('twig.default_path'));

View File

@ -68,7 +68,7 @@ class TranslationPassTest extends TestCase
->setArguments([null, null, null, null, null, null, [], []])
;
$container->register('twig.template_iterator')
->setArguments([null, null, ['other/templates' => null, 'tpl' => 'App']])
->setArguments([null, ['other/templates' => null, 'tpl' => 'App']])
;
$container->setParameter('twig.default_path', 'templates');
@ -109,7 +109,7 @@ class TranslationPassTest extends TestCase
])
;
$container->register('twig.template_iterator')
->setArguments([null, null, ['other/templates' => null, 'tpl' => 'App']])
->setArguments([null, ['other/templates' => null, 'tpl' => 'App']])
;
$container->setParameter('twig.default_path', 'templates');

View File

@ -35,6 +35,7 @@
"conflict": {
"symfony/config": "<4.4",
"symfony/dependency-injection": "<4.4",
"symfony/twig-bundle": "<5.0",
"symfony/yaml": "<4.4"
},
"provide": {