ServicesResetter.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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\HttpKernel\DependencyInjection;
  11. use ProxyManager\Proxy\LazyLoadingInterface;
  12. use Symfony\Component\VarExporter\LazyObjectInterface;
  13. /**
  14. * Resets provided services.
  15. *
  16. * @author Alexander M. Turek <me@derrabus.de>
  17. * @author Nicolas Grekas <p@tchwork.com>
  18. *
  19. * @final since Symfony 7.2
  20. */
  21. class ServicesResetter implements ServicesResetterInterface
  22. {
  23. /**
  24. * @param \Traversable<string, object> $resettableServices
  25. * @param array<string, string|string[]> $resetMethods
  26. */
  27. public function __construct(
  28. private \Traversable $resettableServices,
  29. private array $resetMethods,
  30. ) {
  31. }
  32. public function reset(): void
  33. {
  34. foreach ($this->resettableServices as $id => $service) {
  35. if ($service instanceof LazyObjectInterface && !$service->isLazyObjectInitialized(true)) {
  36. continue;
  37. }
  38. if ($service instanceof LazyLoadingInterface && !$service->isProxyInitialized()) {
  39. continue;
  40. }
  41. if (\PHP_VERSION_ID >= 80400 && (new \ReflectionClass($service))->isUninitializedLazyObject($service)) {
  42. continue;
  43. }
  44. foreach ((array) $this->resetMethods[$id] as $resetMethod) {
  45. if ('?' === $resetMethod[0] && !method_exists($service, $resetMethod = substr($resetMethod, 1))) {
  46. continue;
  47. }
  48. $service->$resetMethod();
  49. }
  50. }
  51. }
  52. }