PHPArray::apply()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2025
6
 * @package Base
7
 * @subpackage Config
8
 */
9
10
11
namespace Aimeos\Base\Config;
12
13
14
/**
15
 * Configuration setting class using arrays
16
 *
17
 * @package Base
18
 * @subpackage Config
19
 */
20
class PHPArray
21
	extends \Aimeos\Base\Config\Base
22
	implements \Aimeos\Base\Config\Iface
23
{
24
	private array $config;
25
26
27
	/**
28
	 * Initialize config object
29
	 *
30
	 * @param array $config Configuration array
31
	 * @param string|array $paths Filesystem path or list of paths to the configuration files
32
	 */
33
	public function __construct( array $config = [], $paths = [] )
34
	{
35
		$this->config = $config;
36
37
		foreach( (array) $paths as $fspath ) {
38
			$this->config = $this->load( $this->config, $fspath );
39
		}
40
	}
41
42
43
	/**
44
	 * Adds the given configuration and overwrite already existing keys.
45
	 *
46
	 * @param array $config Associative list of (multi-dimensional) configuration settings
47
	 * @return \Aimeos\Base\Config\Iface Config instance for method chaining
48
	 */
49
	public function apply( array $config ) : Iface
50
	{
51
		$this->config = array_replace_recursive( $this->config, $config );
52
		return $this;
53
	}
54
55
56
	/**
57
	 * Returns the value of the requested config key.
58
	 *
59
	 * @param string $name Path to the requested value like tree/node/classname
60
	 * @param mixed $default Value returned if requested key isn't found
61
	 * @return mixed Value associated to the requested key
62
	 */
63
	public function get( string $name, $default = null )
64
	{
65
		$parts = explode( '/', trim( $name, '/' ) );
66
67
		if( ( $value = $this->getPart( $this->config, $parts ) ) !== null ) {
68
			return $value;
69
		}
70
71
		return $default;
72
	}
73
74
75
	/**
76
	 * Sets the value for the specified key.
77
	 *
78
	 * @param string $name Path to the requested value like tree/node/classname
79
	 * @param mixed $value Value that should be associated with the given path
80
	 * @return \Aimeos\Base\Config\Iface Config instance for method chaining
81
	 */
82
	public function set( string $name, $value ) : Iface
83
	{
84
		$this->config = $this->setPart( $this->config, explode( '/', trim( $name, '/' ) ), $value );
85
		return $this;
86
	}
87
88
89
	/**
90
	 * Returns a configuration value from an array.
91
	 *
92
	 * @param array $config The array to search in
93
	 * @param string[] $parts Configuration path parts to look for inside the array
94
	 * @return mixed Found value or null if no value is available
95
	 */
96
	protected function getPart( array $config, array $parts )
97
	{
98
		if( ( $current = array_shift( $parts ) ) !== null && isset( $config[$current] ) )
99
		{
100
			if( count( $parts ) > 0 ) {
101
				return $this->getPart( $config[$current], $parts );
102
			}
103
104
			return $config[$current];
105
		}
106
107
		return null;
108
	}
109
110
111
	/**
112
	 * Sets a configuration value in the array.
113
	 *
114
	 * @param mixed $config Configuration sub-part
115
	 * @param string[] $path Configuration path parts
116
	 * @param mixed $value The new value
117
	 */
118
	protected function setPart( $config, array $path, $value )
119
	{
120
		if( ( $current = array_shift( $path ) ) !== null )
121
		{
122
			if( isset( $config[$current] ) ) {
123
				$config[$current] = $this->setPart( $config[$current], $path, $value );
124
			} else {
125
				$config[$current] = $this->setPart( [], $path, $value );
126
			}
127
128
			return $config;
129
		}
130
131
		return $value;
132
	}
133
134
135
	/**
136
	 * Loads the configuration files when found.
137
	 *
138
	 * @param array $config Configuration array which should contain the loaded configuration
139
	 * @param string $path Path to the configuration directory
140
	 * @return array Merged configuration
141
	 */
142
	protected function load( array $config, string $path ) : array
143
	{
144
		if( is_dir( $path ) )
145
		{
146
			foreach( new \DirectoryIterator( $path ) as $entry )
147
			{
148
				if( $entry->isDot() ) {
149
					continue;
150
				}
151
152
				$key = $entry->getBasename( '.php' );
153
				$filepath = $entry->getPathname();
154
155
				if( !isset( $config[$key] ) ) {
156
					$config[$key] = [];
157
				}
158
159
				if( $entry->isDir() ) {
160
					$config[$key] = $this->load( $config[$key], $filepath );
161
				} elseif( $entry->isFile() ) {
162
					$config[$key] = array_replace_recursive( $config[$key], $this->includeFile( $filepath ) );
163
				}
164
			}
165
		}
166
167
		return $config;
168
	}
169
}
170