|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Phossa Project |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5.4 |
|
6
|
|
|
* |
|
7
|
|
|
* @category Library |
|
8
|
|
|
* @package Phossa2\Di |
|
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\Di\Traits; |
|
16
|
|
|
|
|
17
|
|
|
use Phossa2\Di\Interfaces\ScopeInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* ScopeTrait |
|
21
|
|
|
* |
|
22
|
|
|
* Implmentation of ScopeInterface. |
|
23
|
|
|
* |
|
24
|
|
|
* @package Phossa2\Di |
|
25
|
|
|
* @author Hong Zhang <[email protected]> |
|
26
|
|
|
* @see ScopeInterface |
|
27
|
|
|
* @version 2.0.0 |
|
28
|
|
|
* @since 2.0.0 added |
|
29
|
|
|
*/ |
|
30
|
|
|
trait ScopeTrait |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* default scope for objects |
|
34
|
|
|
* |
|
35
|
|
|
* @var string |
|
36
|
|
|
* @access protected |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $default_scope = ScopeInterface::SCOPE_SHARED; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @inheritDoc |
|
42
|
|
|
*/ |
|
43
|
|
|
public function share(/*# bool */ $shared = true) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->default_scope = (bool) $shared ? |
|
46
|
|
|
ScopeInterface::SCOPE_SHARED : ScopeInterface::SCOPE_SINGLE; |
|
47
|
|
|
return $this; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Split 'service_id@scope' into ['service_id', 'scope'] |
|
52
|
|
|
* |
|
53
|
|
|
* if no scope found, use '' |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $id |
|
56
|
|
|
* @return array [$id, $scope] |
|
57
|
|
|
* @access protected |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function splitId(/*# string */ $id)/*# : array */ |
|
60
|
|
|
{ |
|
61
|
|
|
if (false !== strpos($id, '@')) { |
|
62
|
|
|
return explode('@', $id, 2); |
|
63
|
|
|
} else { |
|
64
|
|
|
return [$id, '']; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Return the raw id without scope |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $id |
|
72
|
|
|
* @return string |
|
73
|
|
|
* @access protected |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function idWithoutScope(/*# string */ $id)/*# : string */ |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->splitId($id)[0]; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Get the scope part of $id |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $id |
|
84
|
|
|
* @return string |
|
85
|
|
|
* @access protected |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function getScopeOfId(/*# string */ $id)/*# : string */ |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->splitId($id)[1]; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Append a scope to the $id, if old scope exists, replace it |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $id |
|
96
|
|
|
* @param string $scope |
|
97
|
|
|
* @return string |
|
98
|
|
|
* @access protected |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function scopedId( |
|
101
|
|
|
/*# string */ $id, |
|
102
|
|
|
/*# string */ $scope |
|
103
|
|
|
)/*# : string */ { |
|
104
|
|
|
return $this->idWithoutScope($id) . '@' . $scope; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Returns the raw id and the scope base on default or defined |
|
109
|
|
|
* |
|
110
|
|
|
* @param string $id |
|
111
|
|
|
* @return array [rawId, scope] |
|
112
|
|
|
* @access protected |
|
113
|
|
|
*/ |
|
114
|
|
|
protected function scopedInfo(/*# string */ $id)/*# : array */ |
|
115
|
|
|
{ |
|
116
|
|
|
// split into raw id and scope (if any) |
|
117
|
|
|
list($rawId, $scope) = $this->splitId($id); |
|
118
|
|
|
|
|
119
|
|
|
// use the default scope if no scope given |
|
120
|
|
|
if (empty($scope)) { |
|
121
|
|
|
// use the default |
|
122
|
|
|
$scope = $this->default_scope; |
|
123
|
|
|
|
|
124
|
|
|
// honor predefined scope over the default |
|
125
|
|
|
$def = $this->getResolver()->getService($rawId); |
|
126
|
|
|
if (is_array($def) && isset($def['scope'])) { |
|
127
|
|
|
$scope = $def['scope']; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return [$rawId, $scope]; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Append scope to data |
|
136
|
|
|
* |
|
137
|
|
|
* @param mixed $data |
|
138
|
|
|
* @param string $scope |
|
139
|
|
|
* @return array |
|
140
|
|
|
* @access protected |
|
141
|
|
|
*/ |
|
142
|
|
|
protected function scopedData($data, /*# string */ $scope)/*# : array */ |
|
143
|
|
|
{ |
|
144
|
|
|
if (is_array($data) && isset($data['class'])) { |
|
145
|
|
|
$data['scope'] = $scope; |
|
146
|
|
|
} else { |
|
147
|
|
|
$data = ['class' => $data, 'scope' => $scope]; |
|
148
|
|
|
} |
|
149
|
|
|
return $data; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* From ContainerHelperTrait |
|
154
|
|
|
* |
|
155
|
|
|
* @return ResolverInterface |
|
156
|
|
|
*/ |
|
157
|
|
|
abstract protected function getResolver()/*# : ResolverInterface */; |
|
158
|
|
|
} |
|
159
|
|
|
|