10000 [Twig] Move configuration to PHP · symfony/symfony@238fe2b · GitHub
[go: up one dir, main page]

Skip to content

Commit 238fe2b

Browse files
committed
[Twig] Move configuration to PHP
1 parent 4984ce1 commit 238fe2b

File tree

10 files changed

+268
-223
lines changed

10 files changed

+268
-223
lines changed

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Compiler/UnusedTagsPassUtils.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ public static function getDefinedTags(): array
3737
}
3838
}
3939

40+
// get all tags used in PHP configs
41+
$files = Finder::create()->files()->name('*.php')->path('Resources')->notPath('Tests')->in(\dirname(__DIR__, 5));
42+
foreach ($files as $file) {
43+
$contents = file_get_contents($file);
44+
if (preg_match_all("{->tag\('([^']+)'}", $contents, $matches)) {
45+
foreach ($matches[1] as $match) {
46+
$tags[$match] = true;
47+
}
48+
}
49+
}
50+
4051
// get all tags used in findTaggedServiceIds calls()
4152
$files = Finder::create()->files()->name('*.php')->path('DependencyInjection')->notPath('Tests')->in(\dirname(__DIR__, 5));
4253
foreach ($files as $file) {

src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Symfony\Component\Config\Resource\FileExistenceResource;
1616
use Symfony\Component\Console\Application;
1717
use Symfony\Component\DependencyInjection\ContainerBuilder;
18-
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
18+
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
1919
use Symfony\Component\DependencyInjection\Reference;
2020
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
2121
use Symfony\Component\Mailer\Mailer;
@@ -34,19 +34,19 @@ class TwigExtension extends Extension
3434
{
3535
public function load(array $configs, ContainerBuilder $container)
3636
{
37-
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
38-
$loader->load('twig.xml');
37+
$loader = new PhpFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
38+
$loader->load('twig.php');
3939

4040
if (class_exists('Symfony\Component\Form\Form')) {
41-
$loader->load('form.xml');
41+
$loader->load('form.php');
4242
}
4343

4444
if (class_exists(Application::class)) {
45-
$loader->load('console.xml');
45+
$loader->load('console.php');
4646
}
4747

4848
if (class_exists(Mailer::class)) {
49-
$loader->load('mailer.xml');
49+
$loader->load('mailer.php');
5050
}
5151

5252
if (!class_exists(Translator::class)) {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
13+
14+
use Symfony\Bridge\Twig\Command\DebugCommand;
15+
use Symfony\Bundle\TwigBundle\Command\LintCommand;
16+
17+
return static function (ContainerConfigurator $container) {
18+
$container->services()
19+
->set('twig.command.debug', DebugCommand::class)
20+
->args([
21+
service('twig'),
22+
param('kernel.project_dir'),
23+
param('kernel.bundles_metadata'),
24+
param('twig.default_path'),
25+
service('debug.file_link_formatter')->nullOnInvalid(),
26+
])
27+
->tag('console.command', ['command' => 'debug:twig'])
28+
29+
->set('twig.command.lint', LintCommand::class)
30+
->args([service('twig')])
31+
->tag('console.command', ['command' => 'lint:twig'])
32+
;
33+
};

src/Symfony/Bundle/TwigBundle/Resources/config/console.xml

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
13+
14+
use Symfony\Bridge\Twig\Extension\FormExtension;
15+
use Symfony\Bridge\Twig\Form\TwigRendererEngine;
16+
use Symfony\Component\Form\FormRenderer;
17+
18+
return static function (ContainerConfigurator $container) {
19+
$container->services()
20+
->set('twig.extension.form', FormExtension::class)
21+
22+
->set('twig.form.engine', TwigRendererEngine::class)
23+
->args([param('twig.form.resources'), service('twig')])
24+
25+
->set('twig.form.renderer', FormRenderer::class)
26+
->args([service('twig.form.engine'), service('security.csrf.token_manager')->nullOnInvalid()])
27+
->tag('twig.runtime')
28+
;
29+
};

src/Symfony/Bundle/TwigBundle/Resources/config/form.xml

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
13+
14+
use Symfony\Bridge\Twig\Mime\BodyRenderer;
15+
use Symfony\Component\Mailer\EventListener\MessageListener;
16+
17+
return static function (ContainerConfigurator $container) {
18+
$container->services()
19+
->set('twig.mailer.message_listener', MessageListener::class)
20+
->args([null, service('twig.mime_body_renderer')])
21+
->tag('kernel.event_subscriber')
22+
23+
->set('twig.mime_body_renderer', BodyRenderer::class)
24+
->args([service('twig')])
25+
;
26+
};

src/Symfony/Bundle/TwigBundle/Resources/config/mailer.xml

Lines changed: 0 additions & 18 deletions
< EED3 /div>
This file was deleted.
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
13+
14+
use Psr\Container\ContainerInterface;
15+
use Symfony\Bridge\Twig\AppVariable;
16+
use Symfony\Bridge\Twig\DataCollector\TwigDataCollector;
17+
use Symfony\Bridge\Twig\ErrorRenderer\TwigErrorRenderer;
18+
use Symfony\Bridge\Twig\Extension\AssetExtension;
19+
use Symfony\Bridge\Twig\Extension\CodeExtension;
20+
use Symfony\Bridge\Twig\Extension\ExpressionExtension;
21+
use Symfony\Bridge\Twig\Extension\HttpFoundationExtension;
22+
use Symfony\Bridge\Twig\Extension\HttpKernelExtension;
23+
use Symfony\Bridge\Twig\Extension\HttpKernelRuntime;
24+
use Symfony\Bridge\Twig\Extension\ProfilerExtension;
25+
use Symfony\Bridge\Twig\Extension\RoutingExtension;
26+
use Symfony\Bridge\Twig\Extension\StopwatchExtension;
27+
use Symfony\Bridge\Twig\Extension\TranslationExtension;
28+
use Symfony\Bridge\Twig\Extension\WebLinkExtension;
29+
use Symfony\Bridge\Twig\Extension\WorkflowExtension;
30+
use Symfony\Bridge\Twig\Extension\YamlExtension;
31+
use Symfony\Bridge\Twig\Translation\TwigExtractor;
32+
use Symfony\Bundle\TwigBundle\CacheWarmer\TemplateCacheWarmer;
33+
use Symfony\Bundle\TwigBundle\DependencyInjection\Configurator\EnvironmentConfigurator;
34+
use Symfony\Bundle\TwigBundle\TemplateIterator;
35+
use Twig\Cache\FilesystemCache;
36+
use Twig\Environment;
37+
use Twig\Extension\CoreExtension;
38+
use Twig\Extension\DebugExtension;
39+
use Twig\Extension\EscaperExtension;
40+
use Twig\Extension\OptimizerExtension;
41+
use Twig\Extension\StagingExtension;
42+
use Twig\ExtensionSet;
43+
use Twig\Loader\ChainLoader;
44+
use Twig\Loader\FilesystemLoader;
45+
use Twig\Profiler\Profile;
46+
use Twig\RuntimeLoader\ContainerRuntimeLoader;
47+
use Twig\Template;
48+
use Twig\TemplateWrapper;
49+
50+
return static function (ContainerConfigurator $container) {
51+
$container->services()
52+
->set('twig', Environment::class)
53+
->public()
54+
->args([service('twig.loader'), abstract_arg('Twig options')])
55+
->call('addGlobal', ['app', service('twig.app_variable')])
56+
->call('addRuntimeLoader', [service('twig.runtime_loader')])
57+
->configurator([service('twig.configurator.environment'), 'configure'])
58+
->tag('container.preload', ['class' => FilesystemCache::class])
59+
->tag('container.preload', ['class' => CoreExtension::class])
60+
->tag('container.preload', ['class' => EscaperExtension::class])
61+
->tag('container.preload', ['class' => OptimizerExtension::class])
62+
->tag('container.preload', ['class' => StagingExtension::class])
63+
->tag('container.preload', ['class' => ExtensionSet::class])
64+
->tag('container.preload', ['class' => Template::class])
65+
->tag('container.preload', ['class' => TemplateWrapper::class])
66+
67+
->alias('Twig_Environment', 'twig')
68+
->alias(Environment::class, 'twig')
69+
70+
->set('twig.app_variable', AppVariable::class)
71+
->call('setEnvironment', [param('kernel.environment')])
72+
->call('setDebug', [param('kernel.debug')])
73+
->call('setTokenStorage', [service('security.token_storage')->ignoreOnInvalid()])
74+
->call('setRequestStack', [service('request_stack')->ignoreOnInvalid()])
75+
76+
->set('twig.template_iterator', TemplateIterator::class)
77+
->args([service('kernel'), abstract_arg('Twig paths'), param('twig.default_path')])
78+
79+
->set('twig.template_cache_warmer', TemplateCacheWarmer::class)
80+
->args([service(ContainerInterface::class), service('twig.template_iterator')])
81+
->tag('kernel.cache_warmer')
82+
->tag('container.service_subscriber', ['id' => 'twig'])
83+
84+
->set('twig.loader.native_filesystem', FilesystemLoader::class)
85+
->args([[], param('kernel.project_dir')])
86+
->tag('twig.loader')
87+
88+
->set('twig.loader.chain', ChainLoader::class)
89+
90+
->set('twig.extension.profiler', ProfilerExtension::class)
91+
->args([service('twig.profile'), service('debug.stopwatch')->ignoreOnInvalid()])
92+
93+
->set('twig.profile', Profile::class)
94+
95+
->set('data_collector.twig', TwigDataCollector::class)
96+
->args([service('twig.profile'), service('twig')])
97+
->tag('data_collector', ['template' => '@WebProfiler/Collector/twig.html.twig', 'id' => 'twig', 'priority' => 257])
98+
99+
->set('twig.extension.trans', TranslationExtension::class)
100+
->args([service('translation')->nullOnInvalid()])
101+
->tag('twig.extension')
102+
103+
->set('twig.extension.assets', AssetExtension::class)
104+
->args([service('assets.packages')])
105+
106+
->set('twig.extension.code', CodeExtension::class)
107+
->args([service('debug.file_link_formatter')->ignoreOnInvalid(), param('kernel.project_dir'), param('kernel.charset')])
108+
->tag('twig.extension')
109+
110+
->set('twig.extension.routing', RoutingExtension::class)
111+
->args([service('router')])
112+
113+
->set('twig.extension.yaml', YamlExtension::class)
114+
115+
->set('twig.extension.debug.stopwatch', StopwatchExtension::class)
116+
->args([service('debug.stopwatch')->ignoreOnInvalid(), param('kernel.debug')])
117+
118+
->set('twig.extension.expression', ExpressionExtension::class)
119+
120+
->set('twig.extension.httpkernel', HttpKernelExtension::class)
121+
122+
->set('twig.runtime.httpkernel', HttpKernelRuntime::class)
123+
->args([service('fragment.handler')])
124+
125+
->set('twig.extension.httpfoundation', HttpFoundationExtension::class)
126+
->args([service('url_helper')])
127+
128+
->set('twig.extension.debug', DebugExtension::class)
129+
130+
->set('twig.extension.weblink', WebLinkExtension::class)
131+
->args([service('request_stack')])
132+
133+
->set('twig.translation.extractor', TwigExtractor::class)
134+
->args([service('twig')])
135+
->tag('translation.extractor', ['alias' => 'twig'])
136+
137+
->set('workflow.twig_extension', WorkflowExtension::class)
138+
->args([service('workflow.registry')])
139+
140+
->set('twig.configurator.environment', EnvironmentConfigurator::class)
141+
->args([
142+
abstract_arg('date format, set in TwigExtension'),
143+
abstract_arg('interval format, set in TwigExtension'),
144+
abstract_arg('timezone, set in TwigExtension'),
145+
abstract_arg('decimals, set in TwigExtension'),
146+
abstract_arg('decimal point, set in TwigExtension'),
147+
abstract_arg('thousands separator, set in TwigExtension'),
148+
])
149+
150+
->set('twig.runtime_loader', ContainerRuntimeLoader::class)
151+
->args([abstract_arg('runtime locator')])
152+
153+
->set('twig.error_renderer.html', TwigErrorRenderer::class)
154+
->decorate('error_renderer.html')
155+
->args([
156+
service('twig'),
157+
service('twig.error_renderer.html.inner'),
158+
inline_service(TwigErrorRenderer::class)
159+
->factory([TwigErrorRenderer::class, 'isDebug'])
160+
->args([service('request_stack'), param('kernel.debug')]),
161+
])
162+
;
163+
};

0 commit comments

Comments
 (0)
0