2010-08-09 14:21:59 +01:00
< ? php
2010-08-20 22:09:55 +01:00
namespace Symfony\Component\HttpKernel\Controller ;
2010-08-09 14:21:59 +01:00
2010-08-20 22:09:55 +01:00
use Symfony\Component\HttpKernel\Log\LoggerInterface ;
use Symfony\Component\HttpFoundation\Request ;
2010-08-09 14:21:59 +01:00
/*
* This file is part of the Symfony framework .
*
* ( c ) Fabien Potencier < fabien . potencier @ symfony - project . com >
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE .
*/
/**
* 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 .
*
* @ author Fabien Potencier < fabien . potencier @ symfony - project . com >
*/
class ControllerResolver implements ControllerResolverInterface
{
protected $logger ;
/**
* 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
* the controller name ( a string like ClassName ::: MethodName ) .
*
* @ 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
*/
public function getController ( Request $request )
{
if ( ! $controller = $request -> attributes -> get ( '_controller' )) {
if ( null !== $this -> logger ) {
$this -> logger -> err ( 'Unable to look for the controller as the "_controller" parameter is missing' );
}
return false ;
}
list ( $controller , $method ) = $this -> createController ( $controller );
if ( ! method_exists ( $controller , $method )) {
throw new \InvalidArgumentException ( sprintf ( 'Method "%s::%s" does not exist.' , get_class ( $controller ), $method ));
}
if ( null !== $this -> logger ) {
$this -> logger -> info ( sprintf ( 'Using controller "%s::%s"' , get_class ( $controller ), $method ));
}
return array ( $controller , $method );
}
/**
* Returns the arguments to pass to the controller .
*
* @ param Request $request A Request instance
* @ param mixed $controller A PHP callable
*
* @ throws \RuntimeException When value for argument given is not provided
*/
public function getArguments ( Request $request , $controller )
{
$attributes = $request -> attributes -> all ();
list ( $controller , $method ) = $controller ;
$r = new \ReflectionObject ( $controller );
$arguments = array ();
foreach ( $r -> getMethod ( $method ) -> getParameters () as $param ) {
if ( array_key_exists ( $param -> getName (), $attributes )) {
$arguments [] = $attributes [ $param -> getName ()];
} elseif ( $param -> isDefaultValueAvailable ()) {
$arguments [] = $param -> getDefaultValue ();
} else {
throw new \RuntimeException ( sprintf ( 'Controller "%s::%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).' , get_class ( $controller ), $method , $param -> getName ()));
}
}
return $arguments ;
}
/**
* Returns a callable for the given controller .
*
* @ param string $controller A Controller string
*
* @ return mixed A PHP callable
*/
protected function createController ( $controller )
{
if ( false === strpos ( $controller , '::' )) {
throw new \InvalidArgumentException ( sprintf ( 'Unable to find controller "%s".' , $controller ));
}
list ( $class , $method ) = explode ( '::' , $controller );
if ( ! class_exists ( $class )) {
throw new \InvalidArgumentException ( sprintf ( 'Class "%s" does not exist.' , $class ));
}
return array ( new $class (), $method );
}
}