ServicesResetter.php 1.6 KB

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