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
|
|
|
use ContainerHelperTrait; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* default scope for objects |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
* @access protected |
39
|
|
|
*/ |
40
|
|
|
protected $default_scope = ScopeInterface::SCOPE_SHARED; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Split 'service_id@scope' into ['service_id', 'scope'] |
44
|
|
|
* |
45
|
|
|
* if no scope found, use '' |
46
|
|
|
* |
47
|
|
|
* @param string $id |
48
|
|
|
* @return array [$id, $scope] |
49
|
|
|
* @access protected |
50
|
|
|
*/ |
51
|
|
|
protected function splitId(/*# string */ $id)/*# : array */ |
52
|
|
|
{ |
53
|
|
|
if (false !== strpos($id, '@')) { |
54
|
|
|
return explode('@', $id, 2); |
55
|
|
|
} else { |
56
|
|
|
return [$id, '']; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Return the raw id without scope |
62
|
|
|
* |
63
|
|
|
* @param string $id |
64
|
|
|
* @return string |
65
|
|
|
* @access protected |
66
|
|
|
*/ |
67
|
|
|
protected function idWithoutScope(/*# string */ $id)/*# : string */ |
68
|
|
|
{ |
69
|
|
|
return $this->splitId($id)[0]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get the scope part of $id |
74
|
|
|
* |
75
|
|
|
* @param string $id |
76
|
|
|
* @return string |
77
|
|
|
* @access protected |
78
|
|
|
*/ |
79
|
|
|
protected function getScopeOfId(/*# string */ $id)/*# : string */ |
80
|
|
|
{ |
81
|
|
|
return $this->splitId($id)[1]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Append a scope to the $id, if old scope exists, replace it |
86
|
|
|
* |
87
|
|
|
* @param string $id |
88
|
|
|
* @param string $scope |
89
|
|
|
* @return string |
90
|
|
|
* @access protected |
91
|
|
|
*/ |
92
|
|
|
protected function scopedId( |
93
|
|
|
/*# string */ $id, |
94
|
|
|
/*# string */ $scope |
95
|
|
|
)/*# : string */ { |
96
|
|
|
return $this->idWithoutScope($id) . '@' . $scope; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Returns the raw id and the scope base on default or defined |
101
|
|
|
* |
102
|
|
|
* @param string $id |
103
|
|
|
* @return array [rawId, scope] |
104
|
|
|
* @access protected |
105
|
|
|
*/ |
106
|
|
|
protected function scopedInfo(/*# string */ $id)/*# : array */ |
107
|
|
|
{ |
108
|
|
|
// split into raw id and scope (if any) |
109
|
|
|
list($rawId, $scope) = $this->splitId($id); |
110
|
|
|
|
111
|
|
|
// use the default scope if no scope given |
112
|
|
|
if (empty($scope)) { |
113
|
|
|
// use the default |
114
|
|
|
$scope = $this->default_scope; |
115
|
|
|
|
116
|
|
|
// honor predefined scope over the default |
117
|
|
|
$def = $this->getResolver()->getService($rawId); |
118
|
|
|
if (is_array($def) && isset($def['scope'])) { |
119
|
|
|
$scope = $def['scope']; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return [$rawId, $scope]; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Append scope to data |
128
|
|
|
* |
129
|
|
|
* @param mixed $data |
130
|
|
|
* @param string $scope |
131
|
|
|
* @return array |
132
|
|
|
* @access protected |
133
|
|
|
*/ |
134
|
|
|
protected function scopedData($data, /*# string */ $scope)/*# : array */ |
135
|
|
|
{ |
136
|
|
|
if (is_array($data) && isset($data['class'])) { |
137
|
|
|
$data['scope'] = $scope; |
138
|
|
|
} else { |
139
|
|
|
$data = ['class' => $data, 'scope' => $scope]; |
140
|
|
|
} |
141
|
|
|
return $data; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|