KnpMarkdownBundle/DependencyInjection/KnpMarkdownExtension.php

58 lines
2.4 KiB
PHP
Raw Normal View History

<?php
namespace Knp\Bundle\MarkdownBundle\DependencyInjection;
use Knp\Bundle\MarkdownBundle\MarkdownParserInterface;
use Michelf\MarkdownInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
2017-11-29 19:18:19 +01:00
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\Templating\EngineInterface;
class KnpMarkdownExtension extends Extension
{
2011-02-25 15:43:59 +01:00
/**
* Handles the knp_markdown configuration.
2011-02-25 15:43:59 +01:00
*
* @param array $configs The configurations being loaded
2011-02-25 15:43:59 +01:00
* @param ContainerBuilder $container
*
* @throws InvalidConfigurationException When Sundown parser was selected, but extension is not available
2011-02-25 15:43:59 +01:00
*/
public function load(array $configs , ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('parser.xml');
// BC to support the PHP templates in the Templating component
if (interface_exists(EngineInterface::class)) {
$loader->load('helper.xml');
}
$loader->load('twig.xml');
if ('markdown.parser.sundown' == $config['parser']['service']) {
if (!class_exists('Sundown\\Markdown')) {
throw new InvalidConfigurationException('Sundown parser selected, but required extension is not installed or configured.');
}
$loader->load('sundown.xml');
$definition = $container->getDefinition('markdown.parser.sundown');
$definition->addTag('markdown.parser', array('alias' => 'sundown'));
$container->setParameter('markdown.sundown.extensions', $config['sundown']['extensions']);
$container->setParameter('markdown.sundown.render_flags', $config['sundown']['render_flags']);
}
2017-11-29 19:18:19 +01:00
$container->setAlias('markdown.parser', new Alias($config['parser']['service'], true));
$container->setAlias(MarkdownParserInterface::class, 'markdown.parser');
$container->setAlias(MarkdownInterface::class, 'markdown.parser');
}
}