1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Shared |
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\Shared\Reference; |
16
|
|
|
|
17
|
|
|
use Phossa2\Shared\Message\Message; |
18
|
|
|
use Phossa2\Shared\Exception\InvalidArgumentException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* DelegatorTrait |
22
|
|
|
* |
23
|
|
|
* Implementation of DelegatorInterface |
24
|
|
|
* |
25
|
|
|
* @package Phossa2\Shared |
26
|
|
|
* @author Hong Zhang <[email protected]> |
27
|
|
|
* @see DelegatorInterface |
28
|
|
|
* @version 2.0.8 |
29
|
|
|
* @since 2.0.8 added |
30
|
|
|
*/ |
31
|
|
|
trait DelegatorTrait |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* lookup pool of registries |
35
|
|
|
* |
36
|
|
|
* @var array |
37
|
|
|
* @access protected |
38
|
|
|
*/ |
39
|
|
|
protected $lookup_pool = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* cached lookup key |
43
|
|
|
* |
44
|
|
|
* @var string |
45
|
|
|
* @access private |
46
|
|
|
*/ |
47
|
|
|
private $cache_key; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* cached lookup registry |
51
|
|
|
* |
52
|
|
|
* @var object |
53
|
|
|
* @access private |
54
|
|
|
*/ |
55
|
|
|
private $cache_reg; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritDoc} |
59
|
|
|
*/ |
60
|
|
|
public function addRegistry($registry) |
61
|
|
|
{ |
62
|
|
|
/* @var $registry DelegatorAwareInterface */ |
63
|
|
|
|
64
|
|
|
// check registry type |
65
|
|
|
if (!$this->isValidRegistry($registry)) { |
66
|
|
|
throw new InvalidArgumentException( |
67
|
|
|
Message::get(Message::MSG_ARGUMENT_INVALID, get_class($registry)), |
68
|
|
|
Message::MSG_ARGUMENT_INVALID |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// remove this registry if exists already |
73
|
|
|
$this->removeFromLookup($registry); |
74
|
|
|
|
75
|
|
|
// register delegator |
76
|
|
|
$registry->setDelegator($this); |
77
|
|
|
|
78
|
|
|
// append to the pool end |
79
|
|
|
$this->lookup_pool[] = $registry; |
80
|
|
|
|
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritDoc} |
86
|
|
|
*/ |
87
|
|
|
public function clearAllRegistries() |
88
|
|
|
{ |
89
|
|
|
$this->lookup_pool = []; |
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritDoc} |
95
|
|
|
*/ |
96
|
|
|
public function hasInLookup(/*# string */ $key)/*# : bool */ |
97
|
|
|
{ |
98
|
|
|
foreach ($this->lookup_pool as $registry) { |
99
|
|
|
if ($this->hasInRegistry($registry, $key)) { |
100
|
|
|
$this->cache_key = $key; |
101
|
|
|
$this->cache_reg = $registry; |
102
|
|
|
return true; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritDoc} |
110
|
|
|
*/ |
111
|
|
|
public function getFromLookup(/*# string */ $key) |
112
|
|
|
{ |
113
|
|
|
// lookup already ? try lookup first |
114
|
|
|
if ($key === $this->cache_key || $this->hasInLookup($key)) { |
115
|
|
|
return $this->getFromRegistry($this->cache_reg, $key); |
116
|
|
|
} |
117
|
|
|
// not found |
118
|
|
|
return null; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Remove one object from the pool |
123
|
|
|
* |
124
|
|
|
* @param object $registry |
125
|
|
|
* @return $this |
126
|
|
|
* @access protected |
127
|
|
|
*/ |
128
|
|
|
protected function removeFromLookup($registry) |
129
|
|
|
{ |
130
|
|
|
foreach ($this->lookup_pool as $idx => $reg) { |
131
|
|
|
if ($registry === $reg) { |
132
|
|
|
unset($this->lookup_pool[$idx]); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Is registry type allowed in lookup pool ? |
140
|
|
|
* |
141
|
|
|
* @param object $registry |
142
|
|
|
* @return bool |
143
|
|
|
* @access protected |
144
|
|
|
*/ |
145
|
|
|
abstract protected function isValidRegistry($registry)/*# : bool */; |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Try has in registry |
149
|
|
|
* |
150
|
|
|
* @param object $registry |
151
|
|
|
* @param name $key |
152
|
|
|
* @return bool |
153
|
|
|
* @access protected |
154
|
|
|
*/ |
155
|
|
|
abstract protected function hasInRegistry( |
156
|
|
|
$registry, |
157
|
|
|
/*# string */ $key |
158
|
|
|
)/*# : bool */; |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Try get from registry |
162
|
|
|
* |
163
|
|
|
* @param object $registry |
164
|
|
|
* @param name $key |
165
|
|
|
* @return mixed |
166
|
|
|
* @access protected |
167
|
|
|
*/ |
168
|
|
|
abstract protected function getFromRegistry( |
169
|
|
|
$registry, |
170
|
|
|
/*# string */ $key |
171
|
|
|
); |
172
|
|
|
} |
173
|
|
|
|