From 2c9dc66665b9968952d428a9a128264461f15e2d Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Sun, 16 Oct 2016 10:57:31 +0200 Subject: [PATCH] do not try to register incomplete definitions --- .../Compiler/ExceptionListenerPass.php | 10 +++++-- .../Compiler/ExtensionPass.php | 19 +++++++++++++ .../DependencyInjection/TwigExtension.php | 12 +++++++++ .../TwigBundle/Resources/config/form.xml | 20 ++++++++++++++ .../Resources/config/templating.xml | 21 +++++++++++++++ .../TwigBundle/Resources/config/twig.xml | 27 ------------------- 6 files changed, 80 insertions(+), 29 deletions(-) create mode 100644 src/Symfony/Bundle/TwigBundle/Resources/config/form.xml create mode 100644 src/Symfony/Bundle/TwigBundle/Resources/config/templating.xml diff --git a/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/ExceptionListenerPass.php b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/ExceptionListenerPass.php index 9fbfd65950..711743a5af 100644 --- a/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/ExceptionListenerPass.php +++ b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/ExceptionListenerPass.php @@ -27,8 +27,14 @@ class ExceptionListenerPass implements CompilerPassInterface return; } - // register the exception controller only if Twig is enabled - if ($container->hasParameter('templating.engines')) { + if (!interface_exists('Symfony\Component\Templating\TemplateReferenceInterface')) { + $container->removeDefinition('twig.controller.exception'); + } + + // 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'); diff --git a/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/ExtensionPass.php b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/ExtensionPass.php index d09de2d615..d619de0d81 100644 --- a/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/ExtensionPass.php +++ b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/ExtensionPass.php @@ -23,6 +23,25 @@ class ExtensionPass implements CompilerPassInterface { public function process(ContainerBuilder $container) { + if (!class_exists('Symfony\Component\Asset\Packages')) { + $container->removeDefinition('twig.extension.assets'); + } + + if (!class_exists('Symfony\Component\ExpressionLanguage\Expression')) { + $container->removeDefinition('twig.extension.expression'); + } + + if (!interface_exists('Symfony\Component\Routing\Generator\UrlGeneratorInterface')) { + $container->removeDefinition('twig.extension.routing'); + } + if (!interface_exists('Symfony\Component\Translation\TranslatorInterface')) { + $container->removeDefinition('twig.extension.trans'); + } + + if (!class_exists('Symfony\Component\Yaml\Yaml')) { + $container->removeDefinition('twig.extension.yaml'); + } + if ($container->has('form.extension')) { $container->getDefinition('twig.extension.form')->addTag('twig.extension'); $reflClass = new \ReflectionClass('Symfony\Bridge\Twig\Extension\FormExtension'); diff --git a/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php b/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php index 7b97e120ba..5eb163c886 100644 --- a/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php +++ b/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php @@ -36,6 +36,18 @@ class TwigExtension extends Extension $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); $loader->load('twig.xml'); + if (class_exists('Symfony\Component\Form\Form')) { + $loader->load('form.xml'); + } + + if (interface_exists('Symfony\Component\Templating\EngineInterface')) { + $loader->load('templating.xml'); + } + + if (!interface_exists('Symfony\Component\Translation\TranslatorInterface')) { + $container->removeDefinition('twig.translation.extractor'); + } + foreach ($configs as $key => $config) { if (isset($config['globals'])) { foreach ($config['globals'] as $name => $value) { diff --git a/src/Symfony/Bundle/TwigBundle/Resources/config/form.xml b/src/Symfony/Bundle/TwigBundle/Resources/config/form.xml new file mode 100644 index 0000000000..0703f0f577 --- /dev/null +++ b/src/Symfony/Bundle/TwigBundle/Resources/config/form.xml @@ -0,0 +1,20 @@ + + + + + + + + + + %twig.form.resources% + + + + + + + + diff --git a/src/Symfony/Bundle/TwigBundle/Resources/config/templating.xml b/src/Symfony/Bundle/TwigBundle/Resources/config/templating.xml new file mode 100644 index 0000000000..5a39c95018 --- /dev/null +++ b/src/Symfony/Bundle/TwigBundle/Resources/config/templating.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + diff --git a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml index f2c2a4cee0..c08be1f039 100644 --- a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml +++ b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml @@ -55,22 +55,8 @@ - - - - - - - - - - - - - - @@ -130,21 +116,8 @@ - - - - - - %twig.form.resources% - - - - - - -