1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Patron package. |
5
|
|
|
* |
6
|
|
|
* (c) Olivier Laviale <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Patron; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* A function collection |
16
|
|
|
*/ |
17
|
|
|
class FunctionCollection implements \ArrayAccess |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
private $collection = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param array $functions |
26
|
|
|
*/ |
27
|
|
|
public function __construct(array $functions = []) |
28
|
|
|
{ |
29
|
|
|
foreach ($functions as $name => $definition) |
30
|
|
|
{ |
31
|
|
|
$this[$name] = $definition; |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Calls a function. |
37
|
|
|
* |
38
|
|
|
* @param string $method Function name. |
39
|
|
|
* @param array $arguments Function arguments. |
40
|
|
|
* |
41
|
|
|
* @return mixed |
42
|
|
|
*/ |
43
|
|
|
public function __call($method, $arguments) |
44
|
|
|
{ |
45
|
|
|
$f = $this->find($method); |
46
|
|
|
|
47
|
|
|
if (!$f) |
48
|
|
|
{ |
49
|
|
|
throw new FunctionNotDefined([ $method, $this ]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return call_user_func_array($f, $arguments); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Whether a function exists. |
57
|
|
|
* |
58
|
|
|
* @param string $name Function name. |
59
|
|
|
* |
60
|
|
|
* @return boolean `true` if the function exists, `false` otherwise. |
61
|
|
|
*/ |
62
|
|
|
public function offsetExists($name) |
63
|
|
|
{ |
64
|
|
|
return isset($this->collection[$name]); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Returns a function definition. |
69
|
|
|
* |
70
|
|
|
* @param string $name Function name. |
71
|
|
|
* |
72
|
|
|
* @return array The function's definition. |
73
|
|
|
* |
74
|
|
|
* @throws FunctionNotDefined when the function is not defined. |
75
|
|
|
*/ |
76
|
|
|
public function offsetGet($name) |
77
|
|
|
{ |
78
|
|
|
if (!$this->offsetExists($name)) |
79
|
|
|
{ |
80
|
|
|
throw new FunctionNotDefined([ $name, $this ]); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $this->collection[$name]; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Sets a function definition. |
88
|
|
|
* |
89
|
|
|
* @param string $name Function name. |
90
|
|
|
* @param array $definition Function definition. |
91
|
|
|
*/ |
92
|
|
|
public function offsetSet($name, $definition) |
93
|
|
|
{ |
94
|
|
|
$this->collection[$name] = $definition; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Removes a function. |
99
|
|
|
* |
100
|
|
|
* @param string $name Function name. |
101
|
|
|
*/ |
102
|
|
|
public function offsetUnset($name) |
103
|
|
|
{ |
104
|
|
|
unset($this->collection[$name]); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Finds a function. |
109
|
|
|
* |
110
|
|
|
* @param string $name Function name. |
111
|
|
|
* |
112
|
|
|
* @return callable|false A callable is the function is defined, `false` otherwise. |
113
|
|
|
*/ |
114
|
|
|
public function find($name) |
115
|
|
|
{ |
116
|
|
|
if (isset($this->collection[$name])) |
117
|
|
|
{ |
118
|
|
|
return $this->collection[$name]; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$try = 'ICanBoogie\\' . $name; |
122
|
|
|
|
123
|
|
|
if (function_exists($try)) |
124
|
|
|
{ |
125
|
|
|
return $try; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$try = 'ICanBoogie\I18n\\' . $name; |
129
|
|
|
|
130
|
|
|
if (function_exists($try)) |
131
|
|
|
{ |
132
|
|
|
return $try; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$try = 'Patron\\' . $name; |
136
|
|
|
|
137
|
|
|
if (function_exists($try)) |
138
|
|
|
{ |
139
|
|
|
return $try; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return false; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|