diff --git a/extra/twig-extra-bundle/src/DependencyInjection/Compiler/MissingExtensionSuggestorPass.php b/extra/twig-extra-bundle/src/DependencyInjection/Compiler/MissingExtensionSuggestorPass.php new file mode 100644 index 000000000..6b8056279 --- /dev/null +++ b/extra/twig-extra-bundle/src/DependencyInjection/Compiler/MissingExtensionSuggestorPass.php @@ -0,0 +1,29 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Twig\Extra\TwigExtraBundle\DependencyInjection\Compiler; + +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Reference; + +class MissingExtensionSuggestorPass implements CompilerPassInterface +{ + public function process(ContainerBuilder $container) + { + if ($container->getParameter('kernel.debug')) { + $container->getDefinition('twig') + ->addMethodCall('registerUndefinedFilterCallback', [[new Reference('twig.missing_extension_suggestor'), 'suggestFilter']]) + ->addMethodCall('registerUndefinedFunctionCallback', [[new Reference('twig.missing_extension_suggestor'), 'suggestFunction']]) + ; + } + } +} diff --git a/extra/twig-extra-bundle/src/DependencyInjection/TwigExtraExtension.php b/extra/twig-extra-bundle/src/DependencyInjection/TwigExtraExtension.php index 4a2cec8b3..b8cdbb565 100644 --- a/extra/twig-extra-bundle/src/DependencyInjection/TwigExtraExtension.php +++ b/extra/twig-extra-bundle/src/DependencyInjection/TwigExtraExtension.php @@ -27,6 +27,10 @@ class TwigExtraExtension extends Extension $configuration = $this->getConfiguration($configs, $container); $config = $this->processConfiguration($configuration, $configs); + if ($container->getParameter('kernel.debug')) { + $loader->load('suggestor.xml'); + } + if ($this->isConfigEnabled($container, $config['html'])) { $loader->load('html.xml'); } diff --git a/extra/twig-extra-bundle/src/MissingExtensionSuggestor.php b/extra/twig-extra-bundle/src/MissingExtensionSuggestor.php new file mode 100644 index 000000000..0c974a805 --- /dev/null +++ b/extra/twig-extra-bundle/src/MissingExtensionSuggestor.php @@ -0,0 +1,45 @@ + ['HtmlExtension', 'twig/html-extra'], + 'markdown_to_html' => ['MarkdownExtension', 'twig/markdown-extra'], + 'html_to_markdown' => ['MarkdownExtension', 'twig/markdown-extra'], + ]; + + private const FUNCTIONS = [ + 'html_classes' => ['HtmlExtension', 'twig/html-extra'], + ]; + + public function suggestFilter(string $name): bool + { + if (isset(self::FILTERS[$name])) { + throw new SyntaxError(sprintf('The "%s" filter is part of the %s, which is not installed/enabled; try running "composer require %s twig/extra-bundle".', $name, self::FILTERS[$name][0], self::FILTERS[$name][1])); + } + + return false; + } + + public function suggestFunction(string $name): bool + { + if (isset(self::FUNCTIONS[$name])) { + throw new SyntaxError(sprintf('The "%s" function is part of the %s, which is not installed/enabled; try running "composer require %s twig/extra-bundle".', $name, self::FILTERS[$name][0], self::FILTERS[$name][1])); + } + + return false; + } +} diff --git a/extra/twig-extra-bundle/src/Resources/config/suggestor.xml b/extra/twig-extra-bundle/src/Resources/config/suggestor.xml new file mode 100644 index 000000000..5f5218d93 --- /dev/null +++ b/extra/twig-extra-bundle/src/Resources/config/suggestor.xml @@ -0,0 +1,12 @@ + + + + + + + + + + diff --git a/extra/twig-extra-bundle/src/TwigExtraBundle.php b/extra/twig-extra-bundle/src/TwigExtraBundle.php index 8af1812e2..5ed32a8cc 100644 --- a/extra/twig-extra-bundle/src/TwigExtraBundle.php +++ b/extra/twig-extra-bundle/src/TwigExtraBundle.php @@ -11,12 +11,16 @@ namespace Twig\Extra\TwigExtraBundle; +use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Bundle\Bundle; +use Twig\Extra\TwigExtraBundle\DependencyInjection\Compiler\MissingExtensionSuggestorPass; class TwigExtraBundle extends Bundle { - public function getPath() + public function build(ContainerBuilder $container) { - return \dirname(__DIR__); + parent::build($container); + + $container->addCompilerPass(new MissingExtensionSuggestorPass()); } }