1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Config |
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
10
|
|
|
* @license http://mit-license.org/ MIT License |
11
|
|
|
* @link http://www.phossa.com/ |
12
|
|
|
*/ |
13
|
|
|
/*# declare(strict_types=1); */ |
14
|
|
|
|
15
|
|
|
namespace Phossa2\Config; |
16
|
|
|
|
17
|
|
|
use Phossa2\Config\Message\Message; |
18
|
|
|
use Phossa2\Shared\Base\ObjectAbstract; |
19
|
|
|
use Phossa2\Config\Traits\ArrayAccessTrait; |
20
|
|
|
use Phossa2\Config\Exception\LogicException; |
21
|
|
|
use Phossa2\Config\Interfaces\ConfigInterface; |
22
|
|
|
use Phossa2\Config\Interfaces\WritableInterface; |
23
|
|
|
use Phossa2\Config\Interfaces\DelegatorInterface; |
24
|
|
|
use Phossa2\Config\Traits\DelegatorWritableTrait; |
25
|
|
|
use Phossa2\Shared\Delegator\DelegatorAwareTrait; |
26
|
|
|
use Phossa2\Shared\Delegator\DelegatorAwareInterface; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Delegator |
30
|
|
|
* |
31
|
|
|
* Delegator for config |
32
|
|
|
* |
33
|
|
|
* @package Phossa2\Config |
34
|
|
|
* @author Hong Zhang <[email protected]> |
35
|
|
|
* @see ObjectAbstract |
36
|
|
|
* @see DelegatorInterface |
37
|
|
|
* @see \ArrayAccess |
38
|
|
|
* @see WritableInterface |
39
|
|
|
* @version 2.0.12 |
40
|
|
|
* @since 2.0.0 added |
41
|
|
|
* @since 2.0.7 changed DelegatorInterface, added ChainingInterface |
42
|
|
|
* @since 2.0.10 removed ChainingInterface |
43
|
|
|
* @since 2.0.12 changed set() return value |
44
|
|
|
*/ |
45
|
|
|
class Delegator extends ObjectAbstract implements DelegatorInterface, \ArrayAccess, WritableInterface, DelegatorAwareInterface |
46
|
|
|
{ |
47
|
|
|
use ArrayAccessTrait, DelegatorWritableTrait, DelegatorAwareTrait; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritDoc} |
51
|
|
|
*/ |
52
|
|
|
public function get(/*# string */ $id, $default = null) |
53
|
|
|
{ |
54
|
|
|
if ($this->hasInLookup($id)) { |
55
|
|
|
return $this->getFromLookup($id); |
56
|
|
|
} |
57
|
|
|
return $default; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritDoc} |
62
|
|
|
*/ |
63
|
|
|
public function has(/*# string */ $id)/*# : bool */ |
64
|
|
|
{ |
65
|
|
|
return $this->hasInLookup($id); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritDoc} |
70
|
|
|
*/ |
71
|
|
|
public function addConfig(ConfigInterface $config) |
72
|
|
|
{ |
73
|
|
|
return $this->addRegistry($config); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @since 2.0.12 changed return value |
78
|
|
|
* |
79
|
|
|
* {@inheritDoc} |
80
|
|
|
*/ |
81
|
|
|
public function set(/*# string */ $id, $value)/*# : bool */ |
82
|
|
|
{ |
83
|
|
|
if ($this->isWritable()) { |
84
|
|
|
$this->writable->set($id, $value); |
85
|
|
|
return $this->writable->has($id); |
86
|
|
|
} else { |
87
|
|
|
throw new LogicException( |
88
|
|
|
Message::get(Message::CONFIG_NOT_WRITABLE, $id), |
89
|
|
|
Message::CONFIG_NOT_WRITABLE |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritDoc} |
96
|
|
|
*/ |
97
|
|
|
protected function hasInRegistry( |
98
|
|
|
$registry, |
99
|
|
|
/*# string */ $id |
100
|
|
|
)/*# : bool */ { |
101
|
|
|
/* @var $registry ConfigInterface */ |
102
|
|
|
return $registry->has($id); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritDoc} |
107
|
|
|
*/ |
108
|
|
|
protected function getFromRegistry( |
109
|
|
|
$registry, |
110
|
|
|
/*# string */ $id |
111
|
|
|
) { |
112
|
|
|
/* @var $registry ConfigInterface */ |
113
|
|
|
return $registry->get($id); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|