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
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Trait Axessors. |
15
|
|
|
* |
16
|
|
|
* This trait is used to indicate that a class uses the library and provides them with method call handlers. |
17
|
|
|
* If you wouldn't use method handling functionality, you can add trait {@see Axessors\Axs} to your class. |
18
|
|
|
* |
19
|
|
|
* @package NoOne4rever\Axessors |
20
|
|
|
*/ |
21
|
|
|
trait Axessors |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Redirects an instance method call. |
25
|
|
|
* |
26
|
|
|
* @param string $method the name of the called method |
27
|
|
|
* @param array $args arguments of the called method |
28
|
|
|
* @return mixed result of the called method |
29
|
|
|
* @throws ReThrownError is thrown when there is an error in *injected* callback or condition code |
30
|
|
|
*/ |
31
|
76 |
|
public function __call(string $method, array $args) |
32
|
|
|
{ |
33
|
76 |
|
return self::__axessorsCall($method, $args, $this); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Redirects a static method call. |
38
|
|
|
* |
39
|
|
|
* @param string $method the name of the called method |
40
|
|
|
* @param array $args arguments of the called method |
41
|
|
|
* @return mixed result of the called method |
42
|
|
|
* @throws ReThrownError is thrown when there is an error in *injected* callback or condition code |
43
|
|
|
*/ |
44
|
1 |
|
public static function __callStatic(string $method, array $args) |
45
|
|
|
{ |
46
|
1 |
|
return self::__axessorsCall($method, $args); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Executes *injected* callback or condition. |
51
|
|
|
* |
52
|
|
|
* @param string $code code to execute |
53
|
|
|
* @param mixed $_var value to process |
54
|
|
|
* @param bool $mode mode of execution |
55
|
|
|
* @return mixed the result or condition or callback |
56
|
|
|
* @throws ReThrownError if an error occurred while evaluating *injected* callback or condition |
57
|
|
|
*/ |
58
|
22 |
|
public function __axessorsExecute(string $code, $_var, bool $mode) |
59
|
|
|
{ |
60
|
22 |
|
$var = $_var; |
61
|
|
|
try { |
62
|
22 |
|
$result = (bool)eval('return ' . $code . ';'); // evaluation of the code written in Axessors comment |
63
|
3 |
|
} catch (\Throwable $error) { |
64
|
3 |
|
throw new ReThrownError("an error occurred while evaluating executable string \"$code\": {$error->getMessage()}"); |
65
|
|
|
} |
66
|
19 |
|
return $mode ? $result : $var; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Redirects Axessors method call. |
71
|
|
|
* |
72
|
|
|
* @param string $method method name |
73
|
|
|
* @param array $args method arguments |
74
|
|
|
* @param object|null $object object for instance method call |
75
|
|
|
* @return mixed the result of called method |
76
|
|
|
*/ |
77
|
77 |
|
private static function __axessorsCall(string $method, array $args, $object = null) |
78
|
|
|
{ |
79
|
77 |
|
$callProcessor = new CallProcessor(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), $object); |
80
|
77 |
|
return $callProcessor->call($args, $method); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|