Completed
Push — master ( 097036...1b48a5 )
by Alexander
04:15
created

IniConfigLoader::processLine()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 3
dl 0
loc 12
ccs 8
cts 8
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Class-loader for routes config in ini format
4
 *
5
 * @file      IniConfigLoader.php
6
 *
7
 * PHP version 7.0+
8
 *
9
 * @author    Yancharuk Alexander <alex at itvault dot info>
10
 * @copyright © 2012-2018 Alexander Yancharuk
11
 * @date      2015-05-24 12:08
12
 * @license   The BSD 3-Clause License
13
 *            <https://tldrlegal.com/license/bsd-3-clause-license-(revised)>
14
 */
15
16
namespace Veles\Routing;
17
18
/**
19
 * Class IniConfigLoader
20
 *
21
 * @author  Yancharuk Alexander <alex at itvault dot info>
22
 */
23
class IniConfigLoader extends AbstractConfigLoader
24
{
25
	/**
26
	 * Load routes data from file
27
	 *
28
	 * return array
29
	 */
30 8
	public function load()
31
	{
32 8
		$result = [];
33 8
		$data = parse_ini_file($this->getPath(), true);
34
35 8
		foreach ($data as $name => $section) {
36 8
			$this->buildTree($section);
37
38 8
			$result[$name] = $section;
39
		}
40
41 8
		return $result;
42
	}
43
44
	/**
45
	 * Build array parameters
46
	 *
47
	 * @param array &$config
48
	 */
49 8
	protected function buildTree(array &$config)
50
	{
51 8
		foreach ($config as $name => $value) {
52 8
			$params = explode('.', $name);
53
54 8
			if (1 === count($params)) {
55 8
				continue;
56
			}
57
58 8
			$this->processLine($config, $params, $value);
59
60 8
			unset($config[$name]);
61
		}
62 8
	}
63
64
	/**
65
	 * Processing config line
66
	 *
67
	 * @param array  $config
68
	 * @param array  $params
69
	 * @param string $value
70
	 */
71 8
	protected function processLine(array &$config, array $params, $value)
72
	{
73 8
		$ptr =& $config;
74 8
		$last = end($params);
75
76 8
		foreach ($params as $param) {
77 8
			if ($param === $last) {
78 8
				$ptr[$param] = $value;
79 8
				continue;
80
			}
81
82 8
			$ptr =& $ptr[$param];
83
		}
84 8
	}
85
}
86