Skip to content
Merged
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
30 changes: 20 additions & 10 deletions src/Symfony/Bridge/Twig/UndefinedCallableHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

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

/**
* @internal
Expand All @@ -30,6 +32,8 @@ class UndefinedCallableHandler
'asset' => 'asset',
'asset_version' => 'asset',
'dump' => 'debug-bundle',
'encore_entry_link_tags' => 'webpack-encore-bundle',
'encore_entry_script_tags' => 'webpack-encore-bundle',
'expression' => 'expression-language',
'form_widget' => 'form',
'form_errors' => 'form',
Expand Down Expand Up @@ -64,34 +68,40 @@ class UndefinedCallableHandler
'workflow' => 'enable "framework.workflows"',
];

public static function onUndefinedFilter(string $name): bool
/**
* @return TwigFilter|false
*/
public static function onUndefinedFilter(string $name)
{
if (!isset(self::FILTER_COMPONENTS[$name])) {
return false;
}

self::onUndefined($name, 'filter', self::FILTER_COMPONENTS[$name]);

return true;
throw new SyntaxError(self::onUndefined($name, 'filter', self::FILTER_COMPONENTS[$name]));
}

public static function onUndefinedFunction(string $name): bool
/**
* @return TwigFunction|false
*/
public static function onUndefinedFunction(string $name)
{
if (!isset(self::FUNCTION_COMPONENTS[$name])) {
return false;
}

self::onUndefined($name, 'function', self::FUNCTION_COMPONENTS[$name]);
if ('webpack-encore-bundle' === self::FUNCTION_COMPONENTS[$name]) {
return new TwigFunction($name, static function () { return ''; });
}

return true;
throw new SyntaxError(self::onUndefined($name, 'function', self::FUNCTION_COMPONENTS[$name]));
}

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

throw new SyntaxError(sprintf('Did you forget to run "composer require symfony/%s"? Unknown %s "%s".', $component, $type, $name));
return sprintf('Did you forget to run "composer require symfony/%s"? Unknown %s "%s".', $component, $type, $name);
}
}