Zend2   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 43
dl 0
loc 141
rs 10
c 0
b 0
f 0
wmc 20

6 Methods

Rating   Name   Duplication   Size   Complexity  
A load() 0 22 6
A set() 0 19 3
A __construct() 0 4 1
A get() 0 17 4
A getPart() 0 17 5
A __clone() 0 3 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2014-2018
6
 * @package MW
7
 * @subpackage Config
8
 */
9
10
11
namespace Aimeos\MW\Config;
12
13
14
/**
15
 * Configuration setting class using ZF2 Config
16
 *
17
 * @package MW
18
 * @subpackage Config
19
 */
20
class Zend2
21
	extends \Aimeos\MW\Config\Base
22
	implements \Aimeos\MW\Config\Iface
23
{
24
	private $config;
25
	private $paths;
26
27
28
	/**
29
	 * Initialize config object with ZF2 Config instance
30
	 *
31
	 * @param \Zend\Config\Config $config Configuration object
32
	 * @param array|string $path Filesystem path or list of paths to the configuration files
33
	 */
34
	public function __construct( \Zend\Config\Config $config, $path = [] )
35
	{
36
		$this->config = $config;
37
		$this->paths = (array) $path;
38
	}
39
40
41
	/**
42
	 * Clones the objects inside.
43
	 */
44
	public function __clone()
45
	{
46
		$this->config = clone $this->config;
47
	}
48
49
50
	/**
51
	 * Returns the value of the requested config key.
52
	 *
53
	 * @param string $path Path to the requested value like tree/node/classname
54
	 * @param mixed $default Value returned if requested key isn't found
55
	 * @return mixed Value associated to the requested key or default value if no value in configuration was found
56
	 */
57
	public function get( $path, $default = null )
58
	{
59
		$parts = explode( '/', trim( $path, '/' ) );
60
61
		if( ( $value = $this->getPart( $this->config, $parts ) ) !== null ) {
62
			return $value;
63
		}
64
65
		foreach( $this->paths as $fspath ) {
66
			$this->load( $this->config, $fspath, $parts );
67
		}
68
69
		if( ( $value = $this->getPart( $this->config, $parts ) ) !== null ) {
70
			return $value;
71
		}
72
73
		return $default;
74
	}
75
76
77
	/**
78
	 * Sets the value for the specified key.
79
	 *
80
	 * @param string $path Path to the requested value like tree/node/classname
81
	 * @param mixed $value Value that should be associated with the given path
82
	 */
83
	public function set( $path, $value )
84
	{
85
		$parts = explode( '/', trim( $path, '/' ) );
86
87
		$config = $this->config;
88
		$max = count( $parts ) - 1;
89
90
		for( $i = 0; $i < $max; $i++ )
91
		{
92
			$val = $config->get( $parts[$i] );
93
94
			if( $val instanceof \Zend\Config\Config ) {
95
				$config = $val;
96
			} else {
97
				$config = $config->{$parts[$i]} = new \Zend\Config\Config( [], true );
98
			}
99
		}
100
101
		$config->{$parts[$max]} = $value;
102
	}
103
104
105
	/**
106
	 * Descents into the configuration specified by the given path and returns the value if found.
107
	 *
108
	 * @param Zend\Config\Config $config Configuration object which should contain the loaded configuration
0 ignored issues
show
Bug introduced by
The type Aimeos\MW\Config\Zend\Config\Config was not found. Did you mean Zend\Config\Config? If so, make sure to prefix the type with \.
Loading history...
109
	 * @param array $parts List of config name parts to look for
110
	 * @return mixed Found value or null if no value is available
111
	 */
112
	protected function getPart( \Zend\Config\Config $config, array $parts )
113
	{
114
		if( ( $key = array_shift( $parts ) ) !== null && isset( $config->$key ) )
115
		{
116
			if( $config->$key instanceof \Zend\Config\Config )
117
			{
118
				if( count( $parts  ) > 0 ) {
119
					return $this->getPart( $config->$key, $parts );
120
				}
121
122
				return $config->$key->toArray();
123
			}
124
125
			return $config->$key;
126
		}
127
128
		return null;
129
	}
130
131
132
	/**
133
	 * Loads the configuration files when found.
134
	 *
135
	 * @param Zend\Config\Config $config Configuration object which should contain the loaded configuration
136
	 * @param string $path Path to the configuration directory
137
	 * @param array $parts List of config name parts to look for
138
	 */
139
	protected function load( \Zend\Config\Config $config, $path, array $parts )
140
	{
141
		if( ( $key = array_shift( $parts ) ) !== null )
142
		{
143
			$newPath = $path . DIRECTORY_SEPARATOR . $key;
144
145
			if( is_dir( $newPath ) )
146
			{
147
				if( !isset( $config->$key ) ) {
148
					$config->$key = new \Zend\Config\Config( [], true );
149
				}
150
151
				$this->load( $config->$key, $newPath, $parts );
152
			}
153
154
			if( file_exists( $newPath . '.php' ) )
155
			{
156
				if( !isset( $config->$key ) ) {
157
					$config->$key = new \Zend\Config\Config( [], true );
158
				}
159
160
				$config->$key->merge( new \Zend\Config\Config( $this->includeFile( $newPath . '.php' ), true ) );
161
			}
162
		}
163
	}
164
165
}