2010-02-17 13:53:18 +00:00
< ? php
2010-05-06 11:04:50 +01:00
namespace Symfony\Components\HttpKernel ;
2010-02-17 13:53:18 +00:00
use Symfony\Components\EventDispatcher\Event ;
use Symfony\Components\EventDispatcher\EventDispatcher ;
2010-05-06 11:04:50 +01:00
use Symfony\Components\HttpKernel\Exception\NotFoundHttpException ;
2010-02-17 13:53:18 +00:00
/*
2010-04-07 01:51:29 +01:00
* This file is part of the Symfony package .
2010-02-17 13:53:18 +00:00
*
* ( c ) Fabien Potencier < fabien . potencier @ symfony - project . com >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
/**
2010-05-06 11:04:50 +01:00
* HttpKernel notifies events to convert a Request object to a Response one .
2010-02-17 13:53:18 +00:00
*
2010-04-07 01:51:29 +01:00
* @ package Symfony
2010-05-06 11:04:50 +01:00
* @ subpackage Components_HttpKernel
2010-02-17 13:53:18 +00:00
* @ author Fabien Potencier < fabien . potencier @ symfony - project . com >
*/
2010-05-06 11:04:50 +01:00
class HttpKernel implements HttpKernelInterface
2010-02-17 13:53:18 +00:00
{
2010-05-06 12:25:53 +01:00
protected $dispatcher ;
protected $request ;
/**
* Constructor
*
* @ param EventDispatcher $dispatcher An event dispatcher instance
*/
public function __construct ( EventDispatcher $dispatcher )
2010-04-25 12:18:42 +01:00
{
2010-05-06 12:25:53 +01:00
$this -> dispatcher = $dispatcher ;
2010-04-25 12:18:42 +01:00
}
2010-05-06 12:25:53 +01:00
/**
* Gets the Request instance associated with the main request .
*
* @ return Request A Request instance
*/
public function getRequest ()
2010-04-25 12:18:42 +01:00
{
2010-05-06 12:25:53 +01:00
return $this -> request ;
2010-04-25 12:18:42 +01:00
}
2010-05-06 12:25:53 +01:00
/**
* Handles a request to convert it to a response .
*
* All exceptions are caught , and a core . exception event is notified
* for user management .
*
* @ param Request $request A Request instance
* @ param Boolean $main Whether this is the main request or not
2010-05-06 13:26:48 +01:00
* @ param Boolean $raw Whether to catch exceptions or not
2010-05-06 12:25:53 +01:00
*
* @ return Response $response A Response instance
*
* @ throws \Exception When Exception couldn ' t be caught by event processing
*/
2010-05-06 13:26:48 +01:00
public function handle ( Request $request = null , $main = true , $raw = false )
2010-02-17 13:53:18 +00:00
{
2010-05-06 12:25:53 +01:00
$main = ( Boolean ) $main ;
if ( null === $request )
{
$request = new Request ();
}
if ( true === $main )
{
$this -> request = $request ;
}
try
{
return $this -> handleRaw ( $request , $main );
}
catch ( \Exception $e )
{
2010-05-06 13:26:48 +01:00
if ( true === $raw )
{
throw $e ;
}
2010-05-06 12:25:53 +01:00
// exception
$event = $this -> dispatcher -> notifyUntil ( new Event ( $this , 'core.exception' , array ( 'main_request' => $main , 'request' => $request , 'exception' => $e )));
if ( $event -> isProcessed ())
{
return $this -> filterResponse ( $event -> getReturnValue (), $request , 'A "core.exception" listener returned a non response object.' , $main );
}
throw $e ;
}
2010-02-17 13:53:18 +00:00
}
2010-05-06 12:25:53 +01:00
/**
* Handles a request to convert it to a response .
*
* Exceptions are not caught .
*
* @ param Request $request A Request instance
* @ param Boolean $main Whether this is the main request or not
*
* @ return Response $response A Response instance
*
* @ throws \LogicException If one of the listener does not behave as expected
* @ throws NotFoundHttpException When controller cannot be found
*/
2010-05-06 13:26:48 +01:00
protected function handleRaw ( Request $request , $main = true )
2010-02-17 13:53:18 +00:00
{
2010-05-06 12:25:53 +01:00
$main = ( Boolean ) $main ;
// request
$event = $this -> dispatcher -> notifyUntil ( new Event ( $this , 'core.request' , array ( 'main_request' => $main , 'request' => $request )));
if ( $event -> isProcessed ())
{
return $this -> filterResponse ( $event -> getReturnValue (), $request , 'A "core.request" listener returned a non response object.' , $main );
}
// load controller
$event = $this -> dispatcher -> notifyUntil ( new Event ( $this , 'core.load_controller' , array ( 'main_request' => $main , 'request' => $request )));
if ( ! $event -> isProcessed ())
{
throw new NotFoundHttpException ( 'Unable to find the controller.' );
}
list ( $controller , $arguments ) = $event -> getReturnValue ();
// controller must be a callable
if ( ! is_callable ( $controller ))
{
throw new \LogicException ( sprintf ( 'The controller must be a callable (%s).' , var_export ( $controller , true )));
}
// controller
$event = $this -> dispatcher -> notifyUntil ( new Event ( $this , 'core.controller' , array ( 'main_request' => $main , 'request' => $request , 'controller' => & $controller , 'arguments' => & $arguments )));
if ( $event -> isProcessed ())
{
try
{
return $this -> filterResponse ( $event -> getReturnValue (), $request , 'A "core.controller" listener returned a non response object.' , $main );
}
catch ( \Exception $e )
{
$retval = $event -> getReturnValue ();
}
}
else
{
// call controller
$retval = call_user_func_array ( $controller , $arguments );
}
// view
$event = $this -> dispatcher -> filter ( new Event ( $this , 'core.view' , array ( 'main_request' => $main , 'request' => $request )), $retval );
return $this -> filterResponse ( $event -> getReturnValue (), $request , sprintf ( 'The controller must return a response (instead of %s).' , is_object ( $event -> getReturnValue ()) ? 'an object of class ' . get_class ( $event -> getReturnValue ()) : str_replace ( " \n " , '' , var_export ( $event -> getReturnValue (), true ))), $main );
2010-02-17 13:53:18 +00:00
}
2010-05-06 12:25:53 +01:00
/**
* Filters a response object .
*
* @ param Object $response A Response instance
* @ param string $message A error message in case the response is not a Response object
*
* @ param Object $response The filtered Response instance
*
* @ throws \RuntimeException if the response object does not implement the send () method
*/
protected function filterResponse ( $response , $request , $message , $main )
2010-02-17 13:53:18 +00:00
{
2010-05-06 12:25:53 +01:00
if ( ! $response instanceof Response )
{
throw new \RuntimeException ( $message );
}
2010-02-17 13:53:18 +00:00
2010-05-06 12:25:53 +01:00
$event = $this -> dispatcher -> filter ( new Event ( $this , 'core.response' , array ( 'main_request' => $main , 'request' => $request )), $response );
$response = $event -> getReturnValue ();
2010-02-17 13:53:18 +00:00
2010-05-06 12:25:53 +01:00
if ( ! $response instanceof Response )
{
throw new \RuntimeException ( 'A "core.response" listener returned a non response object.' );
}
2010-02-17 13:53:18 +00:00
2010-05-06 12:25:53 +01:00
return $response ;
2010-02-17 13:53:18 +00:00
}
}