2010-08-09 14:21:59 +01:00
< ? php
/*
2011-01-15 13:29:43 +00:00
* This file is part of the Symfony package .
2010-08-09 14:21:59 +01:00
*
2011-03-06 11:40:06 +00:00
* ( c ) Fabien Potencier < fabien @ symfony . com >
2010-08-09 14:21:59 +01:00
*
2011-01-15 13:29:43 +00:00
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
2010-08-09 14:21:59 +01:00
*/
2011-01-15 13:29:43 +00:00
namespace Symfony\Component\HttpKernel\Controller ;
2013-01-09 09:41:17 +00:00
use Psr\Log\LoggerInterface ;
2011-01-15 13:29:43 +00:00
use Symfony\Component\HttpFoundation\Request ;
2010-08-09 14:21:59 +01:00
/**
* ControllerResolver .
*
* This implementation uses the '_controller' request attribute to determine
* the controller to execute and uses the request attributes to determine
* the controller method arguments .
*
2011-03-06 11:40:06 +00:00
* @ author Fabien Potencier < fabien @ symfony . com >
2011-07-20 09:14:31 +01:00
*
* @ api
2010-08-09 14:21:59 +01:00
*/
class ControllerResolver implements ControllerResolverInterface
{
2011-03-23 18:47:16 +00:00
private $logger ;
2010-08-09 14:21:59 +01:00
/**
* Constructor .
*
* @ param LoggerInterface $logger A LoggerInterface instance
*/
public function __construct ( LoggerInterface $logger = null )
{
$this -> logger = $logger ;
}
/**
* Returns the Controller instance associated with a Request .
*
* This method looks for a '_controller' request attribute that represents
2011-01-23 11:10:45 +00:00
* the controller name ( a string like ClassName :: MethodName ) .
2010-08-09 14:21:59 +01:00
*
* @ param Request $request A Request instance
*
* @ return mixed | Boolean A PHP callable representing the Controller ,
* or false if this resolver is not able to determine the controller
*
* @ throws \InvalidArgumentException | \LogicException If the controller can ' t be found
2011-07-20 09:14:31 +01:00
*
* @ api
2010-08-09 14:21:59 +01:00
*/
public function getController ( Request $request )
{
if ( ! $controller = $request -> attributes -> get ( '_controller' )) {
if ( null !== $this -> logger ) {
2013-01-08 22:27:26 +00:00
$this -> logger -> warning ( 'Unable to look for the controller as the "_controller" parameter is missing' );
2010-08-09 14:21:59 +01:00
}
return false ;
}
2014-03-26 18:20:25 +00:00
if ( is_array ( $controller )) {
2010-09-16 09:46:38 +01:00
return $controller ;
}
2014-03-26 18:20:25 +00:00
if ( is_object ( $controller )) {
if ( method_exists ( $controller , '__invoke' )) {
return $controller ;
}
throw new \InvalidArgumentException ( sprintf ( 'Controller "%s" for URI "%s" is not callable.' , get_class ( $controller ), $request -> getPathInfo ()));
}
2012-02-11 23:34:53 +00:00
if ( false === strpos ( $controller , ':' )) {
if ( method_exists ( $controller , '__invoke' )) {
2014-02-11 07:51:18 +00:00
return new $controller ();
2012-02-11 23:34:53 +00:00
} elseif ( function_exists ( $controller )) {
return $controller ;
}
2011-08-08 22:16:52 +01:00
}
2013-04-20 21:07:10 +01:00
$callable = $this -> createController ( $controller );
2010-08-09 14:21:59 +01:00
2013-04-20 21:07:10 +01:00
if ( ! is_callable ( $callable )) {
2014-03-26 18:20:25 +00:00
throw new \InvalidArgumentException ( sprintf ( 'Controller "%s" for URI "%s" is not callable.' , $controller , $request -> getPathInfo ()));
2010-08-09 14:21:59 +01:00
}
2013-04-20 21:07:10 +01:00
return $callable ;
2010-08-09 14:21:59 +01:00
}
/**
* Returns the arguments to pass to the controller .
*
* @ param Request $request A Request instance
* @ param mixed $controller A PHP callable
*
2012-12-16 12:02:54 +00:00
* @ return array
*
2010-08-09 14:21:59 +01:00
* @ throws \RuntimeException When value for argument given is not provided
2011-07-20 09:14:31 +01:00
*
* @ api
2010-08-09 14:21:59 +01:00
*/
public function getArguments ( Request $request , $controller )
{
2010-09-16 09:46:38 +01:00
if ( is_array ( $controller )) {
2011-01-29 14:36:57 +00:00
$r = new \ReflectionMethod ( $controller [ 0 ], $controller [ 1 ]);
2011-11-24 07:39:49 +00:00
} elseif ( is_object ( $controller ) && ! $controller instanceof \Closure ) {
$r = new \ReflectionObject ( $controller );
$r = $r -> getMethod ( '__invoke' );
2010-09-16 09:46:38 +01:00
} else {
2011-01-29 14:36:57 +00:00
$r = new \ReflectionFunction ( $controller );
2010-09-16 09:46:38 +01:00
}
2010-08-09 14:21:59 +01:00
2011-08-08 22:20:53 +01:00
return $this -> doGetArguments ( $request , $controller , $r -> getParameters ());
}
protected function doGetArguments ( Request $request , $controller , array $parameters )
{
$attributes = $request -> attributes -> all ();
2010-08-09 14:21:59 +01:00
$arguments = array ();
2011-08-08 22:20:53 +01:00
foreach ( $parameters as $param ) {
2012-06-12 13:23:19 +01:00
if ( array_key_exists ( $param -> name , $attributes )) {
$arguments [] = $attributes [ $param -> name ];
2011-04-16 15:26:13 +01:00
} elseif ( $param -> getClass () && $param -> getClass () -> isInstance ( $request )) {
$arguments [] = $request ;
2010-08-09 14:21:59 +01:00
} elseif ( $param -> isDefaultValueAvailable ()) {
$arguments [] = $param -> getDefaultValue ();
} else {
2011-08-08 22:20:53 +01:00
if ( is_array ( $controller )) {
$repr = sprintf ( '%s::%s()' , get_class ( $controller [ 0 ]), $controller [ 1 ]);
} elseif ( is_object ( $controller )) {
$repr = get_class ( $controller );
} else {
$repr = $controller ;
}
2012-06-12 13:23:19 +01:00
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).' , $repr , $param -> name ));
2010-08-09 14:21:59 +01:00
}
}
return $arguments ;
}
/**
* Returns a callable for the given controller .
*
* @ param string $controller A Controller string
*
* @ return mixed A PHP callable
2012-12-16 12:02:54 +00:00
*
* @ throws \InvalidArgumentException
2010-08-09 14:21:59 +01:00
*/
protected function createController ( $controller )
{
if ( false === strpos ( $controller , '::' )) {
throw new \InvalidArgumentException ( sprintf ( 'Unable to find controller "%s".' , $controller ));
}
2011-12-11 20:16:22 +00:00
list ( $class , $method ) = explode ( '::' , $controller , 2 );
2010-08-09 14:21:59 +01:00
if ( ! class_exists ( $class )) {
throw new \InvalidArgumentException ( sprintf ( 'Class "%s" does not exist.' , $class ));
}
return array ( new $class (), $method );
}
}