Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2d29e2d

Browse files
committed
bug #27626 [TwigBundle][DX] Only add the Twig WebLinkExtension if the WebLink component is enabled (thewilkybarkid)
This PR was squashed before being merged into the 3.4 branch (closes #27626). Discussion ---------- [TwigBundle][DX] Only add the Twig WebLinkExtension if the WebLink component is enabled | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | While adding elifesciences/journal#990 I was a bit confused why the `preload()` Twig function didn't work initially. Turns out the WebLink component is disabled by default if using the full stack, but the Twig extension is always enabled. This only adds the Twig extension if the component is enabled, and shows a friendly error message if it's not. Commits ------- cccb66f [TwigBundle][DX] Only add the Twig WebLinkExtension if the WebLink component is enabled
2 parents f083728 + cccb66f commit 2d29e2d

File tree

4 files changed

+28
-12
lines changed

4 files changed

+28
-12
lines changed

src/Symfony/Bridge/Twig/UndefinedCallableHandler.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bridge\Twig;
1313

14+
use Symfony\Bundle\FullStack;
1415
use Twig\Error\SyntaxError;
1516

1617
/**
@@ -55,14 +56,21 @@ class UndefinedCallableHandler
5556
'workflow_marked_places' => 'workflow',
5657
);
5758

59+
private static $fullStackEnable = array(
60+
'form' => 'enable "framework.form"',
61+
'security-core' => 'add the "SecurityBundle"',
62+
'security-http' => 'add the "SecurityBundle"',
63+
'web-link' => 'enable "framework.web_link"',
64+
'workflow' => 'enable "framework.workflows"',
65+
);
66+
5867
public static function onUndefinedFilter($name)
5968
{
6069
if (!isset(self::$filterComponents[$name])) {
6170
return false;
6271
}
6372

64-
// Twig will append the source context to the message, so that it will end up being like "[...] Unknown filter "%s" in foo.html.twig on line 123."
65-
throw new SyntaxError(sprintf('Did you forget to run "composer require symfony/%s"? Unknown filter "%s".', self::$filterComponents[$name], $name));
73+
self::onUndefined($name, 'filter', self::$filterComponents[$name]);
6674
}
6775

6876
public static function onUndefinedFunction($name)
@@ -71,6 +79,15 @@ public static function onUndefinedFunction($name)
7179
return false;
7280
}
7381

74-
throw new SyntaxError(sprintf('Did you forget to run "composer require symfony/%s"? Unknown function "%s".', self::$functionComponents[$name], $name));
82+
self::onUndefined($name, 'function', self::$functionComponents[$name]);
83+
}
84+
85+
private static function onUndefined($name, $type, $component)
86+
{
87+
if (\class_exists(FullStack::class) && isset(self::$fullStackEnable[$component])) {
88+
throw new SyntaxError(sprintf('Did you forget to %s? Unknown %s "%s".', self::$fullStackEnable[$component], $type, $name));
89+
}
90+
91+
throw new SyntaxError(sprintf('Did you forget to run "composer require symfony/%s"? Unknown %s "%s".', $component, $type, $name));
7592
}
7693
}

src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/ExtensionPass.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ public function process(ContainerBuilder $container)
8282
}
8383
}
8484

85+
if ($container->has('web_link.add_link_header_listener')) {
86+
$container->getDefinition('twig.extension.weblink')->addTag('twig.extension');
87+
}
88+
8589
$twigLoader = $container->getDefinition('twig.loader.native_filesystem');
8690
if ($container->has('templating')) {
8791
$loader = $container->getDefinition('twig.loader.filesystem');

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@
1111

1212
namespace Symfony\Bundle\TwigBundle\DependencyInjection;
1313

14-
use Symfony\Bridge\Twig\Extension\WebLinkExtension;
1514
use Symfony\Component\Config\FileLocator;
1615
use Symfony\Component\Config\Resource\FileExistenceResource;
1716
use Symfony\Component\Console\Application;
1817
use Symfony\Component\DependencyInjection\ContainerBuilder;
1918
use Symfony\Component\DependencyInjection\Reference;
2019
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
2120
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
22-
use Symfony\Component\WebLink\HttpHeaderSerializer;
2321
use Twig\Extension\ExtensionInterface;
2422
use Twig\Extension\RuntimeExtensionInterface;
2523
use Twig\Loader\LoaderInterface;
@@ -60,13 +58,6 @@ public function load(array $configs, ContainerBuilder $container)
6058
$container->removeDefinition('twig.translation.extractor');
6159
}
6260

63-
if (class_exists(HttpHeaderSerializer::class)) {
64-
$definition = $container->register('twig.extension.weblink', WebLinkExtension::class);
65-
$definition->setPublic(false);
66-
$definition->addArgument(new Reference('request_stack'));
67-
$definition->addTag('twig.extension');
68-
}
69-
7061
foreach ($configs as $key => $config) {
7162
if (isset($config['globals'])) {
7263
foreach ($config['globals'] as $name => $value) {

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@
114114

115115
<service id="twig.extension.debug" class="Twig\Extension\DebugExtension" />
116116

117+
<service id="twig.extension.weblink" class="Symfony\Bridge\Twig\Extension\WebLinkExtension">
118+
<argument type="service" id="request_stack" />
119+
</service>
120+
117121
<service id="twig.translation.extractor" class="Symfony\Bridge\Twig\Translation\TwigExtractor">
118122
<argument type="service" id="twig" />
119123
<tag name="translation.extractor" alias="twig" />

0 commit comments

Comments
 (0)