文章地址:http://www.hcoding.com/?p=46
上一篇分析Symfony2框架源码,探究Symfony2如何完成一个请求的前半部分,前半部分可以理解为Symfony2框架为处理请求做准备工作,包括container生成、缓存、bundls初始化等一些列准备工作(Symfony2源码分析——启动过程1)。而这一篇讲的是Symfony2如何根据请求的数据生成Response对象,向客户端返回响应数据。
在分析前需要了解Symfony2的事件驱动机制:Symfony2事件驱动。
言归正传,Symfony2请求的工作流程其实是Symfony2内核的事件驱动完成的,下面是Symfony2框架定义好的内核事件:
final class KernelEvents
{
/**
* The REQUEST event occurs at the very beginning of request
* dispatching
*
* This event allows you to create a response for a request before any
* other code in the framework is executed. The event listener method
* receives a Symfony\Component\HttpKernel\Event\GetResponseEvent
* instance.
*
* @var string
*
* @api
*/
const REQUEST = 'kernel.request'; /**
* The EXCEPTION event occurs when an uncaught exception appears
*
* This event allows you to create a response for a thrown exception or
* to modify the thrown exception. The event listener method receives
* a Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent
* instance.
*
* @var string
*
* @api
*/
const EXCEPTION = 'kernel.exception'; /**
* The VIEW event occurs when the return value of a controller
* is not a Response instance
*
* This event allows you to create a response for the return value of the
* controller. The event listener method receives a
* Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent
* instance.
*
* @var string
*
* @api
*/
const VIEW = 'kernel.view'; /**
* The CONTROLLER event occurs once a controller was found for
* handling a request
*
* This event allows you to change the controller that will handle the
* request. The event listener method receives a
* Symfony\Component\HttpKernel\Event\FilterControllerEvent instance.
*
* @var string
*
* @api
*/
const CONTROLLER = 'kernel.controller'; /**
* The RESPONSE event occurs once a response was created for
* replying to a request
*
* This event allows you to modify or replace the response that will be
* replied. The event listener method receives a
* Symfony\Component\HttpKernel\Event\FilterResponseEvent instance.
*
* @var string
*
* @api
*/
const RESPONSE = 'kernel.response'; /**
* The TERMINATE event occurs once a response was sent
*
* This event allows you to run expensive post-response jobs.
* The event listener method receives a
* Symfony\Component\HttpKernel\Event\PostResponseEvent instance.
*
* @var string
*/
const TERMINATE = 'kernel.terminate'; /**
* The FINISH_REQUEST event occurs when a response was generated for a request.
*
* This event allows you to reset the global and environmental state of
* the application, when it was changed during the request.
*
* @var string
*/
const FINISH_REQUEST = 'kernel.finish_request';
}
我们可以编写事件监听器,监听相应的内核事件,在Symfony2触发该事件的时候,相应的事件监听器就会执行。监听和唤醒形象的描述,就像,你(事件监听器)参加校运会,去大会(Symfony2)登记(监听)参加50米短跑(事件),当50米短跑比赛开始了(事件被触发),那你就奔跑吧(监听器执行,其实就是一个执行函数,函数完成什么工作就取决于你的需求了),少年。
Symfony2的内核事件处理流程大部分工作都在HttpKernel::handleRaw方法中:
private function handleRaw(Request $request, $type = self::MASTER_REQUEST)
{
$this->requestStack->push($request); // request
// 初始化事件,事件对象会被传递给监听器,所以事件可以说是一个信息的载体,事件内存放着监听器感兴趣的数据。
$event = new GetResponseEvent($this, $request, $type);
// 触发kernel.request事件,后续详细讲解EventDispatcher::dispatch方法的实现,
// 这里我们需要知道的是,dispatcher把$event传递给所有监听了kernel.request事件的监听器,监听器将会执行。
// kernel.request事件发生在controller执行之前,我们可以在这一步奏完成路由解析等为controller执行提供准备数据,
// 在这个过程允许我们直接生成Response对象,向客户端输出数据,那么controller就不会被执行了。
$this->dispatcher->dispatch(KernelEvents::REQUEST, $event); // 如果我们在kernel.request事件生成了Response对象(响应数据),那么就跳过kernel.controller、kernel.view事件、
// controller也会被跳过,直接执行kernel.response事件。
if ($event->hasResponse()) {
return $this->filterResponse($event->getResponse(), $request, $type);
} // load controller
// 根据路由规则返回 一个对象或者数组或者字符串 ,如果$controller是一个数组,$controller[0]是存放的是要执行的controller对象,
// $controller[0]存放的是controller对象执行的方法,即action,方法的参数没有保存在$controller数组中;
// 如果$controller是对象,那么该对象就实现了__invoke 方法;
// 如果$controller是字符串,那么$controller就是要运行的函数的函数名。
// 图2是$controller的一个var_dump例子
if (false === $controller = $this->resolver->getController($request)) {
throw new NotFoundHttpException(sprintf('Unable to find the controller for path "%s". Maybe you forgot to add the matching route in your routing configuration?', $request->getPathInfo()));
} $event = new FilterControllerEvent($this, $controller, $request, $type);
// 触发kernel.controller事件,这个事件发生在controller执行前。我们可以通过监听这个事件在controller执行前修改controller,
// 或者完成一些动作。
$this->dispatcher->dispatch(KernelEvents::CONTROLLER, $event);
$controller = $event->getController(); // controller arguments
// 从request对象中获取controller方法的参数
$arguments = $this->resolver->getArguments($request, $controller); // call controller
// 执行controller
$response = call_user_func_array($controller, $arguments); // view
// 如果$response不是Response对象,那么kernel.view事件就会触发,监听kernel.view事件的监听器通过$response值生成Response对象。
if (!$response instanceof Response) {
$event = new GetResponseForControllerResultEvent($this, $request, $type, $response);
$this->dispatcher->dispatch(KernelEvents::VIEW, $event); if ($event->hasResponse()) {
$response = $event->getResponse();
} if (!$response instanceof Response) {
$msg = sprintf('The controller must return a response (%s given).', $this->varToString($response)); // the user may have forgotten to return something
if (null === $response) {
$msg .= ' Did you forget to add a return statement somewhere in your controller?';
}
throw new \LogicException($msg);
}
} // 触发kernel.response事件,在向客户端输出Response对象前,我们可以对Response对象进行修改,
// 例如修改response头部,设置缓存、压缩输出数据等。 // 接着触发kernel.finish_request事件,把当前请求从请求栈中弹出,当前请求就完成。
return $this->filterResponse($response, $request, $type); // 千万别忘记了,filterResponse执行完后,Symfony2内核事件处理流程还有最后一步,位于app_dev.php[app.php]最后一行,
// $kernel->terminate($request, $response);这个方法触发kernel.terminate事件,此时,Symfony2已经响应了客户端的请求,
// 向客户端输出了Response对象。监听kernel.terminate事件的监听器,主要是为了完成一些耗时的操作,操作的结果不需要返回给
// 客户端的,例如邮件发送、图片压缩等等。
// 到这里,Symfony2的整个流程就走完了。
}
HttpKernel::filterResponse方法和HttpKernel::finishRequest方法:
private function filterResponse(Response $response, Request $request, $type)
{
$event = new FilterResponseEvent($this, $request, $type, $response); $this->dispatcher->dispatch(KernelEvents::RESPONSE, $event); $this->finishRequest($request, $type); return $event->getResponse();
} /**
* Publishes the finish request event, then pop the request from the stack.
*
* Note that the order of the operations is important here, otherwise
* operations such as {@link RequestStack::getParentRequest()} can lead to
* weird results.
*
* @param Request $request
* @param int $type
*/
private function finishRequest(Request $request, $type)
{
$this->dispatcher->dispatch(KernelEvents::FINISH_REQUEST, new FinishRequestEvent($this, $request, $type));
$this->requestStack->pop();
}
图2
Symfony2框架的事件分发机制的核心代码:
public function dispatch($eventName, Event $event = null)
{
if (null === $event) {
$event = new Event();
} $event->setDispatcher($this);
$event->setName($eventName); if (!isset($this->listeners[$eventName])) {
return $event;
} // $eventName即:KernelEvents::REQUEST、KernelEvents::CONTROLLER、KernelEvents::VIEW、KernelEvents::RESPONSE、KernelEvents::TERMINATE等
// getListeners返回所有监听$eventName事件的监听器
$this->doDispatch($this->getListeners($eventName), $eventName, $event); return $event;
} protected function doDispatch($listeners, $eventName, Event $event)
{
// 监听器执行
foreach ($listeners as $listener) {
call_user_func($listener, $event, $eventName, $this);
// 如果其中一个监听器把$event的propagationStopped属性设置为true,那么表示$eventName这一事件终止执行,
// 事件不会往$listeners里尚未执行的监听器传递该事件。
if ($event->isPropagationStopped()) {
break;
}
}
}