vendor/symfony/dependency-injection/Argument/ServiceLocator.php line 40

  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 \Closure $factory;
  20.     private array $serviceMap;
  21.     private ?array $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.     public function get(string $id): mixed
  30.     {
  31.         return match (\count($this->serviceMap[$id] ?? [])) {
  32.             => parent::get($id),
  33.             => $this->serviceMap[$id][0],
  34.             default => ($this->factory)(...$this->serviceMap[$id]),
  35.         };
  36.     }
  37.     public function getProvidedServices(): array
  38.     {
  39.         return $this->serviceTypes ??= array_map(fn () => '?'$this->serviceMap);
  40.     }
  41. }