Zend   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 45
dl 0
loc 144
rs 10
c 0
b 0
f 0
wmc 20

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A set() 0 20 3
A get() 0 17 4
A load() 0 22 6
A getPart() 0 19 5
A __clone() 0 3 1
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2011
6
 * @copyright Aimeos (aimeos.org), 2014-2018
7
 * @package MW
8
 * @subpackage Config
9
 */
10
11
12
namespace Aimeos\MW\Config;
13
14
15
/**
16
 * Configuration setting class using \Zend_Config.
17
 *
18
 * @package MW
19
 * @subpackage Config
20
 */
21
class Zend
22
	extends \Aimeos\MW\Config\Base
23
	implements \Aimeos\MW\Config\Iface
24
{
25
	private $config;
26
	private $paths;
27
28
29
	/**
30
	 * Initialize config object with \Zend_Config instance.
31
	 *
32
	 * @param \Zend_Config $config Configuration object
33
	 * @param array|string $path Filesystem path or list of paths to the configuration files
34
	 */
35
	public function __construct( \Zend_Config $config, $path = [] )
0 ignored issues
show
Bug introduced by
The type Zend_Config was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
36
	{
37
		$this->config = $config;
38
		$this->paths = (array) $path;
39
	}
40
41
42
	/**
43
	 * Clones the objects inside.
44
	 */
45
	public function __clone()
46
	{
47
		$this->config = clone $this->config;
48
	}
49
50
51
	/**
52
	 * Returns the value of the requested config key.
53
	 *
54
	 * @param string $path Path to the requested value like tree/node/classname
55
	 * @param mixed $default Value returned if requested key isn't found
56
	 * @return mixed Value associated to the requested key or default value if no value in configuration was found
57
	 */
58
	public function get( $path, $default = null )
59
	{
60
		$parts = explode( '/', trim( $path, '/' ) );
61
62
		if( ( $value = $this->getPart( $this->config, $parts ) ) !== null ) {
63
			return $value;
64
		}
65
66
		foreach( $this->paths as $fspath ) {
67
			$this->load( $this->config, $fspath, $parts );
68
		}
69
70
		if( ( $value = $this->getPart( $this->config, $parts ) ) !== null ) {
71
			return $value;
72
		}
73
74
		return $default;
75
	}
76
77
78
	/**
79
	 * Sets the value for the specified key.
80
	 *
81
	 * @param string $path Path to the requested value like tree/node/classname
82
	 * @param mixed $value Value that should be associated with the given path
83
	 */
84
	public function set( $path, $value )
85
	{
86
		$classname = 'Zend_Config';
87
		$parts = explode( '/', trim( $path, '/' ) );
88
89
		$config = $this->config;
90
		$max = count( $parts ) - 1;
91
92
		for( $i = 0; $i < $max; $i++ )
93
		{
94
			$val = $config->get( $parts[$i] );
95
96
			if( $val instanceof $classname ) {
97
				$config = $val;
98
			} else {
99
				$config = $config->{$parts[$i]} = new \Zend_Config( [], true );
100
			}
101
		}
102
103
		$config->{$parts[$max]} = $value;
104
	}
105
106
107
	/**
108
	 * Descents into the configuration specified by the given path and returns the value if found.
109
	 *
110
	 * @param \Zend_Config $config Configuration object which should contain the loaded configuration
111
	 * @param array $parts List of config name parts to look for
112
	 * @return mixed Found value or null if no value is available
113
	 */
114
	protected function getPart( \Zend_Config $config, array $parts )
115
	{
116
		$classname = 'Zend_Config';
117
118
		if( ( $key = array_shift( $parts ) ) !== null && isset( $config->$key ) )
119
		{
120
			if( $config->$key instanceof $classname )
121
			{
122
				if( count( $parts  ) > 0 ) {
123
					return $this->getPart( $config->$key, $parts );
124
				}
125
126
				return $config->$key->toArray();
127
			}
128
129
			return $config->$key;
130
		}
131
132
		return null;
133
	}
134
135
136
	/**
137
	 * Loads the configuration files when found.
138
	 *
139
	 * @param \Zend_Config $config Configuration object which should contain the loaded configuration
140
	 * @param string $path Path to the configuration directory
141
	 * @param array $parts List of config name parts to look for
142
	 */
143
	protected function load( \Zend_Config $config, $path, array $parts )
144
	{
145
		if( ( $key = array_shift( $parts ) ) !== null )
146
		{
147
			$newPath = $path . DIRECTORY_SEPARATOR . $key;
148
149
			if( is_dir( $newPath ) )
150
			{
151
				if( !isset( $config->$key ) ) {
152
					$config->$key = new \Zend_Config( [], true );
153
				}
154
155
				$this->load( $config->$key, $newPath, $parts );
156
			}
157
158
			if( file_exists( $newPath . '.php' ) )
159
			{
160
				if( !isset( $config->$key ) ) {
161
					$config->$key = new \Zend_Config( [], true );
162
				}
163
164
				$config->$key->merge( new \Zend_Config( $this->includeFile( $newPath . '.php' ), true ) );
165
			}
166
		}
167
	}
168
}