Passed
Push — master ( 5d1e58...95ad6f )
by Aimeos
05:54
created

Memory::apply()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Metaways Infosystems GmbH, 2013
6
 * @copyright Aimeos (aimeos.org), 2015-2021
7
 * @package MW
8
 * @subpackage Config
9
 */
10
11
12
namespace Aimeos\MW\Config\Decorator;
13
14
15
/**
16
 * Memory caching decorator for config classes.
17
 *
18
 * @package MW
19
 * @subpackage Config
20
 */
21
class Memory
22
	extends \Aimeos\MW\Config\Decorator\Base
23
	implements \Aimeos\MW\Config\Decorator\Iface
24
{
25
	private $negCache = [];
26
	private $cache = [];
27
	private $config;
28
29
30
	/**
31
	 * Initializes the decorator.
32
	 *
33
	 * @param \Aimeos\MW\Config\Iface $object Config object or decorator
34
	 * @param array $config Pre-cached non-shared configuration
35
	 */
36
	public function __construct( \Aimeos\MW\Config\Iface $object, array $config = [] )
37
	{
38
		parent::__construct( $object );
39
40
		$this->config = $config;
41
	}
42
43
44
	/**
45
	 * Adds the given configuration and overwrite already existing keys.
46
	 *
47
	 * @param array $config Associative list of (multi-dimensional) configuration settings
48
	 * @return \Aimeos\MW\Config\Iface Config instance for method chaining
49
	 */
50
	public function apply( array $config ) : \Aimeos\MW\Config\Iface
51
	{
52
		$this->config = array_replace_recursive( $this->config, $config );
53
		return $this;
54
	}
55
56
57
	/**
58
	 * Returns the value of the requested config key.
59
	 *
60
	 * @param string $name Path to the requested value like tree/node/classname
61
	 * @param mixed $default Value returned if requested key isn't found
62
	 * @return mixed Value associated to the requested key
63
	 */
64
	public function get( string $name, $default = null )
65
	{
66
		$name = trim( $name, '/' );
67
68
		if( isset( $this->negCache[$name] ) ) {
69
			return $default;
70
		}
71
72
		if( array_key_exists( $name, $this->cache ) ) {
73
			return $this->cache[$name];
74
		}
75
76
		if( ( $value = $this->getValueFromArray( $this->config, explode( '/', $name ) ) ) === null ) {
77
			$value = parent::get( $name, null );
78
		}
79
80
		if( $value === null )
81
		{
82
			$this->negCache[$name] = true;
83
			return $default;
84
		}
85
86
		$this->cache[$name] = $value;
87
		return $value;
88
	}
89
90
91
	/**
92
	 * Sets the value for the specified key.
93
	 *
94
	 * @param string $name Path to the requested value like tree/node/classname
95
	 * @param string $value Value that should be associated with the given path
96
	 * @return \Aimeos\MW\Config\Iface Config instance for method chaining
97
	 */
98
	public function set( string $name, $value ) : \Aimeos\MW\Config\Iface
99
	{
100
		$name = trim( $name, '/' );
101
102
		if( $value !== null )
103
		{
104
			$this->cache[$name] = $value;
105
			unset( $this->negCache[$name] );
106
		}
107
		else
108
		{
109
			$this->negCache[$name] = true;
110
		}
111
112
		// don't store local configuration
113
		return $this;
114
	}
115
116
117
	/**
118
	 * Returns the requested configuration value from the given array
119
	 *
120
	 * @param array $config The array to search in
121
	 * @param array $parts Configuration path parts to look for inside the array
122
	 * @return mixed Found configuration value or null if not available
123
	 */
124
	protected function getValueFromArray( array $config, array $parts )
125
	{
126
		if( ( $key = array_shift( $parts ) ) !== null && isset( $config[$key] ) )
127
		{
128
			if( count( $parts ) > 0 ) {
129
				return $this->getValueFromArray( $config[$key], $parts );
130
			}
131
132
			return $config[$key];
133
		}
134
135
		return null;
136
	}
137
}
138