-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDecorator.php
144 lines (112 loc) · 4.22 KB
/
Decorator.php
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
namespace Sagittaracc\PhpPythonDecorator;
use Closure;
use ReflectionClass;
use Sagittaracc\PhpPythonDecorator\exceptions\DecoratorError;
use function get_real_name;
/**
* @author Yuriy Arutyunyan <[email protected]>
*/
trait Decorator
{
public array $scope = [
'modules' => [],
];
public function __call($func, $args)
{
// if ($this->scope['__attributes__']->hasCache($func)) {
// $attributes = $this->scope['__attributes__']->getCache($func);
// }
// else {
$class = new ReflectionClass($this);
$func = get_real_name($func);
$method = $class->getMethod($func);
if (!($method->isPublic())) {
throw new DecoratorError('Only public methods can be decorated!');
}
$params = $method->getParameters();
foreach ($params as $paramIndex => $param) {
$paramAttrs = $param->getAttributes();
foreach ($paramAttrs as $paramAttr) {
$paramAttrInst = $paramAttr->newInstance();
if ($paramAttrInst instanceof PythonDecorator) {
$paramAttrInst->bindTo($this)->wrapper($args[$paramIndex]);
}
}
}
$attributes = $method->getAttributes();
// }
$f = [$this, $func];
foreach (array_reverse($attributes) as $attribute) {
$instance = $attribute->newInstance();
if ($instance instanceof PythonDecorator) {
$instance->bindTo($this, $func);
$f = $instance->wrapper($f);
}
}
return call_user_func_array($f, $args);
}
public function __invoke(...$args)
{
$class = new ReflectionClass($this);
$attributes = $class->getAttributes();
$f = $this;
foreach (array_reverse($attributes) as $attribute) {
$instance = $attribute->newInstance();
if ($instance instanceof PythonDecorator) {
$instance->bindTo($this);
$f = $instance->wrapper($f);
}
}
return
is_callable($f) && $f instanceof Closure
? $f(...$args)
: $f;
}
public function __get($name)
{
$class = new ReflectionClass($this);
$name = get_real_name($name);
$property = $class->getProperty($name);
if (!($property->isPublic())) {
throw new DecoratorError('Only public properties can be decorated!');
}
// COMMENT: Первый атрибут проперти обрабатывается по особенному
// 1. Если он отнаследован от PythonDecorator то в него проперти оборачивается
// 2. Если от другого класса то его инстанс присваивается этому проперти
$attributes = array_reverse($property->getAttributes());
$f = $this->$name ?? null;
$firstAttribute = array_shift($attributes);
$firstInstance = $firstAttribute->newInstance();
$f = $firstInstance instanceof PythonDecorator
? $firstInstance->bindTo($this, $name)->wrapper($f)
: $firstInstance;
foreach ($attributes as $attribute) {
$instance = $attribute->newInstance();
if ($instance instanceof PythonDecorator) {
$instance->bindTo($this, $name);
$f = $instance->wrapper($f);
}
}
$this->$name = $f;
return $this->$name;
}
public function __set($name, $value)
{
$class = new ReflectionClass($this);
$name = get_real_name($name);
$property = $class->getProperty($name);
if (!($property->isPublic())) {
throw new DecoratorError('Only public properties can be validated!');
}
$attributes = $property->getAttributes();
$f = $value;
foreach ($attributes as $attribute) {
$instance = $attribute->newInstance();
if ($instance instanceof PythonDecorator) {
$instance->bindTo($this, $name)->wrapper($f);
}
}
$this->$name = $f;
}
}