ServicesResetter.php 1.7 KB

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