8000 Use PHP instead of XML as the prefered service/route configuration in core by fabpot · Pull Request #36778 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Use PHP instead of XML as the prefered service/route configuration in core #36778

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

Merged
merged 1 commit into from
Jun 10, 2020
Merged
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 @@ -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)) {
Copy link
Member

Choose a reason for hiding this comment

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

To be consistent with what is done for XML files, should we also look for tagged iterator arguments ?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, will do when I have an example :)

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)) {
Expand Down
33 changes: 33 additions & 0 deletions src/Symfony/Bundle/TwigBundle/Resources/config/console.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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\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'])
;
};
24 changes: 0 additions & 24 deletions src/Symfony/Bundle/TwigBundle/Resources/config/console.xml

This file was deleted.

29 changes: 29 additions & 0 deletions src/Symfony/Bundle/TwigBundle/Resources/config/form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?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\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')
;
};
22 changes: 0 additions & 22 deletions src/Symfony/Bundle/TwigBundle/Resources/config/form.xml

This file was deleted.

26 changes: 26 additions & 0 deletions src/Symfony/Bundle/TwigBundle/Resources/config/mailer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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\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')])
;
};
18 changes: 0 additions & 18 deletions src/Symfony/Bundle/TwigBundle/Resources/config/mailer.xml

This file was deleted.

163 changes: 163 additions & 0 deletions src/Symfony/Bundle/TwigBundle/Resources/config/twig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
<?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\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')]),
])
;
};
Loading
0