ManagerRegistry.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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\Bridge\Doctrine;
  11. use Doctrine\Persistence\AbstractManagerRegistry;
  12. use ProxyManager\Proxy\LazyLoadingInterface;
  13. use Symfony\Bridge\ProxyManager\LazyProxy\Instantiator\RuntimeInstantiator;
  14. use Symfony\Component\DependencyInjection\Container;
  15. /**
  16. * References Doctrine connections and entity/document managers.
  17. *
  18. * @author Lukas Kahwe Smith <smith@pooteeweet.org>
  19. */
  20. abstract class ManagerRegistry extends AbstractManagerRegistry
  21. {
  22. /**
  23. * @var Container
  24. */
  25. protected $container;
  26. /**
  27. * {@inheritdoc}
  28. *
  29. * @return object
  30. */
  31. protected function getService($name)
  32. {
  33. return $this->container->get($name);
  34. }
  35. /**
  36. * {@inheritdoc}
  37. *
  38. * @return void
  39. */
  40. protected function resetService($name)
  41. {
  42. if (!$this->container->initialized($name)) {
  43. return;
  44. }
  45. $manager = $this->container->get($name);
  46. if (!$manager instanceof LazyLoadingInterface) {
  47. throw new \LogicException('Resetting a non-lazy manager service is not supported. '.(interface_exists(LazyLoadingInterface::class) && class_exists(RuntimeInstantiator::class) ? sprintf('Declare the "%s" service as lazy.', $name) : 'Try running "composer require symfony/proxy-manager-bridge".'));
  48. }
  49. $manager->setProxyInitializer(\Closure::bind(
  50. function (&$wrappedInstance, LazyLoadingInterface $manager) use ($name) {
  51. if (isset($this->aliases[$name])) {
  52. $name = $this->aliases[$name];
  53. }
  54. if (isset($this->fileMap[$name])) {
  55. $wrappedInstance = $this->load($this->fileMap[$name]);
  56. } else {
  57. $wrappedInstance = $this->{$this->methodMap[$name]}(false);
  58. }
  59. $manager->setProxyInitializer(null);
  60. return true;
  61. },
  62. $this->container,
  63. Container::class
  64. ));
  65. }
  66. }