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