migration symfony 5 4 (#300)

* symfony 5.4 (diff dev)

* symfony 5.4 (working)

* symfony 5.4 (update autoload)

* symfony 5.4 (remove swiftmailer mailer implementation)

* symfony 5.4 (php doc and split Global accessor class)


### Impacted packages:

composer require php:">=7.2.5 <8.0.0" symfony/console:5.4.* symfony/dotenv:5.4.* symfony/framework-bundle:5.4.* symfony/twig-bundle:5.4.* symfony/yaml:5.4.* --update-with-dependencies

composer require symfony/stopwatch:5.4.* symfony/web-profiler-bundle:5.4.* --dev --update-with-dependencies
This commit is contained in:
bdalsass
2022-06-16 09:13:24 +02:00
committed by GitHub
parent abb13b70b9
commit 79da71ecf8
2178 changed files with 87439 additions and 59451 deletions

View File

@@ -1,40 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\TwigBundle\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
/**
* Registers the Twig exception listener if Twig is registered as a templating engine.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class ExceptionListenerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if (false === $container->hasDefinition('twig')) {
return;
}
// register the exception controller only if Twig is enabled and required dependencies do exist
if (!class_exists('Symfony\Component\Debug\Exception\FlattenException') || !interface_exists('Symfony\Component\EventDispatcher\EventSubscriberInterface')) {
$container->removeDefinition('twig.exception_listener');
} elseif ($container->hasParameter('templating.engines')) {
$engines = $container->getParameter('templating.engines');
if (!\in_array('twig', $engines)) {
$container->removeDefinition('twig.exception_listener');
}
}
}
}

View File

@@ -11,11 +11,14 @@
namespace Symfony\Bundle\TwigBundle\DependencyInjection\Compiler;
use Symfony\Component\Asset\Packages;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\ExpressionLanguage\Expression;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Workflow\Workflow;
use Symfony\Component\Yaml\Yaml;
/**
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
@@ -24,40 +27,44 @@ class ExtensionPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if (!class_exists('Symfony\Component\Asset\Packages')) {
if (!class_exists(Packages::class)) {
$container->removeDefinition('twig.extension.assets');
}
if (!class_exists('Symfony\Component\ExpressionLanguage\Expression')) {
if (!class_exists(Expression::class)) {
$container->removeDefinition('twig.extension.expression');
}
if (!interface_exists('Symfony\Component\Routing\Generator\UrlGeneratorInterface')) {
if (!interface_exists(UrlGeneratorInterface::class)) {
$container->removeDefinition('twig.extension.routing');
}
if (!class_exists('Symfony\Component\Yaml\Yaml')) {
if (!class_exists(Yaml::class)) {
$container->removeDefinition('twig.extension.yaml');
}
$viewDir = \dirname((new \ReflectionClass(\Symfony\Bridge\Twig\Extension\FormExtension::class))->getFileName(), 2).'/Resources/views';
$templateIterator = $container->getDefinition('twig.template_iterator');
$templatePaths = $templateIterator->getArgument(1);
$loader = $container->getDefinition('twig.loader.native_filesystem');
if ($container->has('mailer')) {
$emailPath = $viewDir.'/Email';
$loader->addMethodCall('addPath', [$emailPath, 'email']);
$loader->addMethodCall('addPath', [$emailPath, '!email']);
$templatePaths[$emailPath] = 'email';
}
if ($container->has('form.extension')) {
$container->getDefinition('twig.extension.form')->addTag('twig.extension');
$reflClass = new \ReflectionClass('Symfony\Bridge\Twig\Extension\FormExtension');
$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[$coreThemePath] = null;
$container->getDefinition('twig.template_iterator')->replaceArgument(2, $paths);
if ($container->hasDefinition('twig.cache_warmer')) {
$paths = $container->getDefinition('twig.cache_warmer')->getArgument(2);
$paths[$coreThemePath] = null;
$container->getDefinition('twig.cache_warmer')->replaceArgument(2, $paths);
}
$coreThemePath = $viewDir.'/Form';
$loader->addMethodCall('addPath', [$coreThemePath]);
$templatePaths[$coreThemePath] = null;
}
$templateIterator->replaceArgument(1, $templatePaths);
if ($container->has('router')) {
$container->getDefinition('twig.extension.routing')->addTag('twig.extension');
}
@@ -66,19 +73,13 @@ class ExtensionPass implements CompilerPassInterface
$container->getDefinition('twig.extension.httpkernel')->addTag('twig.extension');
$container->getDefinition('twig.runtime.httpkernel')->addTag('twig.runtime');
// inject Twig in the hinclude service if Twig is the only registered templating engine
if ((!$container->hasParameter('templating.engines') || ['twig'] == $container->getParameter('templating.engines')) && $container->hasDefinition('fragment.renderer.hinclude')) {
if ($container->hasDefinition('fragment.renderer.hinclude')) {
$container->getDefinition('fragment.renderer.hinclude')
->addTag('kernel.fragment_renderer', ['alias' => 'hinclude'])
->replaceArgument(0, new Reference('twig'))
;
}
}
if (!$container->has('http_kernel')) {
$container->removeDefinition('twig.controller.preview_error');
}
if ($container->has('request_stack')) {
$container->getDefinition('twig.extension.httpfoundation')->addTag('twig.extension');
}
@@ -96,16 +97,7 @@ class ExtensionPass implements CompilerPassInterface
$container->getDefinition('twig.extension.weblink')->addTag('twig.extension');
}
$twigLoader = $container->getDefinition('twig.loader.native_filesystem');
if ($container->has('templating')) {
$loader = $container->getDefinition('twig.loader.filesystem');
$loader->setMethodCalls(array_merge($twigLoader->getMethodCalls(), $loader->getMethodCalls()));
$twigLoader->clearTag('twig.loader');
} else {
$container->setAlias('twig.loader.filesystem', new Alias('twig.loader.native_filesystem', false));
$container->removeDefinition('templating.engine.twig');
}
$container->setAlias('twig.loader.filesystem', new Alias('twig.loader.native_filesystem', false));
if ($container->has('assets.packages')) {
$container->getDefinition('twig.extension.assets')->addTag('twig.extension');
@@ -115,7 +107,7 @@ class ExtensionPass implements CompilerPassInterface
$container->getDefinition('twig.extension.yaml')->addTag('twig.extension');
}
if (class_exists('Symfony\Component\Stopwatch\Stopwatch')) {
if (class_exists(\Symfony\Component\Stopwatch\Stopwatch::class)) {
$container->getDefinition('twig.extension.debug.stopwatch')->addTag('twig.extension');
}
@@ -128,5 +120,10 @@ class ExtensionPass implements CompilerPassInterface
} else {
$container->getDefinition('workflow.twig_extension')->addTag('twig.extension');
}
if ($container->has('serializer')) {
$container->getDefinition('twig.runtime.serializer')->addTag('twig.runtime');
$container->getDefinition('twig.extension.serializer')->addTag('twig.extension');
}
}
}

View File

@@ -12,8 +12,8 @@
namespace Symfony\Bundle\TwigBundle\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
/**
* Adds tagged twig.extension services to twig service.
@@ -22,6 +22,8 @@ use Symfony\Component\DependencyInjection\Reference;
*/
class TwigEnvironmentPass implements CompilerPassInterface
{
use PriorityTaggedServiceTrait;
public function process(ContainerBuilder $container)
{
if (false === $container->hasDefinition('twig')) {
@@ -37,11 +39,11 @@ class TwigEnvironmentPass implements CompilerPassInterface
$currentMethodCalls = $definition->getMethodCalls();
$twigBridgeExtensionsMethodCalls = [];
$othersExtensionsMethodCalls = [];
foreach ($container->findTaggedServiceIds('twig.extension', true) as $id => $attributes) {
$methodCall = ['addExtension', [new Reference($id)]];
$extensionClass = $container->getDefinition($id)->getClass();
foreach ($this->findAndSortTaggedServices('twig.extension', $container) as $extension) {
$methodCall = ['addExtension', [$extension]];
$extensionClass = $container->getDefinition((string) $extension)->getClass();
if (\is_string($extensionClass) && 0 === strpos($extensionClass, 'Symfony\Bridge\Twig\Extension')) {
if (\is_string($extensionClass) && str_starts_with($extensionClass, 'Symfony\Bridge\Twig\Extension')) {
$twigBridgeExtensionsMethodCalls[] = $methodCall;
} else {
$othersExtensionsMethodCalls[] = $methodCall;

View File

@@ -33,7 +33,7 @@ class TwigLoaderPass implements CompilerPassInterface
$found = 0;
foreach ($container->findTaggedServiceIds('twig.loader', true) as $id => $attributes) {
$priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0;
$priority = $attributes[0]['priority'] ?? 0;
$prioritizedLoaders[$priority][] = $id;
++$found;
}
@@ -43,7 +43,7 @@ class TwigLoaderPass implements CompilerPassInterface
}
if (1 === $found) {
$container->setAlias('twig.loader', $id)->setPrivate(true);
$container->setAlias('twig.loader', $id);
} else {
$chainLoader = $container->getDefinition('twig.loader.chain');
krsort($prioritizedLoaders);
@@ -54,7 +54,7 @@ class TwigLoaderPass implements CompilerPassInterface
}
}
$container->setAlias('twig.loader', 'twig.loader.chain')->setPrivate(true);
$container->setAlias('twig.loader', 'twig.loader.chain');
}
}
}

View File

@@ -14,6 +14,7 @@ namespace Symfony\Bundle\TwigBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
/**
* TwigExtension configuration structure.
@@ -25,18 +26,25 @@ class Configuration implements ConfigurationInterface
/**
* Generates the configuration tree builder.
*
* @return TreeBuilder The tree builder
* @return TreeBuilder
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('twig');
$treeBuilder = new TreeBuilder('twig');
$rootNode = $treeBuilder->getRootNode();
$rootNode
->children()
->scalarNode('exception_controller')->defaultValue('twig.controller.exception:showAction')->end()
->end()
;
$rootNode->beforeNormalization()
->ifTrue(function ($v) { return \is_array($v) && \array_key_exists('exception_controller', $v); })
->then(function ($v) {
if (isset($v['exception_controller'])) {
throw new InvalidConfigurationException('Option "exception_controller" under "twig" must be null or unset, use "error_controller" under "framework" instead.');
}
unset($v['exception_controller']);
return $v;
})
->end();
$this->addFormThemesSection($rootNode);
$this->addGlobalsSection($rootNode);
@@ -54,7 +62,7 @@ class Configuration implements ConfigurationInterface
->arrayNode('form_themes')
->addDefaultChildrenIfNoneSet()
->prototype('scalar')->defaultValue('form_div_layout.html.twig')->end()
->example(['MyBundle::form.html.twig'])
->example(['@My/form.html.twig'])
->validate()
->ifTrue(function ($v) { return !\in_array('form_div_layout.html.twig', $v); })
->then(function ($v) {
@@ -74,12 +82,13 @@ class Configuration implements ConfigurationInterface
->arrayNode('globals')
->normalizeKeys(false)
->useAttributeAsKey('key')
->example(['foo' => '"@bar"', 'pi' => 3.14])
->example(['foo' => '@bar', 'pi' => 3.14])
->prototype('array')
->normalizeKeys(false)
->beforeNormalization()
->ifTrue(function ($v) { return \is_string($v) && 0 === strpos($v, '@'); })
->ifTrue(function ($v) { return \is_string($v) && str_starts_with($v, '@'); })
->then(function ($v) {
if (0 === strpos($v, '@@')) {
if (str_starts_with($v, '@@')) {
return substr($v, 1);
}
@@ -127,7 +136,7 @@ class Configuration implements ConfigurationInterface
->scalarNode('cache')->defaultValue('%kernel.cache_dir%/twig')->end()
->scalarNode('charset')->defaultValue('%kernel.charset%')->end()
->booleanNode('debug')->defaultValue('%kernel.debug%')->end()
->booleanNode('strict_variables')->end()
->booleanNode('strict_variables')->defaultValue('%kernel.debug%')->end()
->scalarNode('auto_reload')->end()
->integerNode('optimizations')->min(-1)->end()
->scalarNode('default_path')

View File

@@ -15,7 +15,7 @@ use Symfony\Bridge\Twig\UndefinedCallableHandler;
use Twig\Environment;
// BC/FC with namespaced Twig
class_exists('Twig\Environment');
class_exists(Environment::class);
/**
* Twig environment configurator.
@@ -31,7 +31,7 @@ class EnvironmentConfigurator
private $decimalPoint;
private $thousandsSeparator;
public function __construct($dateFormat, $intervalFormat, $timezone, $decimals, $decimalPoint, $thousandsSeparator)
public function __construct(string $dateFormat, string $intervalFormat, ?string $timezone, int $decimals, string $decimalPoint, string $thousandsSeparator)
{
$this->dateFormat = $dateFormat;
$this->intervalFormat = $intervalFormat;

View File

@@ -11,13 +11,19 @@
namespace Symfony\Bundle\TwigBundle\DependencyInjection;
use Composer\InstalledVersions;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Resource\FileExistenceResource;
use Symfony\Component\Console\Application;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Form\AbstractRendererEngine;
use Symfony\Component\Form\Form;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Translation\Translator;
use Symfony\Contracts\Service\ResetInterface;
use Twig\Extension\ExtensionInterface;
use Twig\Extension\RuntimeExtensionInterface;
use Twig\Loader\LoaderInterface;
@@ -32,32 +38,32 @@ class TwigExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('twig.xml');
$container->getDefinition('twig.profile')->setPrivate(true);
$container->getDefinition('twig.translation.extractor')->setPrivate(true);
$container->getDefinition('workflow.twig_extension')->setPrivate(true);
$container->getDefinition('twig.exception_listener')->setPrivate(true);
if ($container->has('fragment.handler')) {
$container->getDefinition('twig.runtime.httpkernel')->setPrivate(true);
if (!class_exists(InstalledVersions::class)) {
trigger_deprecation('symfony/twig-bundle', '5.4', 'Configuring Symfony without the Composer Runtime API is deprecated. Consider upgrading to Composer 2.1 or later.');
}
if (class_exists('Symfony\Component\Form\Form')) {
$loader->load('form.xml');
$container->getDefinition('twig.form.renderer')->setPrivate(true);
$loader = new PhpFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('twig.php');
if ($container::willBeAvailable('symfony/form', Form::class, ['symfony/twig-bundle'], true)) {
$loader->load('form.php');
if (is_subclass_of(AbstractRendererEngine::class, ResetInterface::class)) {
$container->getDefinition('twig.form.engine')->addTag('kernel.reset', [
'method' => 'reset',
]);
}
}
if (interface_exists('Symfony\Component\Templating\EngineInterface')) {
$loader->load('templating.xml');
if ($container::willBeAvailable('symfony/console', Application::class, ['symfony/twig-bundle'], true)) {
$loader->load('console.php');
}
if (class_exists(Application::class)) {
$loader->load('console.xml');
if ($container::willBeAvailable('symfony/mailer', Mailer::class, ['symfony/twig-bundle'], true)) {
$loader->load('mailer.php');
}
if (!interface_exists('Symfony\Component\Translation\TranslatorInterface')) {
if (!$container::willBeAvailable('symfony/translation', Translator::class, ['symfony/twig-bundle'], true)) {
$container->removeDefinition('twig.translation.extractor');
}
@@ -78,10 +84,9 @@ class TwigExtension extends Extension
$config = $this->processConfiguration($configuration, $configs);
$container->setParameter('twig.exception_listener.controller', $config['exception_controller']);
$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']);
@@ -103,41 +108,24 @@ class TwigExtension extends Extension
}
// paths are modified in ExtensionPass if forms are enabled
$container->getDefinition('twig.cache_warmer')->replaceArgument(2, $config['paths']);
$container->getDefinition('twig.template_iterator')->replaceArgument(2, $config['paths']);
$container->getDefinition('twig.template_iterator')->replaceArgument(1, $config['paths']);
$bundleHierarchy = $this->getBundleHierarchy($container, $config);
foreach ($bundleHierarchy as $name => $bundle) {
foreach ($this->getBundleTemplatePaths($container, $config) as $name => $paths) {
$namespace = $this->normalizeBundleName($name);
foreach ($bundle['children'] as $child) {
foreach ($bundleHierarchy[$child]['paths'] as $path) {
$twigFilesystemLoaderDefinition->addMethodCall('addPath', [$path, $namespace]);
}
}
foreach ($bundle['paths'] as $path) {
foreach ($paths as $path) {
$twigFilesystemLoaderDefinition->addMethodCall('addPath', [$path, $namespace]);
}
// add exclusive namespace for root bundles only
// to override a bundle template that also extends itself
if (\count($bundle['paths']) > 0 && 0 === \count($bundle['parents'])) {
if ($paths) {
// the last path must be the bundle views directory
$twigFilesystemLoaderDefinition->addMethodCall('addPath', [end($bundle['paths']), '!'.$namespace]);
$twigFilesystemLoaderDefinition->addMethodCall('addPath', [$path, '!'.$namespace]);
}
}
if (file_exists($dir = $container->getParameter('kernel.root_dir').'/Resources/views')) {
$twigFilesystemLoaderDefinition->addMethodCall('addPath', [$dir]);
if (file_exists($defaultTwigPath)) {
$twigFilesystemLoaderDefinition->addMethodCall('addPath', [$defaultTwigPath]);
}
$container->addResource(new FileExistenceResource($dir));
if (file_exists($dir = $container->getParameterBag()->resolveValue($config['default_path']))) {
$twigFilesystemLoaderDefinition->addMethodCall('addPath', [$dir]);
}
$container->addResource(new FileExistenceResource($dir));
$container->addResource(new FileExistenceResource($defaultTwigPath));
if (!empty($config['globals'])) {
$def = $container->getDefinition('twig');
@@ -172,84 +160,33 @@ class TwigExtension extends Extension
$container->registerForAutoconfiguration(RuntimeExtensionInterface::class)->addTag('twig.runtime');
if (false === $config['cache']) {
$container->removeDefinition('twig.cache_warmer');
$container->removeDefinition('twig.template_cache_warmer');
}
if (\PHP_VERSION_ID < 70000) {
$this->addClassesToCompile([
'Twig_Environment',
'Twig_Extension',
'Twig_Extension_Core',
'Twig_Extension_Escaper',
'Twig_Extension_Optimizer',
'Twig_LoaderInterface',
'Twig_Markup',
'Twig_Template',
]);
}
}
private function getBundleHierarchy(ContainerBuilder $container, array $config)
private function getBundleTemplatePaths(ContainerBuilder $container, array $config): array
{
$bundleHierarchy = [];
foreach ($container->getParameter('kernel.bundles_metadata') as $name => $bundle) {
if (!\array_key_exists($name, $bundleHierarchy)) {
$bundleHierarchy[$name] = [
'paths' => [],
'parents' => [],
'children' => [],
];
}
$defaultOverrideBundlePath = $container->getParameterBag()->resolveValue($config['default_path']).'/bundles/'.$name;
if (file_exists($dir = $container->getParameter('kernel.root_dir').'/Resources/'.$name.'/views')) {
$bundleHierarchy[$name]['paths'][] = $dir;
if (file_exists($defaultOverrideBundlePath)) {
$bundleHierarchy[$name][] = $defaultOverrideBundlePath;
}
$container->addResource(new FileExistenceResource($defaultOverrideBundlePath));
if (file_exists($dir = $bundle['path'].'/Resources/views') || file_exists($dir = $bundle['path'].'/templates')) {
$bundleHierarchy[$name][] = $dir;
}
$container->addResource(new FileExistenceResource($dir));
if (file_exists($dir = $container->getParameterBag()->resolveValue($config['default_path']).'/bundles/'.$name)) {
$bundleHierarchy[$name]['paths'][] = $dir;
}
$container->addResource(new FileExistenceResource($dir));
if (file_exists($dir = $bundle['path'].'/Resources/views')) {
$bundleHierarchy[$name]['paths'][] = $dir;
}
$container->addResource(new FileExistenceResource($dir));
if (!isset($bundle['parent']) || null === $bundle['parent']) {
continue;
}
$bundleHierarchy[$name]['parents'][] = $bundle['parent'];
if (!\array_key_exists($bundle['parent'], $bundleHierarchy)) {
$bundleHierarchy[$bundle['parent']] = [
'paths' => [],
'parents' => [],
'children' => [],
];
}
$bundleHierarchy[$bundle['parent']]['children'] = array_merge($bundleHierarchy[$name]['children'], [$name], $bundleHierarchy[$bundle['parent']]['children']);
foreach ($bundleHierarchy[$bundle['parent']]['parents'] as $parent) {
$bundleHierarchy[$name]['parents'][] = $parent;
$bundleHierarchy[$parent]['children'] = array_merge($bundleHierarchy[$name]['children'], [$name], $bundleHierarchy[$parent]['children']);
}
foreach ($bundleHierarchy[$name]['children'] as $child) {
$bundleHierarchy[$child]['parents'] = array_merge($bundleHierarchy[$child]['parents'], $bundleHierarchy[$name]['parents']);
}
}
return $bundleHierarchy;
}
private function normalizeBundleName($name)
private function normalizeBundleName(string $name): string
{
if ('Bundle' === substr($name, -6)) {
if (str_ends_with($name, 'Bundle')) {
$name = substr($name, 0, -6);
}