1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is a part of "Axessors" library. |
4
|
|
|
* |
5
|
|
|
* @author <[email protected]> |
6
|
|
|
* @license GPL |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace NoOne4rever\Axessors; |
10
|
|
|
|
11
|
|
|
use NoOne4rever\Axessors\Exceptions\ReThrownError; |
12
|
|
|
use NoOne4rever\Axessors\Exceptions\TypeError; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Trait Axessors. |
16
|
|
|
* |
17
|
|
|
* This trait is used to indicate that a class uses the library and provides them with method call handlers. |
18
|
|
|
* If you wouldn't use method handling functionality, you can add trait {@see Axessors\Axs} to your class. |
19
|
|
|
* |
20
|
|
|
* @package NoOne4rever\Axessors |
21
|
|
|
*/ |
22
|
|
|
trait Axessors |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Redirects an instance method call. |
26
|
|
|
* |
27
|
|
|
* @param string $method the name of the called method |
28
|
|
|
* @param array $args arguments of the called method |
29
|
|
|
* @return mixed result of the called method |
30
|
|
|
* @throws ReThrownError is thrown when there is an error in *injected* callback or condition code |
31
|
|
|
*/ |
32
|
101 |
|
public function __call(string $method, array $args) |
33
|
|
|
{ |
34
|
101 |
|
return self::__axessorsCall($method, $args, $this); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Redirects a static method call. |
39
|
|
|
* |
40
|
|
|
* @param string $method the name of the called method |
41
|
|
|
* @param array $args arguments of the called method |
42
|
|
|
* @return mixed result of the called method |
43
|
|
|
* @throws ReThrownError is thrown when there is an error in *injected* callback or condition code |
44
|
|
|
*/ |
45
|
1 |
|
public static function __callStatic(string $method, array $args) |
46
|
|
|
{ |
47
|
1 |
|
return self::__axessorsCall($method, $args); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Executes *injected* callback or condition. |
52
|
|
|
* |
53
|
|
|
* @param string $code code to execute |
54
|
|
|
* @param $var mixed value to process |
55
|
|
|
* @param bool $mode mode of execution |
56
|
|
|
* @return mixed the result or condition or callback |
57
|
|
|
* @throws ReThrownError if an error occurred while evaluating *injected* callback or condition |
58
|
|
|
* @throws TypeError if non-countable value supplied to ConditionsRunner::count() |
59
|
|
|
*/ |
60
|
81 |
|
public function __axessorsExecute(string $code, $var, bool $mode) |
61
|
|
|
{ |
62
|
|
|
try { |
63
|
81 |
|
$result = (bool)eval('return ' . $code . ';'); // evaluation of the code written in Axessors comment |
|
|
|
|
64
|
4 |
|
} catch (TypeError $error) { |
|
|
|
|
65
|
1 |
|
throw $error; |
66
|
3 |
|
} catch (\Throwable $error) { |
67
|
3 |
|
throw new ReThrownError("an error occurred while evaluating executable string \"$code\": {$error->getMessage()}"); |
68
|
|
|
} |
69
|
79 |
|
return $mode ? $result : $var; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Executes *injected* callback or condition. |
74
|
|
|
* |
75
|
|
|
* @param string $code code to execute |
76
|
|
|
* @param $var mixed value to process |
77
|
|
|
* @param bool $mode mode of execution |
78
|
|
|
* @return mixed the result or condition or callback |
79
|
|
|
* @throws ReThrownError if an error occurred while evaluating *injected* callback or condition |
80
|
|
|
* @throws TypeError if non-countable value supplied to ConditionsRunner::count() |
81
|
|
|
*/ |
82
|
1 |
|
public static function __axessorsExecuteStatic(string $code, $var, bool $mode) |
83
|
|
|
{ |
84
|
|
|
try { |
85
|
1 |
|
$result = (bool)eval('return ' . $code . ';'); // evaluation of the code written in Axessors comment |
86
|
|
|
} catch (TypeError $error) { |
|
|
|
|
87
|
|
|
throw $error; |
88
|
|
|
} catch (\Throwable $error) { |
89
|
|
|
throw new ReThrownError("an error occurred while evaluating executable string \"$code\": {$error->getMessage()}"); |
90
|
|
|
} |
91
|
1 |
|
return $mode ? $result : $var; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Redirects Axessors method call. |
96
|
|
|
* |
97
|
|
|
* @param string $method method name |
98
|
|
|
* @param array $args method arguments |
99
|
|
|
* @param object|null $object object for instance method call |
100
|
|
|
* @return mixed the result of called method |
101
|
|
|
*/ |
102
|
102 |
|
private static function __axessorsCall(string $method, array $args, $object = null) |
103
|
|
|
{ |
104
|
102 |
|
$callProcessor = new CallProcessor(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), $object); |
105
|
102 |
|
return $callProcessor->call($args, $method); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|