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