Skip to content

Commit

Permalink
bug #27626 [TwigBundle][DX] Only add the Twig WebLinkExtension if the…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
fabpot committed Jun 25, 2018
2 parents f083728 + cccb66f commit 2d29e2d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
23 changes: 20 additions & 3 deletions src/Symfony/Bridge/Twig/UndefinedCallableHandler.php
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bridge\Twig;

use Symfony\Bundle\FullStack;
use Twig\Error\SyntaxError;

/**
Expand Down Expand Up @@ -55,14 +56,21 @@ class UndefinedCallableHandler
'workflow_marked_places' => 'workflow',
);

private static $fullStackEnable = array(
'form' => 'enable "framework.form"',
'security-core' => 'add the "SecurityBundle"',
'security-http' => 'add the "SecurityBundle"',
'web-link' => 'enable "framework.web_link"',
'workflow' => 'enable "framework.workflows"',
);

public static function onUndefinedFilter($name)
{
if (!isset(self::$filterComponents[$name])) {
return false;
}

// 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."
throw new SyntaxError(sprintf('Did you forget to run "composer require symfony/%s"? Unknown filter "%s".', self::$filterComponents[$name], $name));
self::onUndefined($name, 'filter', self::$filterComponents[$name]);
}

public static function onUndefinedFunction($name)
Expand All @@ -71,6 +79,15 @@ public static function onUndefinedFunction($name)
return false;
}

throw new SyntaxError(sprintf('Did you forget to run "composer require symfony/%s"? Unknown function "%s".', self::$functionComponents[$name], $name));
self::onUndefined($name, 'function', self::$functionComponents[$name]);
}

private static function onUndefined($name, $type, $component)
{
if (\class_exists(FullStack::class) && isset(self::$fullStackEnable[$component])) {
throw new SyntaxError(sprintf('Did you forget to %s? Unknown %s "%s".', self::$fullStackEnable[$component], $type, $name));
}

throw new SyntaxError(sprintf('Did you forget to run "composer require symfony/%s"? Unknown %s "%s".', $component, $type, $name));
}
}
Expand Up @@ -82,6 +82,10 @@ public function process(ContainerBuilder $container)
}
}

if ($container->has('web_link.add_link_header_listener')) {
$container->getDefinition('twig.extension.weblink')->addTag('twig.extension');
}

$twigLoader = $container->getDefinition('twig.loader.native_filesystem');
if ($container->has('templating')) {
$loader = $container->getDefinition('twig.loader.filesystem');
Expand Down
Expand Up @@ -11,15 +11,13 @@

namespace Symfony\Bundle\TwigBundle\DependencyInjection;

use Symfony\Bridge\Twig\Extension\WebLinkExtension;
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\Reference;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\WebLink\HttpHeaderSerializer;
use Twig\Extension\ExtensionInterface;
use Twig\Extension\RuntimeExtensionInterface;
use Twig\Loader\LoaderInterface;
Expand Down Expand Up @@ -60,13 +58,6 @@ public function load(array $configs, ContainerBuilder $container)
$container->removeDefinition('twig.translation.extractor');
}

if (class_exists(HttpHeaderSerializer::class)) {
$definition = $container->register('twig.extension.weblink', WebLinkExtension::class);
$definition->setPublic(false);
$definition->addArgument(new Reference('request_stack'));
$definition->addTag('twig.extension');
}

foreach ($configs as $key => $config) {
if (isset($config['globals'])) {
foreach ($config['globals'] as $name => $value) {
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml
Expand Up @@ -114,6 +114,10 @@

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

<service id="twig.extension.weblink" class="Symfony\Bridge\Twig\Extension\WebLinkExtension">
<argument type="service" id="request_stack" />
</service>

<service id="twig.translation.extractor" class="Symfony\Bridge\Twig\Translation\TwigExtractor">
<argument type="service" id="twig" />
<tag name="translation.extractor" alias="twig" />
Expand Down

0 comments on commit 2d29e2d

Please sign in to comment.