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-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
/**
2016-03-25 09:46:04 +00:00
* @ var 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
2016-03-25 09:46:04 +00:00
public function __construct ( ArgumentMetadataFactoryInterface $argumentMetadataFactory = null , array $argumentValueResolvers = array ())
{
$this -> argumentMetadataFactory = $argumentMetadataFactory ;
$this -> argumentValueResolvers = $argumentValueResolvers ;
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
{
$arguments = array ();
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 );
if ( ! $resolved instanceof \Generator ) {
throw new \InvalidArgumentException ( sprintf ( '%s::resolve() must yield at least one value.' , get_class ( $resolver )));
}
foreach ( $resolved as $append ) {
$arguments [] = $append ;
2016-03-16 07:56:41 +00:00
}
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 ;
if ( is_array ( $representative )) {
$representative = sprintf ( '%s::%s()' , get_class ( $representative [ 0 ]), $representative [ 1 ]);
} elseif ( is_object ( $representative )) {
$representative = get_class ( $representative );
}
throw new \RuntimeException ( sprintf ( 'Controller "%s" requires that you provide a value for the "$%s" argument (because there is no default value or because there is a non optional argument after this one).' , $representative , $metadata -> getName ()));
2016-03-16 07:56:41 +00:00
}
return $arguments ;
}
}