Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / symfony / http-kernel / Controller / ArgumentResolver / ServiceValueResolver.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\HttpKernel\Controller\ArgumentResolver;
13
14 use Psr\Container\ContainerInterface;
15 use Symfony\Component\HttpFoundation\Request;
16 use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
17 use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
18
19 /**
20  * Yields a service keyed by _controller and argument name.
21  *
22  * @author Nicolas Grekas <p@tchwork.com>
23  */
24 final class ServiceValueResolver implements ArgumentValueResolverInterface
25 {
26     private $container;
27
28     public function __construct(ContainerInterface $container)
29     {
30         $this->container = $container;
31     }
32
33     /**
34      * {@inheritdoc}
35      */
36     public function supports(Request $request, ArgumentMetadata $argument)
37     {
38         $controller = $request->attributes->get('_controller');
39
40         if (\is_array($controller) && \is_callable($controller, true) && \is_string($controller[0])) {
41             $controller = $controller[0].'::'.$controller[1];
42         } elseif (!\is_string($controller) || '' === $controller) {
43             return false;
44         }
45
46         if ('\\' === $controller[0]) {
47             $controller = ltrim($controller, '\\');
48         }
49
50         return $this->container->has($controller) && $this->container->get($controller)->has($argument->getName());
51     }
52
53     /**
54      * {@inheritdoc}
55      */
56     public function resolve(Request $request, ArgumentMetadata $argument)
57     {
58         if (\is_array($controller = $request->attributes->get('_controller'))) {
59             $controller = $controller[0].'::'.$controller[1];
60         }
61
62         if ('\\' === $controller[0]) {
63             $controller = ltrim($controller, '\\');
64         }
65
66         yield $this->container->get($controller)->get($argument->getName());
67     }
68 }