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. 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. private \Traversable $resettableServices;
  25. private array $resetMethods;
  26. /**
  27. * @param \Traversable<string, object> $resettableServices
  28. * @param array<string, string|string[]> $resetMethods
  29. */
  30. public function __construct(\Traversable $resettableServices, array $resetMethods)
  31. {
  32. $this->resettableServices = $resettableServices;
  33. $this->resetMethods = $resetMethods;
  34. }
  35. public function reset(): void
  36. {
  37. foreach ($this->resettableServices as $id => $service) {
  38. if ($service instanceof LazyObjectInterface && !$service->isLazyObjectInitialized(true)) {
  39. continue;
  40. }
  41. if ($service instanceof LazyLoadingInterface && !$service->isProxyInitialized()) {
  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. }