This repository was archived by the owner on Jan 5, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtendable.php
More file actions
189 lines (166 loc) · 5.09 KB
/
Extendable.php
File metadata and controls
189 lines (166 loc) · 5.09 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
namespace Modulus\Support;
use Error;
use Closure;
use Modulus\Support\Errors\UndefinedMethodError;
use Modulus\Support\Errors\ExtendableArgumentError;
use Modulus\Support\Exceptions\CannotAddMethodException;
use Modulus\Support\Exceptions\CannotCallMethodException;
use Modulus\Support\Exceptions\CannotAddPropertyException;
use Modulus\Support\Exceptions\UndefinedPropertyErrorException;
trait Extendable
{
/**
* A list of custom functions
*
* @var array $functions
*/
public static $functions = [];
/**
* A list of custom static functions
*
* @var array $staticFunctions
*/
public static $staticFunctions = [];
/**
* A list of custom properties
*
* @var array $properties
*/
public static $properties = [];
/**
* Add custom function
*
* @param string $method The name of the method
* @param Closure $closure The callback that should be executed
* @throws CannotAddMethodException|CannotCallMethodException
* @return mixed
*/
public static function bind(string $method, Closure $closure)
{
$trace = debug_backtrace()[1];
if (
$trace['class'] == 'Modulus\Framework\Upstart\Prototype' &&
$trace['function'] == 'bind' &&
count($trace['args']) == 3
) {
if (!array_key_exists($method, Self::$functions)) {
return Self::$functions[$method] = [$method => $closure];
}
throw new CannotAddMethodException;
}
throw new CannotCallMethodException('Call to ' . self::class . '::bind() is not allowed');
}
/**
* Add custom static function
*
* @param string $method The name of the method
* @param Closure $closure The callback that should be executed
* @throws CannotAddMethodException|CannotCallMethodException
* @return mixed
*/
public static function static(string $method, Closure $closure)
{
$trace = debug_backtrace()[1];
if (
$trace['class'] == 'Modulus\Framework\Upstart\Prototype' &&
$trace['function'] == 'static' &&
count($trace['args']) == 3
) {
if (!array_key_exists($method, Self::$staticFunctions)) {
return Self::$staticFunctions[$method] = [$method => $closure];
}
throw new CannotAddMethodException;
}
throw new CannotCallMethodException('Call to ' . self::class . '::static() is not allowed');
}
/**
* Add custom property
*
* @param string $property The name of the property
* @param Closure $closure The callback that should be executed
* @throws CannotAddPropertyException|CannotCallMethodException
* @return mixed
*/
public static function prop(string $property, Closure $closure)
{
$trace = debug_backtrace()[1];
if (
$trace['class'] == 'Modulus\Framework\Upstart\Prototype' &&
$trace['function'] == 'prop' &&
count($trace['args']) == 3
) {
if (!array_key_exists($property, Self::$properties)) {
return Self::$properties[$property] = [$property => $closure];
}
throw new CannotAddPropertyException;
}
throw new CannotCallMethodException('Call to ' . self::class . '::prop() is not allowed');
}
/**
* Call custom function
*
* @param string $method The name of the method
* @param array $args
* @throws ExtendableArgumentError|UndefinedMethodError
* @return mixed
*/
public function __call(string $method, array $args)
{
if (array_key_exists($method, Self::$functions)) {
try {
return call_user_func_array(Self::$functions[$method][$method], array_merge([$this], $args));
} catch (Error $e) {
throw new ExtendableArgumentError($e, self::class, $method);
}
}
/**
* Method does not exist
*/
throw new UndefinedMethodError('Call to undefined method ' . __CLASS__ . "::{$method}()");
}
/**
* Call custom static function
*
* @param string $method The name of the method
* @param array $args
* @throws ExtendableArgumentError|UndefinedMethodError
* @return mixed
*/
public static function __callStatic(string $method, array $args)
{
if (array_key_exists($method, Self::$staticFunctions)) {
try {
return call_user_func_array(Self::$staticFunctions[$method][$method], $args);
} catch (Error $e) {
throw new ExtendableArgumentError($e, self::class, $method, true);
}
}
/**
* Static method does not exist
*/
throw new UndefinedMethodError('Call to undefined method ' . __CLASS__ . "::{$method}()");
}
/**
* Call custom property
*
* @param string $property The name of the property
* @param array $args
* @throws ExtendableArgumentError|UndefinedPropertyErrorException
* @return mixed
*/
public function __get(string $property)
{
if (array_key_exists($property, Self::$properties)) {
try {
return call_user_func_array(Self::$properties[$property][$property], [$this]);
} catch (Error $e) {
throw new ExtendableArgumentError($e, self::class, $property);
}
}
/**
* Property does not exist
*/
throw new UndefinedPropertyErrorException('Undefined property ' . __CLASS__ . '::$' . $property);
}
}