initial commit

This commit is contained in:
boris
2025-09-30 09:24:25 +01:00
committed by boris
parent a783a12c97
commit c7770ea03b
4695 changed files with 525784 additions and 0 deletions

View File

@@ -0,0 +1,63 @@
<?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\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Twig\Attribute\AsTwigFilter;
use Twig\Attribute\AsTwigFunction;
use Twig\Attribute\AsTwigTest;
use Twig\Extension\AbstractExtension;
use Twig\Extension\AttributeExtension;
use Twig\Extension\ExtensionInterface;
/**
* Register an instance of AttributeExtension for each service using the
* PHP attributes to declare Twig callables.
*
* @author Jérôme Tamarelle <jerome@tamarelle.net>
*
* @internal
*/
final class AttributeExtensionPass implements CompilerPassInterface
{
private const TAG = 'twig.attribute_extension';
public static function autoconfigureFromAttribute(ChildDefinition $definition, AsTwigFilter|AsTwigFunction|AsTwigTest $attribute, \ReflectionMethod $reflector): void
{
$class = $reflector->getDeclaringClass();
if ($class->implementsInterface(ExtensionInterface::class)) {
if ($class->isSubclassOf(AbstractExtension::class)) {
throw new LogicException(\sprintf('The class "%s" cannot extend "%s" and use the "#[%s]" attribute on method "%s()", choose one or the other.', $class->name, AbstractExtension::class, $attribute::class, $reflector->name));
}
throw new LogicException(\sprintf('The class "%s" cannot implement "%s" and use the "#[%s]" attribute on method "%s()", choose one or the other.', $class->name, ExtensionInterface::class, $attribute::class, $reflector->name));
}
$definition->addTag(self::TAG);
// The service must be tagged as a runtime to call non-static methods
if (!$reflector->isStatic()) {
$definition->addTag('twig.runtime');
}
}
public function process(ContainerBuilder $container): void
{
foreach ($container->findTaggedServiceIds(self::TAG, true) as $id => $tags) {
$container->register('.twig.extension.'.$id, AttributeExtension::class)
->setArguments([$container->getDefinition($id)->getClass()])
->addTag('twig.extension');
}
}
}

View File

@@ -0,0 +1,148 @@
<?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\Asset\Packages;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Emoji\EmojiTransliterator;
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>
*/
class ExtensionPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
if (!class_exists(Packages::class)) {
$container->removeDefinition('twig.extension.assets');
}
if (!class_exists(\Transliterator::class) || !class_exists(EmojiTransliterator::class)) {
$container->removeDefinition('twig.extension.emoji');
}
if (!class_exists(Expression::class)) {
$container->removeDefinition('twig.extension.expression');
}
if (!interface_exists(UrlGeneratorInterface::class)) {
$container->removeDefinition('twig.extension.routing');
}
if (!class_exists(Yaml::class)) {
$container->removeDefinition('twig.extension.yaml');
}
if (!$container->has('asset_mapper')) {
// edge case where AssetMapper is installed, but not enabled
$container->removeDefinition('twig.extension.importmap');
$container->removeDefinition('twig.runtime.importmap');
}
$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');
$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');
}
if ($container->has('html_sanitizer')) {
$container->getDefinition('twig.extension.htmlsanitizer')->addTag('twig.extension');
}
if ($container->has('fragment.handler')) {
$container->getDefinition('twig.extension.httpkernel')->addTag('twig.extension');
$container->getDefinition('twig.runtime.httpkernel')->addTag('twig.runtime');
if ($container->hasDefinition('fragment.renderer.hinclude')) {
$container->getDefinition('fragment.renderer.hinclude')
->addTag('kernel.fragment_renderer', ['alias' => 'hinclude'])
;
}
}
if ($container->has('request_stack')) {
$container->getDefinition('twig.extension.httpfoundation')->addTag('twig.extension');
}
if ($container->getParameter('kernel.debug')) {
$container->getDefinition('twig.extension.profiler')->addTag('twig.extension');
// only register if the improved version from DebugBundle is *not* present
if (!$container->has('twig.extension.dump')) {
$container->getDefinition('twig.extension.debug')->addTag('twig.extension');
}
}
if ($container->has('web_link.add_link_header_listener')) {
$container->getDefinition('twig.extension.weblink')->addTag('twig.extension');
}
$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');
}
if ($container->hasDefinition('twig.extension.yaml')) {
$container->getDefinition('twig.extension.yaml')->addTag('twig.extension');
}
if (class_exists(\Symfony\Component\Stopwatch\Stopwatch::class)) {
$container->getDefinition('twig.extension.debug.stopwatch')->addTag('twig.extension');
}
if ($container->hasDefinition('twig.extension.expression')) {
$container->getDefinition('twig.extension.expression')->addTag('twig.extension');
}
if ($container->hasDefinition('twig.extension.emoji')) {
$container->getDefinition('twig.extension.emoji')->addTag('twig.extension');
}
if (!class_exists(Workflow::class) || !$container->has('workflow.registry')) {
$container->removeDefinition('workflow.twig_extension');
} 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

@@ -0,0 +1,39 @@
<?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\Compiler\ServiceLocatorTagPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
/**
* Registers Twig runtime services.
*/
class RuntimeLoaderPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
if (!$container->hasDefinition('twig.runtime_loader')) {
return;
}
$definition = $container->getDefinition('twig.runtime_loader');
$mapping = [];
foreach ($container->findTaggedServiceIds('twig.runtime', true) as $id => $attributes) {
$def = $container->getDefinition($id);
$mapping[$def->getClass()] = new Reference($id);
}
$definition->replaceArgument(0, ServiceLocatorTagPass::register($container, $mapping));
}
}

View File

@@ -0,0 +1,57 @@
<?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\Compiler\PriorityTaggedServiceTrait;
use Symfony\Component\DependencyInjection\ContainerBuilder;
/**
* Adds tagged twig.extension services to twig service.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class TwigEnvironmentPass implements CompilerPassInterface
{
use PriorityTaggedServiceTrait;
public function process(ContainerBuilder $container): void
{
if (false === $container->hasDefinition('twig')) {
return;
}
$definition = $container->getDefinition('twig');
// Extensions must always be registered before everything else.
// For instance, global variable definitions must be registered
// afterward. If not, the globals from the extensions will never
// be registered.
$currentMethodCalls = $definition->getMethodCalls();
$twigBridgeExtensionsMethodCalls = [];
$othersExtensionsMethodCalls = [];
foreach ($this->findAndSortTaggedServices('twig.extension', $container) as $extension) {
$methodCall = ['addExtension', [$extension]];
$extensionClass = $container->getDefinition((string) $extension)->getClass();
if (\is_string($extensionClass) && str_starts_with($extensionClass, 'Symfony\Bridge\Twig\Extension')) {
$twigBridgeExtensionsMethodCalls[] = $methodCall;
} else {
$othersExtensionsMethodCalls[] = $methodCall;
}
}
if ($twigBridgeExtensionsMethodCalls || $othersExtensionsMethodCalls) {
$definition->setMethodCalls(array_merge($twigBridgeExtensionsMethodCalls, $othersExtensionsMethodCalls, $currentMethodCalls));
}
}
}

View File

@@ -0,0 +1,60 @@
<?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;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Reference;
/**
* Adds services tagged twig.loader as Twig loaders.
*
* @author Daniel Leech <daniel@dantleech.com>
*/
class TwigLoaderPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
if (false === $container->hasDefinition('twig')) {
return;
}
$prioritizedLoaders = [];
$found = 0;
foreach ($container->findTaggedServiceIds('twig.loader', true) as $id => $attributes) {
$priority = $attributes[0]['priority'] ?? 0;
$prioritizedLoaders[$priority][] = $id;
++$found;
}
if (!$found) {
throw new LogicException('No twig loaders found. You need to tag at least one loader with "twig.loader".');
}
if (1 === $found) {
$container->setAlias('twig.loader', $id);
} else {
$chainLoader = $container->getDefinition('twig.loader.chain');
krsort($prioritizedLoaders);
foreach ($prioritizedLoaders as $loaders) {
foreach ($loaders as $loader) {
$chainLoader->addMethodCall('addLoader', [new Reference($loader)]);
}
}
$container->setAlias('twig.loader', 'twig.loader.chain');
}
}
}

View File

@@ -0,0 +1,234 @@
<?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;
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;
use Symfony\Component\Mime\HtmlToTextConverter\HtmlToTextConverterInterface;
/**
* TwigExtension configuration structure.
*
* @author Jeremy Mikola <jmikola@gmail.com>
*/
class Configuration implements ConfigurationInterface
{
/**
* Generates the configuration tree builder.
*/
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('twig');
$rootNode = $treeBuilder->getRootNode();
$rootNode
->docUrl('https://symfony.com/doc/{version:major}.{version:minor}/reference/configuration/twig.html', 'symfony/twig-bundle')
->beforeNormalization()
->ifTrue(fn ($v) => \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);
$this->addTwigOptions($rootNode);
$this->addTwigFormatOptions($rootNode);
$this->addMailerSection($rootNode);
return $treeBuilder;
}
private function addFormThemesSection(ArrayNodeDefinition $rootNode): void
{
$rootNode
->fixXmlConfig('form_theme')
->children()
->arrayNode('form_themes')
->addDefaultChildrenIfNoneSet()
->prototype('scalar')->defaultValue('form_div_layout.html.twig')->end()
->example(['@My/form.html.twig'])
->validate()
->ifTrue(fn ($v) => !\in_array('form_div_layout.html.twig', $v, true))
->then(fn ($v) => array_merge(['form_div_layout.html.twig'], $v))
->end()
->end()
->end()
;
}
private function addGlobalsSection(ArrayNodeDefinition $rootNode): void
{
$rootNode
->fixXmlConfig('global')
->children()
->arrayNode('globals')
->normalizeKeys(false)
->useAttributeAsKey('key')
->example(['foo' => '@bar', 'pi' => 3.14])
->prototype('array')
->normalizeKeys(false)
->beforeNormalization()
->ifTrue(fn ($v) => \is_string($v) && str_starts_with($v, '@'))
->then(function ($v) {
if (str_starts_with($v, '@@')) {
return substr($v, 1);
}
return ['id' => substr($v, 1), 'type' => 'service'];
})
->end()
->beforeNormalization()
->ifTrue(function ($v) {
if (\is_array($v)) {
$keys = array_keys($v);
sort($keys);
return $keys !== ['id', 'type'] && $keys !== ['value'];
}
return true;
})
->then(fn ($v) => ['value' => $v])
->end()
->children()
->scalarNode('id')->end()
->scalarNode('type')
->validate()
->ifNotInArray(['service'])
->thenInvalid('The %s type is not supported')
->end()
->end()
->variableNode('value')->end()
->end()
->end()
->end()
->end()
;
}
private function addTwigOptions(ArrayNodeDefinition $rootNode): void
{
$rootNode
->fixXmlConfig('path')
->children()
->scalarNode('autoescape_service')->defaultNull()->end()
->scalarNode('autoescape_service_method')->defaultNull()->end()
->scalarNode('base_template_class')
->setDeprecated('symfony/twig-bundle', '7.1')
->example('Twig\Template')
->cannotBeEmpty()
->end()
->scalarNode('cache')->defaultTrue()->end()
->scalarNode('charset')->defaultValue('%kernel.charset%')->end()
->booleanNode('debug')->defaultValue('%kernel.debug%')->end()
->booleanNode('strict_variables')->defaultValue('%kernel.debug%')->end()
->scalarNode('auto_reload')->end()
->integerNode('optimizations')->min(-1)->end()
->scalarNode('default_path')
->info('The default path used to load templates.')
->defaultValue('%kernel.project_dir%/templates')
->end()
->arrayNode('file_name_pattern')
->example('*.twig')
->info('Pattern of file name used for cache warmer and linter.')
->beforeNormalization()
->ifString()
->then(fn ($value) => [$value])
->end()
->prototype('scalar')->end()
->end()
->arrayNode('paths')
->normalizeKeys(false)
->useAttributeAsKey('paths')
->beforeNormalization()
->ifArray()
->then(function ($paths) {
$normalized = [];
foreach ($paths as $path => $namespace) {
if (\is_array($namespace)) {
// xml
$path = $namespace['value'];
$namespace = $namespace['namespace'];
}
// path within the default namespace
if (ctype_digit((string) $path)) {
$path = $namespace;
$namespace = null;
}
$normalized[$path] = $namespace;
}
return $normalized;
})
->end()
->prototype('variable')->end()
->end()
->end()
;
}
private function addTwigFormatOptions(ArrayNodeDefinition $rootNode): void
{
$rootNode
->children()
->arrayNode('date')
->info('The default format options used by the date filter.')
->addDefaultsIfNotSet()
->children()
->scalarNode('format')->defaultValue('F j, Y H:i')->end()
->scalarNode('interval_format')->defaultValue('%d days')->end()
->scalarNode('timezone')
->info('The timezone used when formatting dates, when set to null, the timezone returned by date_default_timezone_get() is used.')
->defaultNull()
->end()
->end()
->end()
->arrayNode('number_format')
->info('The default format options for the number_format filter.')
->addDefaultsIfNotSet()
->children()
->integerNode('decimals')->defaultValue(0)->end()
->scalarNode('decimal_point')->defaultValue('.')->end()
->scalarNode('thousands_separator')->defaultValue(',')->end()
->end()
->end()
->end()
;
}
private function addMailerSection(ArrayNodeDefinition $rootNode): void
{
$rootNode
->children()
->arrayNode('mailer')
->children()
->scalarNode('html_to_text_converter')
->info(\sprintf('A service implementing the "%s".', HtmlToTextConverterInterface::class))
->defaultNull()
->end()
->end()
->end()
->end()
;
}
}

View File

@@ -0,0 +1,49 @@
<?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\Configurator;
use Symfony\Bridge\Twig\UndefinedCallableHandler;
use Twig\Environment;
use Twig\Extension\CoreExtension;
/**
* Twig environment configurator.
*
* @author Christian Flothmann <christian.flothmann@xabbuh.de>
*/
class EnvironmentConfigurator
{
public function __construct(
private string $dateFormat,
private string $intervalFormat,
private ?string $timezone,
private int $decimals,
private string $decimalPoint,
private string $thousandsSeparator,
) {
}
public function configure(Environment $environment): void
{
$environment->getExtension(CoreExtension::class)->setDateFormat($this->dateFormat, $this->intervalFormat);
if (null !== $this->timezone) {
$environment->getExtension(CoreExtension::class)->setTimezone($this->timezone);
}
$environment->getExtension(CoreExtension::class)->setNumberFormat($this->decimals, $this->decimalPoint, $this->thousandsSeparator);
// wrap UndefinedCallableHandler in closures for lazy-autoloading
$environment->registerUndefinedFilterCallback(fn ($name) => UndefinedCallableHandler::onUndefinedFilter($name));
$environment->registerUndefinedFunctionCallback(fn ($name) => UndefinedCallableHandler::onUndefinedFunction($name));
}
}

View File

@@ -0,0 +1,259 @@
<?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;
use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\AttributeExtensionPass;
use Symfony\Component\AssetMapper\AssetMapper;
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\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\LocaleSwitcher;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Validator\Constraint;
use Symfony\Contracts\Service\ResetInterface;
use Twig\Attribute\AsTwigFilter;
use Twig\Attribute\AsTwigFunction;
use Twig\Attribute\AsTwigTest;
use Twig\Environment;
use Twig\Extension\ExtensionInterface;
use Twig\Extension\RuntimeExtensionInterface;
use Twig\Loader\LoaderInterface;
/**
* TwigExtension.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Jeremy Mikola <jmikola@gmail.com>
*/
class TwigExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container): void
{
$loader = new PhpFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('twig.php');
if (method_exists(Environment::class, 'resetGlobals')) {
$container->getDefinition('twig')->addTag('kernel.reset', ['method' => 'resetGlobals']);
}
if ($container::willBeAvailable('symfony/form', Form::class, ['symfony/twig-bundle'])) {
$loader->load('form.php');
if (is_subclass_of(AbstractRendererEngine::class, ResetInterface::class)) {
$container->getDefinition('twig.form.engine')->addTag('kernel.reset', [
'method' => 'reset',
]);
}
}
if ($container::willBeAvailable('symfony/console', Application::class, ['symfony/twig-bundle'])) {
$loader->load('console.php');
}
if (!$container::willBeAvailable('symfony/translation', Translator::class, ['symfony/twig-bundle'])) {
$container->removeDefinition('twig.translation.extractor');
}
if ($container::willBeAvailable('symfony/validator', Constraint::class, ['symfony/twig-bundle'])) {
$loader->load('validator.php');
}
foreach ($configs as $key => $config) {
if (isset($config['globals'])) {
foreach ($config['globals'] as $name => $value) {
if (\is_array($value) && isset($value['key'])) {
$configs[$key]['globals'][$name] = [
'key' => $name,
'value' => $value,
];
}
}
}
}
$configuration = $this->getConfiguration($configs, $container);
$config = $this->processConfiguration($configuration, $configs);
if ($container::willBeAvailable('symfony/mailer', Mailer::class, ['symfony/twig-bundle'])) {
$loader->load('mailer.php');
if ($htmlToTextConverter = $config['mailer']['html_to_text_converter'] ?? null) {
$container->getDefinition('twig.mime_body_renderer')->setArgument('$converter', new Reference($htmlToTextConverter));
}
if (ContainerBuilder::willBeAvailable('symfony/translation', LocaleSwitcher::class, ['symfony/framework-bundle'])) {
$container->getDefinition('twig.mime_body_renderer')->setArgument('$localeSwitcher', new Reference('translation.locale_switcher', ContainerBuilder::IGNORE_ON_INVALID_REFERENCE));
}
}
if ($container::willBeAvailable('symfony/asset-mapper', AssetMapper::class, ['symfony/twig-bundle'])) {
$loader->load('importmap.php');
}
$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']);
$envConfiguratorDefinition->replaceArgument(1, $config['date']['interval_format']);
$envConfiguratorDefinition->replaceArgument(2, $config['date']['timezone']);
$envConfiguratorDefinition->replaceArgument(3, $config['number_format']['decimals']);
$envConfiguratorDefinition->replaceArgument(4, $config['number_format']['decimal_point']);
$envConfiguratorDefinition->replaceArgument(5, $config['number_format']['thousands_separator']);
$twigFilesystemLoaderDefinition = $container->getDefinition('twig.loader.native_filesystem');
// register user-configured paths
foreach ($config['paths'] as $path => $namespace) {
if (!$namespace) {
$twigFilesystemLoaderDefinition->addMethodCall('addPath', [$path]);
} else {
$twigFilesystemLoaderDefinition->addMethodCall('addPath', [$path, $namespace]);
}
}
// paths are modified in ExtensionPass if forms are enabled
$container->getDefinition('twig.template_iterator')->replaceArgument(1, $config['paths']);
$container->getDefinition('twig.template_iterator')->replaceArgument(3, $config['file_name_pattern']);
if ($container->hasDefinition('twig.command.lint')) {
$container->getDefinition('twig.command.lint')->replaceArgument(1, $config['file_name_pattern'] ?: ['*.twig']);
}
foreach ($this->getBundleTemplatePaths($container, $config) as $name => $paths) {
$namespace = $this->normalizeBundleName($name);
foreach ($paths as $path) {
$twigFilesystemLoaderDefinition->addMethodCall('addPath', [$path, $namespace]);
}
if ($paths) {
// the last path must be the bundle views directory
$twigFilesystemLoaderDefinition->addMethodCall('addPath', [$path, '!'.$namespace]);
}
}
if (file_exists($defaultTwigPath)) {
$twigFilesystemLoaderDefinition->addMethodCall('addPath', [$defaultTwigPath]);
}
$container->addResource(new FileExistenceResource($defaultTwigPath));
if (!empty($config['globals'])) {
$def = $container->getDefinition('twig');
foreach ($config['globals'] as $key => $global) {
if (isset($global['type']) && 'service' === $global['type']) {
$def->addMethodCall('addGlobal', [$key, new Reference($global['id'])]);
} else {
$def->addMethodCall('addGlobal', [$key, $global['value']]);
}
}
}
if (true === $config['cache']) {
$autoReloadOrDefault = $container->getParameterBag()->resolveValue($config['auto_reload'] ?? $config['debug']);
$buildDir = $container->getParameter('kernel.build_dir');
$cacheDir = $container->getParameter('kernel.cache_dir');
if ($autoReloadOrDefault || $cacheDir === $buildDir) {
$config['cache'] = '%kernel.cache_dir%/twig';
}
}
if (true === $config['cache']) {
$config['cache'] = new Reference('twig.template_cache.chain');
} else {
$container->removeDefinition('twig.template_cache.chain');
$container->removeDefinition('twig.template_cache.runtime_cache');
$container->removeDefinition('twig.template_cache.readonly_cache');
$container->removeDefinition('twig.template_cache.warmup_cache');
if (false === $config['cache']) {
$container->removeDefinition('twig.template_cache_warmer');
} else {
$container->getDefinition('twig.template_cache_warmer')->replaceArgument(2, null);
}
}
if (isset($config['autoescape_service'])) {
$config['autoescape'] = [new Reference($config['autoescape_service']), $config['autoescape_service_method'] ?? '__invoke'];
} else {
$config['autoescape'] = 'name';
}
$container->getDefinition('twig')->replaceArgument(1, array_intersect_key($config, [
'debug' => true,
'charset' => true,
'base_template_class' => true,
'strict_variables' => true,
'autoescape' => true,
'cache' => true,
'auto_reload' => true,
'optimizations' => true,
]));
$container->registerForAutoconfiguration(ExtensionInterface::class)->addTag('twig.extension');
$container->registerForAutoconfiguration(LoaderInterface::class)->addTag('twig.loader');
$container->registerForAutoconfiguration(RuntimeExtensionInterface::class)->addTag('twig.runtime');
$container->registerAttributeForAutoconfiguration(AsTwigFilter::class, AttributeExtensionPass::autoconfigureFromAttribute(...));
$container->registerAttributeForAutoconfiguration(AsTwigFunction::class, AttributeExtensionPass::autoconfigureFromAttribute(...));
$container->registerAttributeForAutoconfiguration(AsTwigTest::class, AttributeExtensionPass::autoconfigureFromAttribute(...));
}
private function getBundleTemplatePaths(ContainerBuilder $container, array $config): array
{
$bundleHierarchy = [];
foreach ($container->getParameter('kernel.bundles_metadata') as $name => $bundle) {
$defaultOverrideBundlePath = $container->getParameterBag()->resolveValue($config['default_path']).'/bundles/'.$name;
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));
}
return $bundleHierarchy;
}
private function normalizeBundleName(string $name): string
{
if (str_ends_with($name, 'Bundle')) {
$name = substr($name, 0, -6);
}
return $name;
}
public function getXsdValidationBasePath(): string|false
{
return __DIR__.'/../Resources/config/schema';
}
public function getNamespace(): string
{
return 'http://symfony.com/schema/dic/twig';
}
}