LazyProxyTrait.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Di\LazyLoader;
  12. use Hyperf\Context\ApplicationContext;
  13. trait LazyProxyTrait
  14. {
  15. public function __construct()
  16. {
  17. $vars = get_object_vars($this);
  18. foreach (array_keys($vars) as $var) {
  19. unset($this->{$var});
  20. }
  21. }
  22. public function __call($method, $arguments)
  23. {
  24. $obj = $this->getInstance();
  25. return call_user_func([$obj, $method], ...$arguments);
  26. }
  27. public function __get($name)
  28. {
  29. return $this->getInstance()->{$name};
  30. }
  31. public function __set($name, $value)
  32. {
  33. $this->getInstance()->{$name} = $value;
  34. }
  35. public function __isset($name)
  36. {
  37. return isset($this->getInstance()->{$name});
  38. }
  39. public function __unset($name)
  40. {
  41. unset($this->getInstance()->{$name});
  42. }
  43. public function __wakeup()
  44. {
  45. $vars = get_object_vars($this);
  46. foreach (array_keys($vars) as $var) {
  47. unset($this->{$var});
  48. }
  49. }
  50. /**
  51. * Return The Proxy Target.
  52. * @return mixed
  53. */
  54. public function getInstance()
  55. {
  56. return ApplicationContext::getContainer()->get(self::PROXY_TARGET);
  57. }
  58. }