|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @license LGPLv3, https://opensource.org/licenses/LGPL-3.0 |
|
5
|
|
|
* @copyright Aimeos (aimeos.org), 2016-2022 |
|
6
|
|
|
* @package Base |
|
7
|
|
|
* @subpackage Config |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
namespace Aimeos\Base\Config\Decorator; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Protection decorator for config classes. |
|
16
|
|
|
* |
|
17
|
|
|
* @package Base |
|
18
|
|
|
* @subpackage Config |
|
19
|
|
|
*/ |
|
20
|
|
|
class Protect |
|
21
|
|
|
extends \Aimeos\Base\Config\Decorator\Base |
|
22
|
|
|
implements \Aimeos\Base\Config\Decorator\Iface |
|
23
|
|
|
{ |
|
24
|
|
|
private $prefixes = []; |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Initializes the decorator |
|
29
|
|
|
* |
|
30
|
|
|
* @param \Aimeos\Base\Config\Iface $object Config object or decorator |
|
31
|
|
|
* @param string[] $prefixes Allowed prefixes for getting and setting values |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct( \Aimeos\Base\Config\Iface $object, array $prefixes = [] ) |
|
34
|
|
|
{ |
|
35
|
|
|
parent::__construct( $object ); |
|
36
|
|
|
|
|
37
|
|
|
foreach( $prefixes as $prefix ) { |
|
38
|
|
|
$this->prefixes[$prefix] = strlen( $prefix ); |
|
39
|
|
|
} |
|
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
|
|
|
* @throws \Aimeos\Base\Config\Exception If retrieving configuration isn't allowed |
|
50
|
|
|
*/ |
|
51
|
|
|
public function get( string $name, $default = null ) |
|
52
|
|
|
{ |
|
53
|
|
|
foreach( $this->prefixes as $prefix => $len ) |
|
54
|
|
|
{ |
|
55
|
|
|
if( strncmp( $name, $prefix, $len ) === 0 ) { |
|
56
|
|
|
return parent::get( $name, $default ); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
throw new \Aimeos\Base\Config\Exception( sprintf( 'Not allowed to access "%1$s" configuration', $name ) ); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|