Completed
Push — master ( 621267...4bcb6e )
by Vitaly
04:06
created

XMLContainerBuilder::__construct()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 28
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 2
Bugs 1 Features 1
Metric Value
dl 0
loc 28
ccs 13
cts 13
cp 1
rs 8.439
c 2
b 1
f 1
cc 5
eloc 12
nc 5
nop 4
crap 5
1
<?php declare(strict_types = 1);
2
/**
3
 * Created by Vitaly Iegorov <[email protected]>.
4
 * on 18.08.16 at 13:04
5
 */
6
namespace samsonframework\container;
7
8
use samsonframework\container\collection\CollectionClassResolver;
9
use samsonframework\container\resolver\ResolverInterface;
10
use samsonframework\filemanager\FileManagerInterface;
11
use samsonphp\generator\Generator;
12
13
/**
14
 * XML configuration dependency injection container builder.
15
 *
16
 * @author Vitaly Egorov <[email protected]>
17
 */
18
class XMLContainerBuilder extends ContainerBuilder
19
{
20
    /**
21
     * Resolve xml config
22
     *
23
     * @param string $xmlConfig
24
     *
25
     */
26 1
    public function __construct($xmlConfig, FileManagerInterface $fileManager, ResolverInterface $classResolver, Generator $generator)
27
    {
28 1
        $this->classResolver = $classResolver;
29
30
        // Convert xml to array
31 1
        $arrayData = $this->xml2array(new \SimpleXMLElement($xmlConfig));
32
        // Iterate config and resolve single instance
33 1
        foreach ($arrayData as $key => $classesArrayData) {
34 1
            if ($key === CollectionClassResolver::KEY) {
35 1
                foreach ($classesArrayData as $classArrayData) {
36
                    // Store metadata
37 1
                    $classMetadata = $this->classResolver->resolve($classArrayData);
38
39
                    // Store by metadata name as alias
40 1
                    $this->classAliases[$classMetadata->name] = $classMetadata->className;
41
42
                    // Store class in defined scopes
43 1
                    foreach ($classMetadata->scopes as $scope) {
44 1
                        $this->scopes[$scope][] = $classMetadata->className;
45
                    }
46
47 1
                    $this->classMetadata[$classMetadata->className] = $classMetadata;
48
                }
49
            }
50
        }
51
52 1
        parent::__construct($fileManager, $classResolver, $generator);
53 1
    }
54
55
    /**
56
     * function xml2array
57
     *
58
     * This function is part of the PHP manual.
59
     *
60
     * The PHP manual text and comments are covered by the Creative Commons
61
     * Attribution 3.0 License, copyright (c) the PHP Documentation Group
62
     *
63
     * @author  k dot antczak at livedata dot pl
64
     * @date    2011-04-22 06:08 UTC
65
     * @link    http://www.php.net/manual/en/ref.simplexml.php#103617
66
     * @license http://www.php.net/license/index.php#doc-lic
67
     * @license http://creativecommons.org/licenses/by/3.0/
68
     * @license CC-BY-3.0 <http://spdx.org/licenses/CC-BY-3.0>
69
     */
70 1
    protected function xml2array($xmlObject, $out = array())
71
    {
72 1
        foreach ((array)$xmlObject as $index => $node) {
73 1
            $out[$index] = (is_object($node) || is_array($node)) ? $this->xml2array($node) : $node;
74
        }
75
76 1
        return $out;
77
    }
78
}
79