1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Marwan Al-Soltany <[email protected]> |
5
|
|
|
* @copyright Marwan Al-Soltany 2020 |
6
|
|
|
* For the full copyright and license information, please view |
7
|
|
|
* the LICENSE file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace MAKS\AmqpAgent\Worker; |
13
|
|
|
|
14
|
|
|
use ReflectionClass; |
15
|
|
|
use MAKS\AmqpAgent\Helper\Singleton; |
16
|
|
|
use MAKS\AmqpAgent\Worker\AbstractWorker; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* An abstract decorator class implementing mapping functions (proxy functions) to turn a normal worker into a singleton. |
20
|
|
|
* @since 1.0.0 |
21
|
|
|
*/ |
22
|
|
|
abstract class AbstractWorkerSingleton extends Singleton |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* The full qualified name of the instantiated class. |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected static $class; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The instance of the worker class (a class that extends AbstractWorker). |
32
|
|
|
* Sub-classes of this class should instantiate a worker and set it |
33
|
|
|
* to the protected $worker property in their __construct() method. |
34
|
|
|
* @var AbstractWorker |
35
|
|
|
*/ |
36
|
|
|
protected $worker; |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Returns an instance of the class this method was called on. |
41
|
|
|
* @param array ...$arguments The same arguments of the normal worker. |
42
|
|
|
* @return self |
43
|
|
|
*/ |
44
|
7 |
|
public static function getInstance() |
45
|
|
|
{ |
46
|
7 |
|
$worker = parent::getInstance(); |
47
|
7 |
|
$workerReference = $worker->worker; |
48
|
|
|
|
49
|
7 |
|
static::$class = get_class($workerReference); |
50
|
|
|
|
51
|
7 |
|
$arguments = func_get_args(); |
52
|
7 |
|
$argsCount = func_num_args(); |
53
|
|
|
|
54
|
7 |
|
if ($argsCount > 0) { |
55
|
1 |
|
$reflection = new ReflectionClass($workerReference); |
56
|
1 |
|
$properties = $reflection->getConstructor()->getParameters(); |
57
|
|
|
|
58
|
1 |
|
$index = 0; |
59
|
1 |
|
foreach ($properties as $property) { |
60
|
1 |
|
$member = $property->getName(); |
61
|
1 |
|
$workerReference->{$member} = $arguments[$index]; |
62
|
1 |
|
$index++; |
63
|
1 |
|
if ($index === $argsCount) { |
64
|
1 |
|
break; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
7 |
|
return $worker; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Gets a class member via public property access notation. |
75
|
|
|
* @param string $member Property name. |
76
|
|
|
* @return mixed |
77
|
|
|
*/ |
78
|
3 |
|
public function __get(string $member) |
79
|
|
|
{ |
80
|
3 |
|
if (defined(static::$class . '::' . $member)) { |
81
|
1 |
|
return constant(static::$class . '::' . $member); |
82
|
3 |
|
} elseif (isset(static::$class::$$member)) { |
83
|
1 |
|
return static::$class::$$member; |
84
|
|
|
} |
85
|
|
|
|
86
|
2 |
|
return $this->worker->$member; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Sets a class member via public property assignment notation. |
91
|
|
|
* @param string $member Property name. |
92
|
|
|
* @param mixed $value Override for object property or a static property. |
93
|
|
|
* @return void |
94
|
|
|
*/ |
95
|
2 |
|
public function __set(string $member, $value) |
96
|
|
|
{ |
97
|
2 |
|
if (isset(static::$class::$$member)) { |
98
|
1 |
|
static::$class::$$member = $value; |
99
|
1 |
|
return; |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
$this->worker->{$member} = $value; |
103
|
1 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Calls a method on a class that extend AbstractWorker and throws an exception for calls to undefined methods. |
107
|
|
|
* @param string $method Function name. |
108
|
|
|
* @param array $arguments Function arguments. |
109
|
|
|
* @return mixed |
110
|
|
|
*/ |
111
|
3 |
|
public function __call(string $method, array $arguments) |
112
|
|
|
{ |
113
|
3 |
|
$function = [$this->worker, $method]; |
114
|
3 |
|
$return = call_user_func_array($function, $arguments); |
115
|
|
|
|
116
|
|
|
// check to return the right object to allow for trouble-free chaining. |
117
|
3 |
|
if ($return instanceof $this->worker) { |
118
|
3 |
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
3 |
|
return $return; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Calls a method on a class that extend AbstractWorker and throws an exception for calls to undefined static methods. |
126
|
|
|
* @param string $method Function name. |
127
|
|
|
* @param array $arguments Function arguments. |
128
|
|
|
* @return mixed |
129
|
|
|
*/ |
130
|
1 |
|
public static function __callStatic(string $method, array $arguments) |
131
|
|
|
{ |
132
|
1 |
|
$function = [static::$class, $method]; |
133
|
1 |
|
$return = forward_static_call_array($function, $arguments); |
134
|
|
|
|
135
|
1 |
|
return $return; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|