Source of file AbstractWorkerSingleton.php
Size: 4,017 Bytes - Last Modified: 2021-01-12T22:04:13+00:00
C:/Users/MAKS/Code/_PROJECTS/amqp-agent/src/Worker/AbstractWorkerSingleton.php
12345678910111213141516171819202122232425262728293031323334353637383940414243444546
Covered by 7 test(s):
47
Covered by 7 test(s):
4849
Covered by 7 test(s):
5051
Covered by 7 test(s):
52
Covered by 7 test(s):
5354
Covered by 7 test(s):
55
Covered by 1 test(s):
56
Covered by 1 test(s):
5758
Covered by 1 test(s):
59
Covered by 1 test(s):
60
Covered by 1 test(s):
61
Covered by 1 test(s):
62
Covered by 1 test(s):
63
Covered by 1 test(s):
64
Covered by 1 test(s):
6566676869
Covered by 7 test(s):
7071727374757677787980
Covered by 3 test(s):
81
Covered by 1 test(s):
82
Covered by 3 test(s):
83
Covered by 1 test(s):
848586
Covered by 2 test(s):
8788899091929394959697
Covered by 2 test(s):
98
Covered by 1 test(s):
99
Covered by 1 test(s):
100101102
Covered by 1 test(s):
103
Covered by 1 test(s):
104105106107108109110111112113
Covered by 3 test(s):
114
Covered by 3 test(s):
115116117
Covered by 3 test(s):
118
Covered by 3 test(s):
119120121
Covered by 3 test(s):
122123124125126127128129130131132
Covered by 1 test(s):
133
Covered by 1 test(s):
134135
Covered by 1 test(s):
136137138
| <?php /** * @author Marwan Al-Soltany <MarwanAlsoltany@gmail.com> * @copyright Marwan Al-Soltany 2020 * For the full copyright and license information, please view * the LICENSE file that was distributed with this source code. */ declare(strict_types=1); namespace MAKS\AmqpAgent\Worker; use ReflectionClass; use MAKS\AmqpAgent\Helper\Singleton; use MAKS\AmqpAgent\Worker\AbstractWorker; /** * An abstract decorator class implementing mapping functions (proxy functions) to turn a normal worker into a singleton. * @since 1.0.0 */ abstract class AbstractWorkerSingleton extends Singleton { /** * The full qualified name of the instantiated class. * @var string */ protected static $class; /** * The instance of the worker class (a class that extends AbstractWorker). * Sub-classes of this class should instantiate a worker and set it * to the protected $worker property in their __construct() method. * @var AbstractWorker */ protected $worker; /** * Returns an instance of the class this method was called on. * @param array ...$arguments The same arguments of the normal worker. * @return self */ public static function getInstance() { $worker = parent::getInstance(); $workerReference = $worker->worker; static::$class = get_class($workerReference); $arguments = func_get_args(); $argsCount = func_num_args(); if ($argsCount > 0) { $reflection = new ReflectionClass($workerReference); $properties = $reflection->getConstructor()->getParameters(); $index = 0; foreach ($properties as $property) { $member = $property->getName(); $workerReference->{$member} = $arguments[$index]; $index++; if ($index === $argsCount) { break; } } } return $worker; } /** * Gets a class member via public property access notation. * @param string $member Property name. * @return mixed */ public function __get(string $member) { if (defined(static::$class . '::' . $member)) { return constant(static::$class . '::' . $member); } elseif (isset(static::$class::$$member)) { return static::$class::$$member; } return $this->worker->$member; } /** * Sets a class member via public property assignment notation. * @param string $member Property name. * @param mixed $value Override for object property or a static property. * @return void */ public function __set(string $member, $value) { if (isset(static::$class::$$member)) { static::$class::$$member = $value; return; } $this->worker->{$member} = $value; } /** * Calls a method on a class that extend AbstractWorker and throws an exception for calls to undefined methods. * @param string $method Function name. * @param array $arguments Function arguments. * @return mixed */ public function __call(string $method, array $arguments) { $function = [$this->worker, $method]; $return = call_user_func_array($function, $arguments); // check to return the right object to allow for trouble-free chaining. if ($return instanceof $this->worker) { return $this; } return $return; } /** * Calls a method on a class that extend AbstractWorker and throws an exception for calls to undefined static methods. * @param string $method Function name. * @param array $arguments Function arguments. * @return mixed */ public static function __callStatic(string $method, array $arguments) { $function = [static::$class, $method]; $return = forward_static_call_array($function, $arguments); return $return; } } |