XmlLoader::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * XML file loader.
4
 *
5
 * @package SugiPHP.Config
6
 * @author  Plamen Popov <[email protected]>
7
 * @license http://opensource.org/licenses/mit-license.php (MIT License)
8
 */
9
10
namespace SugiPHP\Config;
11
12
class XmlLoader implements LoaderInterface
13
{
14
    protected $locator;
15
16
    public function __construct(LocatorInterface $locator = null)
17
    {
18
        $this->locator = $locator;
19
    }
20
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function load($resource)
25
    {
26
        // check the extension. If it's not provided we'll add .xml
27
        if (pathinfo($resource, PATHINFO_EXTENSION) === "") {
28
            $resource .= ".xml";
29
        }
30
31
        $file = false;
32
33
        if ($this->locator) {
34
            // pass it to the locator (if set) and than include the file
35
            $file = $this->locator->locate($resource);
36
        } elseif (is_file($resource) && is_readable($resource)) {
37
            // check if the $resource is a real file and include it
38
            $file = $resource;
39
        }
40
41
        if ($file) {
42
            $xmlstring = file_get_contents($file);
43
            $xml = simplexml_load_string($xmlstring);
44
            $json = json_encode($xml);
45
            $array = json_decode($json, true);
46
47
            return $array;
48
        }
49
50
        return null;
51
    }
52
}
53