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\WritableTrait; |
20
|
|
|
use Phossa2\Config\Traits\ArrayAccessTrait; |
21
|
|
|
use Phossa2\Shared\Reference\DelegatorTrait; |
22
|
|
|
use Phossa2\Config\Exception\LogicException; |
23
|
|
|
use Phossa2\Config\Interfaces\ConfigInterface; |
24
|
|
|
use Phossa2\Shared\Reference\DelegatorInterface; |
25
|
|
|
use Phossa2\Config\Interfaces\WritableInterface; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Delegator |
29
|
|
|
* |
30
|
|
|
* Implmentation of DelegatorInterface |
31
|
|
|
* |
32
|
|
|
* @package Phossa2\Config |
33
|
|
|
* @author Hong Zhang <[email protected]> |
34
|
|
|
* @see ObjectAbstract |
35
|
|
|
* @see DelegatorInterface |
36
|
|
|
* @version 2.0.0 |
37
|
|
|
* @since 2.0.0 added |
38
|
|
|
*/ |
39
|
|
|
class Delegator extends ObjectAbstract implements \ArrayAccess, DelegatorInterface, ConfigInterface, WritableInterface |
40
|
|
|
{ |
41
|
|
|
use ArrayAccessTrait, DelegatorTrait, WritableTrait; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* constructor |
45
|
|
|
* |
46
|
|
|
* @access public |
47
|
|
|
*/ |
48
|
|
|
public function __construct() |
49
|
|
|
{ |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritDoc} |
54
|
|
|
*/ |
55
|
|
|
public function get(/*# string */ $key, $default = null) |
56
|
|
|
{ |
57
|
|
|
if ($this->hasInLookup($key)) { |
58
|
|
|
return $this->getFromLookup($key); |
59
|
|
|
} |
60
|
|
|
return $default; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritDoc} |
65
|
|
|
*/ |
66
|
|
|
public function has(/*# string */ $key)/*# : bool */ |
67
|
|
|
{ |
68
|
|
|
return $this->hasInLookup($key); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritDoc} |
73
|
|
|
*/ |
74
|
|
|
public function isWritable()/*# : bool */ |
75
|
|
|
{ |
76
|
|
|
foreach ($this->lookup_pool as $reg) { |
77
|
|
|
if ($reg instanceof WritableInterface && |
78
|
|
|
$reg->isWritable() |
79
|
|
|
) { |
80
|
|
|
$this->setWritable($reg); |
81
|
|
|
return true; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
return false; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritDoc} |
89
|
|
|
*/ |
90
|
|
|
public function set(/*# string */ $key, $value) |
91
|
|
|
{ |
92
|
|
|
if ($this->isWritable()) { |
93
|
|
|
$this->writable->set($key, $value); |
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
throw new LogicException( |
97
|
|
|
Message::get(Message::CONFIG_NOT_WRITABLE), |
98
|
|
|
Message::CONFIG_NOT_WRITABLE |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritDoc} |
104
|
|
|
*/ |
105
|
|
|
protected function isValidRegistry($registry)/*# : bool */ |
106
|
|
|
{ |
107
|
|
|
return $registry instanceof ConfigInterface; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritDoc} |
112
|
|
|
*/ |
113
|
|
|
protected function hasInRegistry( |
114
|
|
|
$registry, |
115
|
|
|
/*# string */ $key |
116
|
|
|
)/*# : bool */ { |
117
|
|
|
/* @var $registry ConfigInterface */ |
118
|
|
|
return $registry->has($key); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* {@inheritDoc} |
123
|
|
|
*/ |
124
|
|
|
protected function getFromRegistry( |
125
|
|
|
$registry, |
126
|
|
|
/*# string */ $key |
127
|
|
|
) { |
128
|
|
|
/* @var $registry ConfigInterface */ |
129
|
|
|
return $registry->get($key); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|