ConfigLoader::load()   B
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 47
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 47
ccs 29
cts 29
cp 1
rs 8.5125
cc 6
eloc 28
nc 4
nop 2
crap 6
1
<?php
2
3
/*
4
 * This file is part of the "RocketORM" package.
5
 *
6
 * https://github.com/RocketORM/ORM
7
 *
8
 * For the full license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Rocket\ORM\Config;
13
14
use Rocket\ORM\Config\Exception\ConfigurationFileNotFoundException;
15
use Symfony\Component\Finder\Finder;
16
use Symfony\Component\Finder\SplFileInfo;
17
use Symfony\Component\Yaml\Yaml;
18
19
/**
20
 * @author Sylvain Lorinet <[email protected]>
21
 */
22
class ConfigLoader
23
{
24
    /**
25
     * @var array
26
     */
27
    protected $config;
28
29
30
    /**
31
     * @param string|null $path
32
     * @param array       $options
33
     */
34 2
    public function __construct($path = null, array $options = [])
35
    {
36 2
        $this->load($path, $options);
37 1
    }
38
39
    /**
40
     * @param string $path
41
     * @param array  $options
42
     *
43
     * @throws ConfigurationFileNotFoundException
44
     */
45 2
    protected function load($path, array $options)
46
    {
47 2
        if (null != $path) {
48 1
            if (!is_file($path)) {
49 1
                throw new ConfigurationFileNotFoundException('The rocket configuration file is not found in the selected folder "' . $path . '"');
50
            }
51 1
        } else {
52 2
            $rootDir = getcwd();
53 2
            $finder = new Finder();
54
            $dirs = [
55 2
                $rootDir . '/config',
56
                $rootDir . '/configs'
57 2
            ];
58
59 2
            $availableDirs = [$rootDir];
60 2
            foreach ($dirs as $dir) {
61 2
                if (is_dir($dir)) {
62 1
                    $availableDirs[] = $dir;
63 1
                }
64 2
            }
65
66
            $finder
67 2
                ->files()
68 2
                ->in($availableDirs)
69 2
                ->name('rocket.yml')
70 2
                ->depth(0)
71
            ;
72
73
            /** @var SplFileInfo $file */
74 2
            $files = $finder->getIterator();
75 2
            $files->rewind();
76 2
            $file = $files->current();
77
78 2
            if (null == $file) {
79 1
                throw new ConfigurationFileNotFoundException('The rocket configuration file is not found. Please create a new one into your root folder or in a folder named "/config" or "/configs".');
80
            }
81
82 1
            $path = $file->getRealPath();
83
84
        }
85
86
        // Replace "__DIR__" string to the absolute path where the file is loaded
87 1
        $__DIR__ = join(DIRECTORY_SEPARATOR, array_slice(explode(DIRECTORY_SEPARATOR, $path), 0, -1));
88 1
        $yamlString = str_replace('__DIR__', $__DIR__, file_get_contents($path));
89
90 1
        $this->config = array_merge(Yaml::parse($yamlString), $options)['rocket'];
91 1
    }
92
93
    /**
94
     * @return array
95
     */
96 1
    public function all()
97
    {
98 1
        return $this->config;
99
    }
100
}
101