Completed
Pull Request — master (#341)
by De Cramer
05:55
created

eXpansionMlComposeExtension::parseFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 2
1
<?php
2
3
namespace eXpansion\Framework\MlComposeBundle\DependencyInjection;
4
5
use Oliverde8\PageCompose\Service\BlockDefinitions;
6
use Symfony\Component\Config\FileLocator;
7
use Symfony\Component\DependencyInjection\ContainerBuilder;
8
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
9
use Symfony\Component\DependencyInjection\Reference;
10
use Symfony\Component\Finder\Finder;
11
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
12
13
14
/**
15
 * Class eXpansionMlComposeExtension
16
 *
17
 * @author    de Cramer Oliver<[email protected]>
18
 * @copyright 2018 Oliverde8
19
 * @package eXpansion\Framework\MlComposeBundle\DependencyInjection
20
 */
21
class eXpansionMlComposeExtension extends Extension
22
{
23
24
    /**
25
     * @inheritdoc
26
     */
27
    public function load(array $configs, ContainerBuilder $container)
28
    {
29
30
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
31
        $loader->load('services.yml');
32
33
        $bundles = $container->getParameter('kernel.bundles');
34
        $finder = new Finder();
35
36
        $pageLayout = [];
37
38
        foreach ($bundles as $bundle) {
39
            $reflection = new \ReflectionClass($bundle);
40
            $dir = dirname($reflection->getFilename()) . '/Resources/config/ml-compose/';
41
42
            if (is_dir($dir)) {
43
                $files = $finder->files()
44
                    ->name("*.xml")
45
                    ->in($dir);
46
47
                foreach ($files as $file) {
48
                    $pageLayout += $this->parseFile($file, $container);
49
                }
50
            }
51
        }
52
53
        $container->getDefinition(BlockDefinitions::class)->setArgument('$buildFactories', $pageLayout);
54
    }
55
56
    protected function parseFile($filePath, ContainerBuilder $container)
57
    {
58
        $xsdPath = realpath(__DIR__ . "/../Resources/config/page-compose.xsd");
59
        $dom = new \DOMDocument();
60
        $dom->loadXML(str_replace("exp:ml_compose_bundle:page-compose.xsd", $xsdPath, file_get_contents($filePath)));
61
62
        if (!$dom->schemaValidate($xsdPath)) {
63
            var_dump($errors = libxml_get_errors());
0 ignored issues
show
Security Debugging Code introduced by
var_dump($errors = libxml_get_errors()); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
64
            echo "File contains errors";
65
            die(2);
0 ignored issues
show
Coding Style Compatibility introduced by
The method parseFile() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
66
            // TODO do this better.
67
        }
68
69
        $xml = simplexml_load_file($filePath);
70
        return $this->processBlock($xml, $container);
71
    }
72
73
    protected function processBlock(\SimpleXMLElement $element, ContainerBuilder $container, $parent = null) {
74
        $blockDefinition = [];
75
76
        if ($parent) {
77
            $blockDefinition['parent'] = $parent;
78
        }
79
80
        if (isset($element->attributes()['component'])) {
81
            $blockDefinition['component'] = (string) $element->attributes()['component'];
82
        } else {
83
            $blockDefinition['extends'] = (string) $element['@attributes']['extends'];
84
        }
85
86
        if (isset($element->attributes()['alias'])) {
87
            $blockDefinition['alias'] = (string) $element->attributes()['alias'];
88
        }
89
90
        foreach ($element->argument as $argument) {
91
            $name = (string) $argument->attributes()['name'];
92
93
            switch ($this->getXsiType($argument)) {
94
                case "expr":
95
                    $blockDefinition['config']['expr'][$name] = (string) $argument;
96
                    break;
97
98
                case "action":
99
                    $blockDefinition['config']['action'][$name]['method'] = (string)$argument->method[0];
100
                    if (isset($argument->service[0])) {
101
                        // Use symfony to actually get a reference.
102
                        $blockDefinition['config']['action'][$name]['service'] = new Reference((string)$argument->service[0]);
103
                    }
104
                    break;
105
106
                case "args":
107
                    $blockDefinition['config']['args'][$name] = [];
108
                    foreach ($argument->arg as $arg) {
109
                        $blockDefinition['config']['args'][$name][] = (string) $arg;
110
                    }
111
                    break;
112
113
                default:
114
                    $blockDefinition['config']['def'][$name] = (string) $argument;
115
            }
116
        }
117
118
        $blockDefinitions = [
119
            (string) $element->attributes()['id'] => $blockDefinition,
120
        ];
121
122
        foreach ($element->block as $block) {
123
            $blockDefinitions += $this->processBlock($block, $container, (string) $element->attributes()['id']);
124
        }
125
126
        return $blockDefinitions;
127
    }
128
129
    protected function getXsiType(\SimpleXMLElement $element)
130
    {
131
        $namespaces = $element->getNamespaces();
132
133
        if (isset($namespaces['xsi']) && isset($element->attributes($namespaces['xsi'])['type'])) {
134
            return (string) $element->attributes($element->getNamespaces()['xsi'])['type'];
135
        }
136
137
        return null;
138
    }
139
140
}