RedisProxyTrait.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Cache\Traits;
  11. /**
  12. * @internal
  13. */
  14. trait RedisProxyTrait
  15. {
  16. private \Closure $initializer;
  17. private ?parent $realInstance = null;
  18. public static function createLazyProxy(\Closure $initializer, ?self $instance = null): static
  19. {
  20. $instance ??= (new \ReflectionClass(static::class))->newInstanceWithoutConstructor();
  21. $instance->realInstance = null;
  22. $instance->initializer = $initializer;
  23. return $instance;
  24. }
  25. public function isLazyObjectInitialized(bool $partial = false): bool
  26. {
  27. return isset($this->realInstance);
  28. }
  29. public function initializeLazyObject(): object
  30. {
  31. return $this->realInstance ??= ($this->initializer)();
  32. }
  33. public function resetLazyObject(): bool
  34. {
  35. $this->realInstance = null;
  36. return true;
  37. }
  38. public function __destruct()
  39. {
  40. }
  41. }