ServiceLocator.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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\DependencyInjection\Argument;
  11. use Symfony\Component\DependencyInjection\ServiceLocator as BaseServiceLocator;
  12. /**
  13. * @author Nicolas Grekas <p@tchwork.com>
  14. *
  15. * @internal
  16. */
  17. class ServiceLocator extends BaseServiceLocator
  18. {
  19. private $factory;
  20. private $serviceMap;
  21. private $serviceTypes;
  22. public function __construct(\Closure $factory, array $serviceMap, array $serviceTypes = null)
  23. {
  24. $this->factory = $factory;
  25. $this->serviceMap = $serviceMap;
  26. $this->serviceTypes = $serviceTypes;
  27. parent::__construct($serviceMap);
  28. }
  29. /**
  30. * {@inheritdoc}
  31. *
  32. * @return mixed
  33. */
  34. public function get($id)
  35. {
  36. return isset($this->serviceMap[$id]) ? ($this->factory)(...$this->serviceMap[$id]) : parent::get($id);
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function getProvidedServices(): array
  42. {
  43. return $this->serviceTypes ?? $this->serviceTypes = array_map(function () { return '?'; }, $this->serviceMap);
  44. }
  45. }