ServiceLocator.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. public function get($id)
  33. {
  34. return isset($this->serviceMap[$id]) ? ($this->factory)(...$this->serviceMap[$id]) : parent::get($id);
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function getProvidedServices(): array
  40. {
  41. return $this->serviceTypes ?? $this->serviceTypes = array_map(function () { return '?'; }, $this->serviceMap);
  42. }
  43. }