added suggestions for extra extensions

This commit is contained in:
Fabien Potencier
2019-08-10 13:41:12 +02:00
parent 879d1b1e76
commit 908eef05de
5 changed files with 96 additions and 2 deletions
@@ -0,0 +1,29 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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']])
;
}
}
}
@@ -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');
}
@@ -0,0 +1,45 @@
<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Extra\TwigExtraBundle;
use Twig\Error\SyntaxError;
final class MissingExtensionSuggestor
{
private const FILTERS = [
'data_uri' => ['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;
}
}
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<defaults public="false" />
<service id="twig.missing_extension_suggestor" class="Twig\Extra\TwigExtraBundle\MissingExtensionSuggestor" />
</services>
</container>
@@ -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());
}
}