2016-03-16 07:56:41 +00:00
< ? php
/*
* This file is part of the Symfony package .
*
* ( c ) Fabien Potencier < fabien @ symfony . com >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
namespace Symfony\Component\HttpKernel\Controller ;
use Symfony\Component\HttpFoundation\Request ;
2016-04-01 07:34:22 +01:00
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\DefaultValueResolver ;
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\RequestAttributeValueResolver ;
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\RequestValueResolver ;
2017-01-06 07:18:08 +00:00
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\SessionValueResolver ;
2016-04-01 07:34:22 +01:00
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\VariadicValueResolver ;
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadataFactory ;
2016-03-25 09:46:04 +00:00
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadataFactoryInterface ;
2016-03-16 07:56:41 +00:00
/**
2016-04-01 05:28:01 +01:00
* Responsible for resolving the arguments passed to an action .
2016-03-16 07:56:41 +00:00
*
2016-03-25 09:46:04 +00:00
* @ author Iltar van der Berg < kjarli @ gmail . com >
2016-03-16 07:56:41 +00:00
*/
2016-03-25 09:46:04 +00:00
final class ArgumentResolver implements ArgumentResolverInterface
2016-03-16 07:56:41 +00:00
{
2016-03-25 09:46:04 +00:00
private $argumentMetadataFactory ;
2016-03-16 07:56:41 +00:00
/**
2017-02-02 18:39:04 +00:00
* @ var iterable | ArgumentValueResolverInterface []
2016-03-16 07:56:41 +00:00
*/
2016-03-25 09:46:04 +00:00
private $argumentValueResolvers ;
2016-03-16 07:56:41 +00:00
2019-01-16 18:24:45 +00:00
public function __construct ( ArgumentMetadataFactoryInterface $argumentMetadataFactory = null , iterable $argumentValueResolvers = [])
2016-03-25 09:46:04 +00:00
{
2016-04-01 07:34:22 +01:00
$this -> argumentMetadataFactory = $argumentMetadataFactory ? : new ArgumentMetadataFactory ();
2016-06-09 15:25:32 +01:00
$this -> argumentValueResolvers = $argumentValueResolvers ? : self :: getDefaultArgumentValueResolvers ();
2016-03-16 07:56:41 +00:00
}
2016-03-25 09:46:04 +00:00
/**
* { @ inheritdoc }
*/
public function getArguments ( Request $request , $controller )
2016-03-16 07:56:41 +00:00
{
2019-01-16 18:24:45 +00:00
$arguments = [];
2016-03-25 09:46:04 +00:00
foreach ( $this -> argumentMetadataFactory -> createArgumentMetadata ( $controller ) as $metadata ) {
foreach ( $this -> argumentValueResolvers as $resolver ) {
if ( ! $resolver -> supports ( $request , $metadata )) {
continue ;
2016-03-16 07:56:41 +00:00
}
2016-03-25 09:46:04 +00:00
$resolved = $resolver -> resolve ( $request , $metadata );
2019-06-24 01:24:36 +01:00
$atLeastOne = false ;
2016-03-25 09:46:04 +00:00
foreach ( $resolved as $append ) {
2019-06-24 01:24:36 +01:00
$atLeastOne = true ;
2016-03-25 09:46:04 +00:00
$arguments [] = $append ;
2016-03-16 07:56:41 +00:00
}
2019-06-24 01:24:36 +01:00
if ( ! $atLeastOne ) {
throw new \InvalidArgumentException ( sprintf ( '%s::resolve() must yield at least one value.' , \get_class ( $resolver )));
}
2016-03-25 09:46:04 +00:00
// continue to the next controller argument
continue 2 ;
2016-03-16 07:56:41 +00:00
}
2016-03-25 09:46:04 +00:00
$representative = $controller ;
2018-07-26 09:45:46 +01:00
if ( \is_array ( $representative )) {
$representative = sprintf ( '%s::%s()' , \get_class ( $representative [ 0 ]), $representative [ 1 ]);
} elseif ( \is_object ( $representative )) {
$representative = \get_class ( $representative );
2016-03-25 09:46:04 +00:00
}
2016-08-31 09:55:27 +01:00
throw new \RuntimeException ( sprintf ( 'Controller "%s" requires that you provide a value for the "$%s" argument. Either the argument is nullable and no null value has been provided, no default value has been provided or because there is a non optional argument after this one.' , $representative , $metadata -> getName ()));
2016-03-16 07:56:41 +00:00
}
return $arguments ;
}
2016-06-09 15:25:32 +01:00
2017-08-05 15:43:05 +01:00
public static function getDefaultArgumentValueResolvers () : iterable
2016-06-09 15:25:32 +01:00
{
2019-01-16 18:24:45 +00:00
return [
2016-06-09 15:25:32 +01:00
new RequestAttributeValueResolver (),
new RequestValueResolver (),
2017-01-06 07:18:08 +00:00
new SessionValueResolver (),
2016-06-09 15:25:32 +01:00
new DefaultValueResolver (),
new VariadicValueResolver (),
2019-01-16 18:24:45 +00:00
];
2016-06-09 15:25:32 +01:00
}
2016-03-16 07:56:41 +00:00
}