1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* This file is part of the O2System Framework package.
|
4
|
|
|
*
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE
|
6
|
|
|
* file that was distributed with this source code.
|
7
|
|
|
*
|
8
|
|
|
* @author Steeve Andrian Salim
|
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim
|
10
|
|
|
*/
|
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------
|
13
|
|
|
|
14
|
|
|
namespace O2System\Spl\Containers;
|
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------
|
17
|
|
|
|
18
|
|
|
use Psr\Container\ContainerInterface;
|
19
|
|
|
|
20
|
|
|
/**
|
21
|
|
|
* Class SplClosureContainer
|
22
|
|
|
*
|
23
|
|
|
* @package O2System\Spl\Dependency
|
24
|
|
|
*/
|
25
|
|
|
class SplClosureContainer implements \Countable, ContainerInterface
|
26
|
|
|
{
|
27
|
|
|
/**
|
28
|
|
|
* Inversion Control Closure Container
|
29
|
|
|
*
|
30
|
|
|
* @var array
|
31
|
|
|
*/
|
32
|
|
|
protected $closures = [];
|
33
|
|
|
|
34
|
|
|
// ------------------------------------------------------------------------
|
35
|
|
|
|
36
|
|
|
/**
|
37
|
|
|
* SplClosureContainer::attach
|
38
|
|
|
*
|
39
|
|
|
* Adds an closure object in the registry.
|
40
|
|
|
*
|
41
|
|
|
* @param string $offset
|
42
|
|
|
* @param \Closure $closure
|
43
|
|
|
*/
|
44
|
|
|
public function attach($offset, \Closure $closure)
|
45
|
|
|
{
|
46
|
|
|
$this->closures[ $offset ] = $closure;
|
47
|
|
|
}
|
48
|
|
|
|
49
|
|
|
// ------------------------------------------------------------------------
|
50
|
|
|
|
51
|
|
|
/**
|
52
|
|
|
* SplClosureContainer::__isset
|
53
|
|
|
*
|
54
|
|
|
* A convenient way to checks if the registry has a specific closure object,
|
55
|
|
|
* by doing isset( $ioc->offset )
|
56
|
|
|
*
|
57
|
|
|
* @param string $offset
|
58
|
|
|
*
|
59
|
|
|
* @return bool
|
60
|
|
|
*/
|
61
|
|
|
public function __isset($offset)
|
62
|
|
|
{
|
63
|
|
|
return $this->has($offset);
|
64
|
|
|
}
|
65
|
|
|
|
66
|
|
|
// ------------------------------------------------------------------------
|
67
|
|
|
|
68
|
|
|
/**
|
69
|
|
|
* SplClosureContainer::has
|
70
|
|
|
*
|
71
|
|
|
* Checks if the registry contains a specific closure object.
|
72
|
|
|
*
|
73
|
|
|
* @param string $offset
|
74
|
|
|
*
|
75
|
|
|
* @return bool
|
76
|
|
|
*/
|
77
|
|
|
public function has($offset)
|
78
|
|
|
{
|
79
|
|
|
return (bool)isset($this->closures[ $offset ]);
|
80
|
|
|
}
|
81
|
|
|
|
82
|
|
|
// ------------------------------------------------------------------------
|
83
|
|
|
|
84
|
|
|
/**
|
85
|
|
|
* SplClosureContainer::__unset
|
86
|
|
|
*
|
87
|
|
|
* A convenient way to removes a specific closure object
|
88
|
|
|
* by doing unset( $ioc->offset )
|
89
|
|
|
*
|
90
|
|
|
* @param string $offset
|
91
|
|
|
*/
|
92
|
|
|
public function __unset($offset)
|
93
|
|
|
{
|
94
|
|
|
$this->detach($offset);
|
95
|
|
|
}
|
96
|
|
|
|
97
|
|
|
// ------------------------------------------------------------------------
|
98
|
|
|
|
99
|
|
|
/**
|
100
|
|
|
* SplClosureContainer::detach
|
101
|
|
|
*
|
102
|
|
|
* Removes a specific closure object
|
103
|
|
|
*
|
104
|
|
|
* @param string $offset
|
105
|
|
|
*/
|
106
|
|
|
public function detach($offset)
|
107
|
|
|
{
|
108
|
|
|
if (isset($this->closures[ $offset ])) {
|
109
|
|
|
unset($this->closures[ $offset ]);
|
110
|
|
|
}
|
111
|
|
|
}
|
112
|
|
|
|
113
|
|
|
// ------------------------------------------------------------------------
|
114
|
|
|
|
115
|
|
|
/**
|
116
|
|
|
* SplClosureContainer::__call
|
117
|
|
|
*
|
118
|
|
|
* A convenient way to access the closure object result
|
119
|
|
|
* by doing $ioc->offset('foo', 'bar');
|
120
|
|
|
*
|
121
|
|
|
* @param string $offset
|
122
|
|
|
* @param array $arguments
|
123
|
|
|
*
|
124
|
|
|
* @return mixed Returns FALSE when calling the closure is failed.
|
125
|
|
|
*/
|
126
|
|
|
public function __call($offset, array $arguments = [])
|
127
|
|
|
{
|
128
|
|
|
return $this->get($offset, $arguments);
|
129
|
|
|
}
|
130
|
|
|
|
131
|
|
|
// ------------------------------------------------------------------------
|
132
|
|
|
|
133
|
|
|
/**
|
134
|
|
|
* SplClosureContainer::get
|
135
|
|
|
*
|
136
|
|
|
* Returns the closure object result.
|
137
|
|
|
*
|
138
|
|
|
* @param string $offset
|
139
|
|
|
* @param array $arguments
|
140
|
|
|
*
|
141
|
|
|
* @return mixed Returns FALSE when calling the closure is failed.
|
142
|
|
|
*/
|
143
|
|
|
public function get($offset, array $arguments = [])
|
144
|
|
|
{
|
145
|
|
|
return isset($this->closures[ $offset ]) ? call_user_func_array(
|
146
|
|
|
$this->closures[ $offset ],
|
147
|
|
|
$arguments
|
148
|
|
|
) : false;
|
149
|
|
|
}
|
150
|
|
|
|
151
|
|
|
// ------------------------------------------------------------------------
|
152
|
|
|
|
153
|
|
|
/**
|
154
|
|
|
* SplClosureContainer::count
|
155
|
|
|
*
|
156
|
|
|
* Count elements of an object
|
157
|
|
|
*
|
158
|
|
|
* @link http://php.net/manual/en/countable.count.php
|
159
|
|
|
* @return int The custom count as an integer.
|
160
|
|
|
* </p>
|
161
|
|
|
* <p>
|
162
|
|
|
* The return value is cast to an integer.
|
163
|
|
|
* @since 5.1.0
|
164
|
|
|
*/
|
165
|
|
|
public function count()
|
166
|
|
|
{
|
167
|
|
|
return count($this->closures);
|
168
|
|
|
}
|
169
|
|
|
} |