1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: horat1us |
5
|
|
|
* Date: 5/8/17 |
6
|
|
|
* Time: 4:43 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Horat1us\MethodInjection; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class InjectMethods |
14
|
|
|
* @package Horat1us\MethodInjection |
15
|
|
|
*/ |
16
|
|
|
trait InjectMethods |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var \Closure[] |
20
|
|
|
*/ |
21
|
|
|
protected $injectedMethods; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* InjectMethods constructor. |
25
|
|
|
* |
26
|
|
|
* @param \Closure[] $injectedMethods |
27
|
|
|
*/ |
28
|
5 |
|
public function __construct(array $injectedMethods = []) |
29
|
|
|
{ |
30
|
5 |
|
foreach ($injectedMethods as $key => $injectedMethod) { |
31
|
1 |
|
$this->injectMethod($key, $injectedMethod); |
32
|
|
|
} |
33
|
5 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Inject method |
37
|
|
|
* |
38
|
|
|
* @param string $name |
39
|
|
|
* @param \Closure $closure |
40
|
|
|
* @return $this |
41
|
|
|
*/ |
42
|
4 |
|
public function injectMethod(string $name, \Closure $closure) |
43
|
|
|
{ |
44
|
4 |
|
$injected = clone $closure; |
45
|
|
|
|
46
|
4 |
|
$injected->bindTo($this); |
47
|
4 |
|
$this->injectedMethods[$name] = $injected; |
48
|
|
|
|
49
|
4 |
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Removing injected method |
54
|
|
|
* |
55
|
|
|
* @param string $name |
56
|
|
|
* @return $this |
57
|
|
|
*/ |
58
|
1 |
|
public function removeMethod(string $name) |
59
|
|
|
{ |
60
|
1 |
|
unset($this->injectedMethods[$name]); |
61
|
|
|
|
62
|
1 |
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Calling injected method |
67
|
|
|
* |
68
|
|
|
* @param $name |
69
|
|
|
* @param $arguments |
70
|
|
|
* @return mixed |
71
|
|
|
* @throws MethodNotFoundException |
72
|
|
|
*/ |
73
|
4 |
|
public function __call($name, $arguments) |
74
|
|
|
{ |
75
|
4 |
|
$method = $this->injectedMethods[$name] ?? null; |
76
|
4 |
|
if(!$method instanceof \Closure) { |
77
|
2 |
|
throw new MethodNotFoundException($this, $name, $arguments); |
78
|
|
|
} |
79
|
3 |
|
return call_user_func($method, ...$arguments); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* JS-style settings methods |
84
|
|
|
* |
85
|
|
|
* @param $name |
86
|
|
|
* @param $value |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
3 |
|
public function __set($name, $value) |
90
|
|
|
{ |
91
|
3 |
|
if($value instanceof \Closure) { |
92
|
3 |
|
$this->injectMethod($name, $value); |
93
|
3 |
|
return true; |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
$this->{$name} = $value; |
97
|
1 |
|
return false; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param $name |
102
|
|
|
* @return \Closure|mixed |
103
|
|
|
*/ |
104
|
1 |
|
public function __get($name) |
105
|
|
|
{ |
106
|
1 |
|
if(array_key_exists($name, $this->injectedMethods)) { |
107
|
1 |
|
return $this->injectedMethods[$name]; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $this->{$name}; |
111
|
|
|
} |
112
|
|
|
} |