Chris@14: Chris@14: * Chris@14: * For the full copyright and license information, please view the LICENSE Chris@14: * file that was distributed with this source code. Chris@14: */ Chris@14: Chris@14: namespace Symfony\Component\HttpKernel\Controller\ArgumentResolver; Chris@14: Chris@14: use Psr\Container\ContainerInterface; Chris@14: use Symfony\Component\HttpFoundation\Request; Chris@14: use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface; Chris@14: use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; Chris@14: Chris@14: /** Chris@14: * Yields a service keyed by _controller and argument name. Chris@14: * Chris@14: * @author Nicolas Grekas Chris@14: */ Chris@14: final class ServiceValueResolver implements ArgumentValueResolverInterface Chris@14: { Chris@14: private $container; Chris@14: Chris@14: public function __construct(ContainerInterface $container) Chris@14: { Chris@14: $this->container = $container; Chris@14: } Chris@14: Chris@14: /** Chris@14: * {@inheritdoc} Chris@14: */ Chris@14: public function supports(Request $request, ArgumentMetadata $argument) Chris@14: { Chris@14: $controller = $request->attributes->get('_controller'); Chris@14: Chris@14: if (\is_array($controller) && \is_callable($controller, true) && \is_string($controller[0])) { Chris@14: $controller = $controller[0].'::'.$controller[1]; Chris@16: } elseif (!\is_string($controller) || '' === $controller) { Chris@16: return false; Chris@14: } Chris@14: Chris@16: if ('\\' === $controller[0]) { Chris@16: $controller = ltrim($controller, '\\'); Chris@16: } Chris@16: Chris@17: if (!$this->container->has($controller) && false !== $i = strrpos($controller, ':')) { Chris@17: $controller = substr($controller, 0, $i).strtolower(substr($controller, $i)); Chris@17: } Chris@17: Chris@16: return $this->container->has($controller) && $this->container->get($controller)->has($argument->getName()); Chris@14: } Chris@14: Chris@14: /** Chris@14: * {@inheritdoc} Chris@14: */ Chris@14: public function resolve(Request $request, ArgumentMetadata $argument) Chris@14: { Chris@14: if (\is_array($controller = $request->attributes->get('_controller'))) { Chris@14: $controller = $controller[0].'::'.$controller[1]; Chris@14: } Chris@14: Chris@16: if ('\\' === $controller[0]) { Chris@16: $controller = ltrim($controller, '\\'); Chris@16: } Chris@16: Chris@17: if (!$this->container->has($controller)) { Chris@17: $i = strrpos($controller, ':'); Chris@17: $controller = substr($controller, 0, $i).strtolower(substr($controller, $i)); Chris@17: } Chris@17: Chris@14: yield $this->container->get($controller)->get($argument->getName()); Chris@14: } Chris@14: }