From 0aedd54fc1dce3c74fb602a50b1ea564deba21c7 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 10 May 2020 12:35:43 +0200 Subject: [PATCH] [Twig] Move configuration to PHP --- .../Compiler/UnusedTagsPassUtils.php | 11 ++ .../DependencyInjection/TwigExtension.php | 12 +- .../TwigBundle/Resources/config/console.php | 33 ++++ .../TwigBundle/Resources/config/console.xml | 24 --- .../TwigBundle/Resources/config/form.php | 29 ++++ .../TwigBundle/Resources/config/form.xml | 22 --- .../TwigBundle/Resources/config/mailer.php | 26 +++ .../TwigBundle/Resources/config/mailer.xml | 18 -- .../TwigBundle/Resources/config/twig.php | 163 ++++++++++++++++++ .../TwigBundle/Resources/config/twig.xml | 153 ---------------- src/Symfony/Bundle/TwigBundle/composer.json | 4 +- 11 files changed, 270 insertions(+), 225 deletions(-) create mode 100644 src/Symfony/Bundle/TwigBundle/Resources/config/console.php delete mode 100644 src/Symfony/Bundle/TwigBundle/Resources/config/console.xml create mode 100644 src/Symfony/Bundle/TwigBundle/Resources/config/form.php delete mode 100644 src/Symfony/Bundle/TwigBundle/Resources/config/form.xml create mode 100644 src/Symfony/Bundle/TwigBundle/Resources/config/mailer.php delete mode 100644 src/Symfony/Bundle/TwigBundle/Resources/config/mailer.xml create mode 100644 src/Symfony/Bundle/TwigBundle/Resources/config/twig.php delete mode 100644 src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/UnusedTagsPassUtils.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/UnusedTagsPassUtils.php index 67c97263ccdfd..eee7a64533c22 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/UnusedTagsPassUtils.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/UnusedTagsPassUtils.php @@ -37,6 +37,17 @@ public static function getDefinedTags(): array } } + // get all tags used in PHP configs + $files = Finder::create()->files()->name('*.php')->path('Resources')->notPath('Tests')->in(\dirname(__DIR__, 5)); + foreach ($files as $file) { + $contents = file_get_contents($file); + if (preg_match_all("{->tag\('([^']+)'}", $contents, $matches)) { + foreach ($matches[1] as $match) { + $tags[$match] = true; + } + } + } + // get all tags used in findTaggedServiceIds calls() $files = Finder::create()->files()->name('*.php')->path('DependencyInjection')->notPath('Tests')->in(\dirname(__DIR__, 5)); foreach ($files as $file) { diff --git a/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php b/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php index a70fd31afe3f8..13826834c02be 100644 --- a/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php +++ b/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php @@ -15,7 +15,7 @@ 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\HttpKernel\DependencyInjection\Extension; use Symfony\Component\Mailer\Mailer; @@ -34,19 +34,19 @@ class TwigExtension extends Extension { public function load(array $configs, ContainerBuilder $container) { - $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); - $loader->load('twig.xml'); + $loader = new PhpFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); + $loader->load('twig.php'); if (class_exists('Symfony\Component\Form\Form')) { - $loader->load('form.xml'); + $loader->load('form.php'); } if (class_exists(Application::class)) { - $loader->load('console.xml'); + $loader->load('console.php'); } if (class_exists(Mailer::class)) { - $loader->load('mailer.xml'); + $loader->load('mailer.php'); } if (!class_exists(Translator::class)) { diff --git a/src/Symfony/Bundle/TwigBundle/Resources/config/console.php b/src/Symfony/Bundle/TwigBundle/Resources/config/console.php new file mode 100644 index 0000000000000..9abd75da19ffc --- /dev/null +++ b/src/Symfony/Bundle/TwigBundle/Resources/config/console.php @@ -0,0 +1,33 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\DependencyInjection\Loader\Configurator; + +use Symfony\Bridge\Twig\Command\DebugCommand; +use Symfony\Bundle\TwigBundle\Command\LintCommand; + +return static function (ContainerConfigurator $container) { + $container->services() + ->set('twig.command.debug', DebugCommand::class) + ->args([ + service('twig'), + param('kernel.project_dir'), + param('kernel.bundles_metadata'), + param('twig.default_path'), + service('debug.file_link_formatter')->nullOnInvalid(), + ]) + ->tag('console.command', ['command' => 'debug:twig']) + + ->set('twig.command.lint', LintCommand::class) + ->args([service('twig')]) + ->tag('console.command', ['command' => 'lint:twig']) + ; +}; diff --git a/src/Symfony/Bundle/TwigBundle/Resources/config/console.xml b/src/Symfony/Bundle/TwigBundle/Resources/config/console.xml deleted file mode 100644 index 68afbcc30f2c6..0000000000000 --- a/src/Symfony/Bundle/TwigBundle/Resources/config/console.xml +++ /dev/null @@ -1,24 +0,0 @@ - - - - - - - - - - %kernel.project_dir% - %kernel.bundles_metadata% - %twig.default_path% - - - - - - - - - - diff --git a/src/Symfony/Bundle/TwigBundle/Resources/config/form.php b/src/Symfony/Bundle/TwigBundle/Resources/config/form.php new file mode 100644 index 0000000000000..bbc1b51a9c296 --- /dev/null +++ b/src/Symfony/Bundle/TwigBundle/Resources/config/form.php @@ -0,0 +1,29 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\DependencyInjection\Loader\Configurator; + +use Symfony\Bridge\Twig\Extension\FormExtension; +use Symfony\Bridge\Twig\Form\TwigRendererEngine; +use Symfony\Component\Form\FormRenderer; + +return static function (ContainerConfigurator $container) { + $container->services() + ->set('twig.extension.form', FormExtension::class) + + ->set('twig.form.engine', TwigRendererEngine::class) + ->args([param('twig.form.resources'), service('twig')]) + + ->set('twig.form.renderer', FormRenderer::class) + ->args([service('twig.form.engine'), service('security.csrf.token_manager')->nullOnInvalid()]) + ->tag('twig.runtime') + ; +}; diff --git a/src/Symfony/Bundle/TwigBundle/Resources/config/form.xml b/src/Symfony/Bundle/TwigBundle/Resources/config/form.xml deleted file mode 100644 index 8fe29572c687c..0000000000000 --- a/src/Symfony/Bundle/TwigBundle/Resources/config/form.xml +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - - - - %twig.form.resources% - - - - - - - - - - diff --git a/src/Symfony/Bundle/TwigBundle/Resources/config/mailer.php b/src/Symfony/Bundle/TwigBundle/Resources/config/mailer.php new file mode 100644 index 0000000000000..1444481f2c0ba --- /dev/null +++ b/src/Symfony/Bundle/TwigBundle/Resources/config/mailer.php @@ -0,0 +1,26 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\DependencyInjection\Loader\Configurator; + +use Symfony\Bridge\Twig\Mime\BodyRenderer; +use Symfony\Component\Mailer\EventListener\MessageListener; + +return static function (ContainerConfigurator $container) { + $container->services() + ->set('twig.mailer.message_listener', MessageListener::class) + ->args([null, service('twig.mime_body_renderer')]) + ->tag('kernel.event_subscriber') + + ->set('twig.mime_body_renderer', BodyRenderer::class) + ->args([service('twig')]) + ; +}; diff --git a/src/Symfony/Bundle/TwigBundle/Resources/config/mailer.xml b/src/Symfony/Bundle/TwigBundle/Resources/config/mailer.xml deleted file mode 100644 index 0e425952ffe59..0000000000000 --- a/src/Symfony/Bundle/TwigBundle/Resources/config/mailer.xml +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - - null - - - - - - - - - diff --git a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.php b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.php new file mode 100644 index 0000000000000..9dd13f7a038e6 --- /dev/null +++ b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.php @@ -0,0 +1,163 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\DependencyInjection\Loader\Configurator; + +use Psr\Container\ContainerInterface; +use Symfony\Bridge\Twig\AppVariable; +use Symfony\Bridge\Twig\DataCollector\TwigDataCollector; +use Symfony\Bridge\Twig\ErrorRenderer\TwigErrorRenderer; +use Symfony\Bridge\Twig\Extension\AssetExtension; +use Symfony\Bridge\Twig\Extension\CodeExtension; +use Symfony\Bridge\Twig\Extension\ExpressionExtension; +use Symfony\Bridge\Twig\Extension\HttpFoundationExtension; +use Symfony\Bridge\Twig\Extension\HttpKernelExtension; +use Symfony\Bridge\Twig\Extension\HttpKernelRuntime; +use Symfony\Bridge\Twig\Extension\ProfilerExtension; +use Symfony\Bridge\Twig\Extension\RoutingExtension; +use Symfony\Bridge\Twig\Extension\StopwatchExtension; +use Symfony\Bridge\Twig\Extension\TranslationExtension; +use Symfony\Bridge\Twig\Extension\WebLinkExtension; +use Symfony\Bridge\Twig\Extension\WorkflowExtension; +use Symfony\Bridge\Twig\Extension\YamlExtension; +use Symfony\Bridge\Twig\Translation\TwigExtractor; +use Symfony\Bundle\TwigBundle\CacheWarmer\TemplateCacheWarmer; +use Symfony\Bundle\TwigBundle\DependencyInjection\Configurator\EnvironmentConfigurator; +use Symfony\Bundle\TwigBundle\TemplateIterator; +use Twig\Cache\FilesystemCache; +use Twig\Environment; +use Twig\Extension\CoreExtension; +use Twig\Extension\DebugExtension; +use Twig\Extension\EscaperExtension; +use Twig\Extension\OptimizerExtension; +use Twig\Extension\StagingExtension; +use Twig\ExtensionSet; +use Twig\Loader\ChainLoader; +use Twig\Loader\FilesystemLoader; +use Twig\Profiler\Profile; +use Twig\RuntimeLoader\ContainerRuntimeLoader; +use Twig\Template; +use Twig\TemplateWrapper; + +return static function (ContainerConfigurator $container) { + $container->services() + ->set('twig', Environment::class) + ->public() + ->args([service('twig.loader'), abstract_arg('Twig options')]) + ->call('addGlobal', ['app', service('twig.app_variable')]) + ->call('addRuntimeLoader', [service('twig.runtime_loader')]) + ->configurator([service('twig.configurator.environment'), 'configure']) + ->tag('container.preload', ['class' => FilesystemCache::class]) + ->tag('container.preload', ['class' => CoreExtension::class]) + ->tag('container.preload', ['class' => EscaperExtension::class]) + ->tag('container.preload', ['class' => OptimizerExtension::class]) + ->tag('container.preload', ['class' => StagingExtension::class]) + ->tag('container.preload', ['class' => ExtensionSet::class]) + ->tag('container.preload', ['class' => Template::class]) + ->tag('container.preload', ['class' => TemplateWrapper::class]) + + ->alias('Twig_Environment', 'twig') + ->alias(Environment::class, 'twig') + + ->set('twig.app_variable', AppVariable::class) + ->call('setEnvironment', [param('kernel.environment')]) + ->call('setDebug', [param('kernel.debug')]) + ->call('setTokenStorage', [service('security.token_storage')->ignoreOnInvalid()]) + ->call('setRequestStack', [service('request_stack')->ignoreOnInvalid()]) + + ->set('twig.template_iterator', TemplateIterator::class) + ->args([service('kernel'), abstract_arg('Twig paths'), param('twig.default_path')]) + + ->set('twig.template_cache_warmer', TemplateCacheWarmer::class) + ->args([service(ContainerInterface::class), service('twig.template_iterator')]) + ->tag('kernel.cache_warmer') + ->tag('container.service_subscriber', ['id' => 'twig']) + + ->set('twig.loader.native_filesystem', FilesystemLoader::class) + ->args([[], param('kernel.project_dir')]) + ->tag('twig.loader') + + ->set('twig.loader.chain', ChainLoader::class) + + ->set('twig.extension.profiler', ProfilerExtension::class) + ->args([service('twig.profile'), service('debug.stopwatch')->ignoreOnInvalid()]) + + ->set('twig.profile', Profile::class) + + ->set('data_collector.twig', TwigDataCollector::class) + ->args([service('twig.profile'), service('twig')]) + ->tag('data_collector', ['template' => '@WebProfiler/Collector/twig.html.twig', 'id' => 'twig', 'priority' => 257]) + + ->set('twig.extension.trans', TranslationExtension::class) + ->args([service('translation')->nullOnInvalid()]) + ->tag('twig.extension') + + ->set('twig.extension.assets', AssetExtension::class) + ->args([service('assets.packages')]) + + ->set('twig.extension.code', CodeExtension::class) + ->args([service('debug.file_link_formatter')->ignoreOnInvalid(), param('kernel.project_dir'), param('kernel.charset')]) + ->tag('twig.extension') + + ->set('twig.extension.routing', RoutingExtension::class) + ->args([service('router')]) + + ->set('twig.extension.yaml', YamlExtension::class) + + ->set('twig.extension.debug.stopwatch', StopwatchExtension::class) + ->args([service('debug.stopwatch')->ignoreOnInvalid(), param('kernel.debug')]) + + ->set('twig.extension.expression', ExpressionExtension::class) + + ->set('twig.extension.httpkernel', HttpKernelExtension::class) + + ->set('twig.runtime.httpkernel', HttpKernelRuntime::class) + ->args([service('fragment.handler')]) + + ->set('twig.extension.httpfoundation', HttpFoundationExtension::class) + ->args([service('url_helper')]) + + ->set('twig.extension.debug', DebugExtension::class) + + ->set('twig.extension.weblink', WebLinkExtension::class) + ->args([service('request_stack')]) + + ->set('twig.translation.extractor', TwigExtractor::class) + ->args([service('twig')]) + ->tag('translation.extractor', ['alias' => 'twig']) + + ->set('workflow.twig_extension', WorkflowExtension::class) + ->args([service('workflow.registry')]) + + ->set('twig.configurator.environment', EnvironmentConfigurator::class) + ->args([ + abstract_arg('date format, set in TwigExtension'), + abstract_arg('interval format, set in TwigExtension'), + abstract_arg('timezone, set in TwigExtension'), + abstract_arg('decimals, set in TwigExtension'), + abstract_arg('decimal point, set in TwigExtension'), + abstract_arg('thousands separator, set in TwigExtension'), + ]) + + ->set('twig.runtime_loader', ContainerRuntimeLoader::class) + ->args([abstract_arg('runtime locator')]) + + ->set('twig.error_renderer.html', TwigErrorRenderer::class) + ->decorate('error_renderer.html') + ->args([ + service('twig'), + service('twig.error_renderer.html.inner'), + inline_service(TwigErrorRenderer::class) + ->factory([TwigErrorRenderer::class, 'isDebug']) + ->args([service('request_stack'), param('kernel.debug')]), + ]) + ; +}; diff --git a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml deleted file mode 100644 index cb30219365e4e..0000000000000 --- a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml +++ /dev/null @@ -1,153 +0,0 @@ - - - - - - - - - - - - app - - - - - - - - - - - - - - - - - - - - %kernel.environment% - %kernel.debug% - - - - - - - - %twig.default_path% - - - - - - - - - - - - %kernel.project_dir% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - %kernel.project_dir% - %kernel.charset% - - - - - - - - - - - %kernel.debug% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - %kernel.debug% - - - - - diff --git a/src/Symfony/Bundle/TwigBundle/composer.json b/src/Symfony/Bundle/TwigBundle/composer.json index 6ce9127bad46d..d03cd5c73ba33 100644 --- a/src/Symfony/Bundle/TwigBundle/composer.json +++ b/src/Symfony/Bundle/TwigBundle/composer.json @@ -27,7 +27,7 @@ "require-dev": { "symfony/asset": "^4.4|^5.0", "symfony/stopwatch": "^4.4|^5.0", - "symfony/dependency-injection": "^4.4|^5.0", + "symfony/dependency-injection": "^5.2", "symfony/expression-language": "^4.4|^5.0", "symfony/finder": "^4.4|^5.0", "symfony/form": "^4.4|^5.0", @@ -40,7 +40,7 @@ "doctrine/cache": "~1.0" }, "conflict": { - "symfony/dependency-injection": "<4.4", + "symfony/dependency-injection": "<5.2", "symfony/framework-bundle": "<5.0", "symfony/translation": "<5.0" },