Skip to content

[FrameworkBundle] forward-compatibility with HttpKernel 5 #33356

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

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ class TemplateFinder implements TemplateFinderInterface
private $templates;

/**
* @param string $rootDir The directory where global templates can be stored
* @param string|null $rootDir The directory where global templates can be stored
*/
public function __construct(KernelInterface $kernel, TemplateNameParserInterface $parser, string $rootDir)
public function __construct(KernelInterface $kernel, TemplateNameParserInterface $parser, string $rootDir = null)
{
$this->kernel = $kernel;
$this->parser = $parser;
Expand All @@ -60,7 +60,9 @@ public function findAllTemplates()
$templates = array_merge($templates, $this->findTemplatesInBundle($bundle));
}

$templates = array_merge($templates, $this->findTemplatesInFolder($this->rootDir.'/views'));
if (null !== $this->rootDir) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't we use $this->kernel->getContainer()->hasParameter('kernel.root_dir') here as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have the container here. We inject the argument depending on the presence of the parameter.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but we have the kernel instance, no? $this->kernel->getContainer()?

$templates = array_merge($templates, $this->findTemplatesInFolder($this->rootDir.'/views'));
}

return $this->templates = $templates;
}
Expand Down Expand Up @@ -99,7 +101,7 @@ private function findTemplatesInBundle(BundleInterface $bundle): array
$name = $bundle->getName();
$templates = array_unique(array_merge(
$this->findTemplatesInFolder($bundle->getPath().'/Resources/views'),
$this->findTemplatesInFolder($this->rootDir.'/'.$name.'/views')
null !== $this->rootDir ? $this->findTemplatesInFolder($this->rootDir.'/'.$name.'/views') : []
));

foreach ($templates as $i => $template) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
$domain = $input->getOption('domain');
/** @var KernelInterface $kernel */
$kernel = $this->getApplication()->getKernel();
$rootDir = $kernel->getContainer()->getParameter('kernel.root_dir');
Copy link
Member

@nicolas-grekas nicolas-grekas Aug 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$rootDir = $kernel->getContainer()->hasParameter('kernel.root_dir') ? $kernel->getContainer()->getParameter('kernel.root_dir') : null
then compare to null below?


// Define Root Paths
$transPaths = $this->transPaths;
if (is_dir($dir = $rootDir.'/Resources/translations')) {
if ($kernel->getContainer()->hasParameter('kernel.root_dir') && is_dir($dir = $kernel->getContainer()->getParameter('kernel.root_dir').'/Resources/translations')) {
if ($dir !== $this->defaultTransPath) {
$notice = sprintf('Storing translations in the "%s" directory is deprecated since Symfony 4.2, ', $dir);
@trigger_error($notice.($this->defaultTransPath ? sprintf('use the "%s" directory instead.', $this->defaultTransPath) : 'configure and use "framework.translator.default_path" instead.'), E_USER_DEPRECATED);
Expand All @@ -147,7 +146,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$transPaths[] = $this->defaultTransPath;
}
$viewsPaths = $this->viewsPaths;
if (is_dir($dir = $rootDir.'/Resources/views')) {
if ($kernel->getContainer()->hasParameter('kernel.root_dir') && is_dir($dir = $kernel->getContainer()->getParameter('kernel.root_dir').'/Resources/views')) {
if ($dir !== $this->defaultViewsPath) {
$notice = sprintf('Loading Twig templates from the "%s" directory is deprecated since Symfony 4.2, ', $dir);
@trigger_error($notice.($this->defaultViewsPath ? sprintf('use the "%s" directory instead.', $this->defaultViewsPath) : 'configure and use "twig.default_path" instead.'), E_USER_DEPRECATED);
Expand All @@ -168,15 +167,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
if ($this->defaultTransPath) {
$transPaths[] = $this->defaultTransPath;
}
if (is_dir($dir = sprintf('%s/Resources/%s/translations', $rootDir, $bundle->getName()))) {
if ($kernel->getContainer()->hasParameter('kernel.root_dir') && is_dir($dir = sprintf('%s/Resources/%s/translations', $kernel->getContainer()->getParameter('kernel.root_dir'), $bundle->getName()))) {
$transPaths[] = $dir;
$notice = sprintf('Storing translations files for "%s" in the "%s" directory is deprecated since Symfony 4.2, ', $dir, $bundle->getName());
@trigger_error($notice.($this->defaultTransPath ? sprintf('use the "%s" directory instead.', $this->defaultTransPath) : 'configure and use "framework.translator.default_path" instead.'), E_USER_DEPRECATED);
}
if ($this->defaultViewsPath) {
$viewsPaths[] = $this->defaultViewsPath;
}
if (is_dir($dir = sprintf('%s/Resources/%s/views', $rootDir, $bundle->getName()))) {
if ($kernel->getContainer()->hasParameter('kernel.root_dir') && is_dir($dir = sprintf('%s/Resources/%s/views', $kernel->getContainer()->getParameter('kernel.root_dir'), $bundle->getName()))) {
$viewsPaths[] = $dir;
$notice = sprintf('Loading Twig templates for "%s" from the "%s" directory is deprecated since Symfony 4.2, ', $bundle->getName(), $dir);
@trigger_error($notice.($this->defaultViewsPath ? sprintf('use the "%s" directory instead.', $this->defaultViewsPath) : 'configure and use "twig.default_path" instead.'), E_USER_DEPRECATED);
Expand Down Expand Up @@ -210,12 +209,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
$bundleDir = $bundle->getPath();
$transPaths[] = is_dir($bundleDir.'/Resources/translations') ? $bundleDir.'/Resources/translations' : $bundle->getPath().'/translations';
$viewsPaths[] = is_dir($bundleDir.'/Resources/views') ? $bundleDir.'/Resources/views' : $bundle->getPath().'/templates';
if (is_dir($deprecatedPath = sprintf('%s/Resources/%s/translations', $rootDir, $bundle->getName()))) {
if ($kernel->getContainer()->hasParameter('kernel.root_dir') && is_dir($deprecatedPath = sprintf('%s/Resources/%s/translations', $kernel->getContainer()->getParameter('kernel.root_dir'), $bundle->getName()))) {
$transPaths[] = $deprecatedPath;
$notice = sprintf('Storing translations files for "%s" in the "%s" directory is deprecated since Symfony 4.2, ', $bundle->getName(), $deprecatedPath);
@trigger_error($notice.($this->defaultTransPath ? sprintf('use the "%s" directory instead.', $this->defaultTransPath) : 'configure and use "framework.translator.default_path" instead.'), E_USER_DEPRECATED);
}
if (is_dir($deprecatedPath = sprintf('%s/Resources/%s/views', $rootDir, $bundle->getName()))) {
if ($kernel->getContainer()->hasParameter('kernel.root_dir') && is_dir($deprecatedPath = sprintf('%s/Resources/%s/views', $kernel->getContainer()->getParameter('kernel.root_dir'), $bundle->getName()))) {
$viewsPaths[] = $deprecatedPath;
$notice = sprintf('Loading Twig templates for "%s" from the "%s" directory is deprecated since Symfony 4.2, ', $bundle->getName(), $deprecatedPath);
@trigger_error($notice.($this->defaultViewsPath ? sprintf('use the "%s" directory instead.', $this->defaultViewsPath) : 'configure and use "twig.default_path" instead.'), E_USER_DEPRECATED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
/** @var KernelInterface $kernel */
$kernel = $this->getApplication()->getKernel();
$rootDir = $kernel->getContainer()->getParameter('kernel.root_dir');

// Define Root Paths
$transPaths = $this->transPaths;
if (is_dir($dir = $rootDir.'/Resources/translations')) {
if ($kernel->getContainer()->hasParameter('kernel.root_dir') && is_dir($dir = $kernel->getContainer()->getParameter('kernel.root_dir').'/Resources/translations')) {
if ($dir !== $this->defaultTransPath) {
$notice = sprintf('Storing translations in the "%s" directory is deprecated since Symfony 4.2, ', $dir);
@trigger_error($notice.($this->defaultTransPath ? sprintf('use the "%s" directory instead.', $this->defaultTransPath) : 'configure and use "framework.translator.default_path" instead.'), E_USER_DEPRECATED);
Expand All @@ -138,7 +137,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$transPaths[] = $this->defaultTransPath;
}
$viewsPaths = $this->viewsPaths;
if (is_dir($dir = $rootDir.'/Resources/views')) {
if ($kernel->getContainer()->hasParameter('kernel.root_dir') && is_dir($dir = $kernel->getContainer()->getParameter('kernel.root_dir').'/Resources/views')) {
if ($dir !== $this->defaultViewsPath) {
$notice = sprintf('Storing templates in the "%s" directory is deprecated since Symfony 4.2, ', $dir);
@trigger_error($notice.($this->defaultViewsPath ? sprintf('use the "%s" directory instead.', $this->defaultViewsPath) : 'configure and use "twig.default_path" instead.'), E_USER_DEPRECATED);
Expand All @@ -160,15 +159,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if ($this->defaultTransPath) {
$transPaths[] = $this->defaultTransPath;
}
if (is_dir($dir = sprintf('%s/Resources/%s/translations', $rootDir, $foundBundle->getName()))) {
if ($kernel->getContainer()->hasParameter('kernel.root_dir') && is_dir($dir = sprintf('%s/Resources/%s/translations', $kernel->getContainer()->getParameter('kernel.root_dir'), $foundBundle->getName()))) {
$transPaths[] = $dir;
$notice = sprintf('Storing translations files for "%s" in the "%s" directory is deprecated since Symfony 4.2, ', $foundBundle->getName(), $dir);
@trigger_error($notice.($this->defaultTransPath ? sprintf('use the "%s" directory instead.', $this->defaultTransPath) : 'configure and use "framework.translator.default_path" instead.'), E_USER_DEPRECATED);
}
if ($this->defaultViewsPath) {
$viewsPaths[] = $this->defaultViewsPath;
}
if (is_dir($dir = sprintf('%s/Resources/%s/views', $rootDir, $foundBundle->getName()))) {
if ($kernel->getContainer()->hasParameter('kernel.root_dir') && is_dir($dir = sprintf('%s/Resources/%s/views', $kernel->getContainer()->getParameter('kernel.root_dir'), $foundBundle->getName()))) {
$viewsPaths[] = $dir;
$notice = sprintf('Storing templates for "%s" in the "%s" directory is deprecated since Symfony 4.2, ', $foundBundle->getName(), $dir);
@trigger_error($notice.($this->defaultViewsPath ? sprintf('use the "%s" directory instead.', $this->defaultViewsPath) : 'configure and use "twig.default_path" instead.'), E_USER_DEPRECATED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Lock\Factory;
use Symfony\Component\Lock\Lock;
use Symfony\Component\Lock\LockFactory;
Expand Down Expand Up @@ -183,6 +184,15 @@ public function load(array $configs, ContainerBuilder $container)
}
}

if (Kernel::MAJOR_VERSION < 5) {
$container->getDefinition('file_locator')
->addArgument('%kernel.root_dir%/Resources')
->addArgument([
'%kernel.root_dir%',
])
->addArgument(false);
}

// Load Cache configuration first as it is used by other components
$loader->load('cache.xml');

Expand Down Expand Up @@ -960,6 +970,14 @@ private function registerTemplatingConfiguration(array $config, ContainerBuilder
->addMethodCall('setLogger', [$logger]);
}

if (Kernel::MAJOR_VERSION < 5) {
$container->getDefinition('templating.finder')
->addArgument('%kernel.root_dir%/Resources');
} else {
$container->getDefinition('templating.finder')
->addArgument(null);
}

if (!empty($config['loaders'])) {
$loaders = array_map(function ($loader) { return new Reference($loader); }, $config['loaders']);

Expand Down Expand Up @@ -1150,14 +1168,13 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
$dirs[] = $transPaths[] = \dirname(\dirname($r->getFileName())).'/Resources/translations';
}
$defaultDir = $container->getParameterBag()->resolveValue($config['default_path']);
$rootDir = $container->getParameter('kernel.root_dir');
foreach ($container->getParameter('kernel.bundles_metadata') as $name => $bundle) {
if ($container->fileExists($dir = $bundle['path'].'/Resources/translations') || $container->fileExists($dir = $bundle['path'].'/translations')) {
$dirs[] = $dir;
} else {
$nonExistingDirs[] = $dir;
}
if ($container->fileExists($dir = $rootDir.sprintf('/Resources/%s/translations', $name))) {
if ($container->hasParameter('kernel.root_dir') && $container->fileExists($dir = $container->getParameter('kernel.root_dir').sprintf('/Resources/%s/translations', $name))) {
@trigger_error(sprintf('Translations directory "%s" is deprecated since Symfony 4.2, use "%s" instead.', $dir, $defaultDir), E_USER_DEPRECATED);
$dirs[] = $dir;
} else {
Expand Down Expand Up @@ -1187,7 +1204,7 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
$nonExistingDirs[] = $defaultDir;
}

if ($container->fileExists($dir = $rootDir.'/Resources/translations')) {
if ($container->hasParameter('kernel.root_dir') && $container->fileExists($dir = $container->getParameter('kernel.root_dir').'/Resources/translations')) {
if ($dir !== $defaultDir) {
@trigger_error(sprintf('Translations directory "%s" is deprecated since Symfony 4.2, use "%s" instead.', $dir, $defaultDir), E_USER_DEPRECATED);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,6 @@

<service id="file_locator" class="Symfony\Component\HttpKernel\Config\FileLocator">
<argument type="service" id="kernel" />
<argument>%kernel.root_dir%/Resources</argument>
<argument type="collection">
<argument>%kernel.root_dir%</argument>
</argument>
<argument>false</argument>
</service>
<service id="Symfony\Component\HttpKernel\Config\FileLocator" alias="file_locator" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
<service id="templating.finder" class="Symfony\Bundle\FrameworkBundle\CacheWarmer\TemplateFinder">
<argument type="service" id="kernel" />
<argument type="service" id="templating.filename_parser" />
<argument>%kernel.root_dir%/Resources</argument>
Copy link
Member

@yceruto yceruto Aug 27, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there another way to get this path for TemplateFinder? otherwise it's a BC break to me (solved by #33356 (comment))

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what we can do here. The parameter will be gone with HttpKernel 5 and the class will be dropped in FrameworkBundle 5 too. I don't see what would be the fallback value here. The only safe solution probably would be to not allow HttpKernel 5.


<deprecated>The "%service_id%" service is deprecated since Symfony 4.3 and will be removed in 5.0.</deprecated>
</service>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Translation\Translator;
use Twig\Extension\ExtensionInterface;
Expand Down Expand Up @@ -48,6 +49,10 @@ public function load(array $configs, ContainerBuilder $container)

if (class_exists(Application::class)) {
$loader->load('console.xml');

if (Kernel::MAJOR_VERSION < 5) {
$container->getDefinition('twig.command.debug')->replaceArgument(5, '%kernel.root_dir%');
}
}

if (class_exists(Mailer::class)) {
Expand Down Expand Up @@ -108,6 +113,10 @@ public function load(array $configs, ContainerBuilder $container)
$container->getDefinition('twig.cache_warmer')->replaceArgument(2, $config['paths']);
$container->getDefinition('twig.template_iterator')->replaceArgument(2, $config['paths']);

if (Kernel::MAJOR_VERSION < 5) {
$container->getDefinition('twig.template_iterator')->replaceArgument(1, '%kernel.root_dir%');
}

foreach ($this->getBundleTemplatePaths($container, $config) as $name => $paths) {
$namespace = $this->normalizeBundleName($name);
foreach ($paths as $path) {
Expand All @@ -120,14 +129,16 @@ public function load(array $configs, ContainerBuilder $container)
}
}

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);
}
if ($container->hasParameter('kernel.root_dir')) {
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]);
$twigFilesystemLoaderDefinition->addMethodCall('addPath', [$dir]);
}
$container->addResource(new FileExistenceResource($dir));
}
$container->addResource(new FileExistenceResource($dir));

if (file_exists($defaultTwigPath)) {
$twigFilesystemLoaderDefinition->addMethodCall('addPath', [$defaultTwigPath]);
Expand Down Expand Up @@ -176,12 +187,14 @@ private function getBundleTemplatePaths(ContainerBuilder $container, array $conf
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);
if ($container->hasParameter('kernel.root_dir')) {
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;
$bundleHierarchy[$name][] = $dir;
}
$container->addResource(new FileExistenceResource($dir));
}
$container->addResource(new FileExistenceResource($dir));

if (file_exists($defaultOverrideBundlePath)) {
$bundleHierarchy[$name][] = $defaultOverrideBundlePath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<argument>%kernel.bundles_metadata%</argument>
<argument>%twig.default_path%</argument>
<argument type="service" id="debug.file_link_formatter" on-invalid="null" />
<argument>%kernel.root_dir%</argument>
<argument />
<tag name="console.command" command="debug:twig" />
</service>

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

<service id="twig.template_iterator" class="Symfony\Bundle\TwigBundle\TemplateIterator">
<argument type="service" id="kernel" />
<argument>%kernel.root_dir%</argument>
<argument />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, we should update TemplateIterator to get this path from $this->kernel->getContainer() to keep BC.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and this argument needs to be deprecated to get rid of it in 5.0.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see how this would solve the BC break. If the parameter isn't present, asking the kernel for the parameter doesn't solve that.

<argument type="collection" /> <!-- Twig paths -->
<argument>%twig.default_path%</argument>
</service>
Expand Down
Loading