WebuniCommonMarkExtension::createConverter()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 33
rs 8.8571
cc 1
eloc 24
nc 1
nop 3
1
<?php
2
3
/*
4
 * This is part of the webuni/commonmark-bundle package.
5
 *
6
 * (c) Martin Hasoň <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Webuni\Bundle\CommonMarkBundle\DependencyInjection;
13
14
use Symfony\Component\Config\FileLocator;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\DefinitionDecorator;
17
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
18
use Symfony\Component\DependencyInjection\Reference;
19
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;
20
21
class WebuniCommonMarkExtension extends ConfigurableExtension
22
{
23
    protected function loadInternal(array $mergedConfig, ContainerBuilder $container)
24
    {
25
        $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
26
        $loader->load('services.xml');
27
28
        if ($mergedConfig['extensions']['attributes'] && class_exists('Webuni\CommonMark\AttributesExtension\AttributesExtension')) {
29
            $loader->load('extensions/attributes.xml');
30
        }
31
32
        if ($mergedConfig['extensions']['table'] && class_exists('Webuni\CommonMark\TableExtension\TableExtension')) {
33
            $loader->load('extensions/table.xml');
34
        }
35
36
        $converters = [];
37
        foreach ($mergedConfig['converters'] as $name => $config) {
38
            $converters[$name] = new Reference($this->createConverter($name, $config, $container));
39
        }
40
41
        $registry = $container->getDefinition('webuni_commonmark.converter_registry');
42
        $registry->replaceArgument(0, $converters);
43
    }
44
45
    public function getAlias()
46
    {
47
        return 'webuni_commonmark';
48
    }
49
50
    private function createConverter($name, array $config, ContainerBuilder $container)
51
    {
52
        $environment = new DefinitionDecorator($config['environment']);
53
        $environment->setPublic(false);
54
        $environment->setClass($container->getDefinition($config['environment'])->getClass());
55
        $environment->addMethodCall('mergeConfig', [$config['config']]);
56
        $environment->addTag('webuni_commonmark.environment.extensions', $config['extensions']);
57
        // $environment->addTag('webuni_commonmark.environment', ['parent' => $config['environment'], 'extensions' => [$config['extensions']]);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
58
59
        $environmentName = 'webuni_commonmark.'.$name.'_environment'.$config['environment'];
60
        $container->setDefinition($environmentName, $environment);
61
62
        $parser = new DefinitionDecorator($config['parser']);
63
        $parser->setPublic(false);
64
        $parser->setClass($container->getDefinition($config['parser'])->getClass());
65
        $parser->replaceArgument(0, new Reference($environmentName));
66
67
        $renderer = new DefinitionDecorator($config['renderer']);
68
        $renderer->setPublic(false);
69
        $renderer->setClass($container->getDefinition($config['renderer'])->getClass());
70
        $renderer->replaceArgument(0, new Reference($environmentName));
71
72
        $converter = new DefinitionDecorator($config['converter']);
73
        $converter->setPublic(true);
74
        $converter->setClass($container->getDefinition($config['converter'])->getClass());
75
        $converter->replaceArgument(0, $parser);
76
        $converter->replaceArgument(1, $renderer);
77
78
        $converterName = 'webuni_commonmark.'.$name.'_converter';
79
        $container->setDefinition($converterName, $converter);
80
81
        return $converterName;
82
    }
83
}
84