Symfony2源码分析——启动过程1

    本文通过阅读分析Symfony2的源码,了解Symfony2启动过程中完成哪些工作,从阅读源码了解Symfony2框架。

    Symfony2的核心本质是把Request转换成Response的一个过程。

    我们大概看看入口文件(web_dev.php)的源码,入口文件从总体上描述了Symfony2框架的工作的流程:

 require_once __DIR__.'/../app/AppKernel.php';

 $kernel = new AppKernel('dev', true);
$kernel->loadClassCache();
//利用请求信息($_GET $_POST $_SERVER等等)构造Request对象
$request = Request::createFromGlobals();
//Symfony2框架核心工作就是把Request对象转换成Response对象
$response = $kernel->handle($request);
//向客户端输出Response对象
$response->send();
//完成一些耗时的后台操作,例如邮件发送,图片裁剪等等耗时工作
$kernel->terminate($request, $response);

    Symfony2框架通过客户端的请求信息来决定生成并返回响应的数据,我们下面的Symfony2源码分析重点就是AppKernel::handle方法。

    AppKernel::handle的实现继承于Kernel::handle

     /**
*
* @param Request $request Request对象实例
* @param int $type 请求的类型(子请求 or 主请求)
* @param bool $catch 是否捕捉异常
*
* @return Response Response对象实例
*
*/
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
{
//$this->booted Symfony2框架只启动一次
if (false === $this->booted) {
//初始化并启动所有注册在AppKernel里面的所有bundles(AppKernel::registerBundles)
//初始化container
//加载、缓存配置数据和路由数据、编译container容器等,为后面事件处理做准备。
$this->boot();
} //开启事件处理,Symfony2内核的请求处理过程本质是一系列的事件处理过程
return $this->getHttpKernel()->handle($request, $type, $catch);
}

    AppKernel::boot方法

     public function boot()
{
if (true === $this->booted) {
return;
} if ($this->loadClassCache) {
$this->doLoadClassCache($this->loadClassCache[], $this->loadClassCache[]);
} // init bundles
//初始化注册到AppKernel里的所有bundle(AppKernel::registerBundles)
$this->initializeBundles(); // init container
//初始化并编译缓存container,包括载入配置信息、编译信息、service等
//Symfony2的核心组件的加载,和各个组件之间的关联关系都在container容器初始化中完成,所以这会是下面详细描述
$this->initializeContainer(); //把bundle注入到container,并启动bundle
foreach ($this->getBundles() as $bundle) {
$bundle->setContainer($this->container);
$bundle->boot();
} //标记Symfony2只启动一次并启动成功
$this->booted = true;
}

    AppKernel::initializeContainer源码解析

     protected function initializeContainer()
{
//检查app/cache/dev[prod]缓存文件是否过期,以container缓存文件的最后修改时间为参考时间,
//如果app/cache/dev[prod]下的存在一个或者多个缓存文件的最后修改时间大于container缓存文件的
//最后修改时间,就判断为缓存过期。
//另外,如果$this->debug为false(即关闭debug的情况下)只要container缓存文件存在,那么就认为
//缓存不过期
$class = $this->getContainerClass();
$cache = new ConfigCache($this->getCacheDir().'/'.$class.'.php', $this->debug);
$fresh = true;
if (!$cache->isFresh()) {
//初始化一个ContainerBuilder对象实例;
//自动加载所有注册的Bundle的DependencyInjection下的所有extension,Bundle可以通过extension来加载属于该Bundle配置(service的配置、
//路由的配置等等)、Bundle的全局变量等
//同时这些extension加载的信息都会被保存到container中;
//加载并保存compiler pass到container,为下一步compile做准备,我们可以通过compiler pass修改已经注册到container的service的属性
//compiler pass的官方文档http://symfony.com/doc/current/cookbook/service_container/compiler_passes.html
$container = $this->buildContainer();
//执行compiler pass 的process方法,container的compile过程主要是执行上一步保存到container内的compiler pass 的process方法
$container->compile();
//生成container的缓存(appDevDebugProjectContainer.php),该container包含了service的获取方法、别名的映射关系
$this->dumpContainer($cache, $container, $class, $this->getContainerBaseClass()); $fresh = false;
} require_once $cache; $this->container = new $class();
$this->container->set('kernel', $this); //...............
if (!$fresh && $this->container->has('cache_warmer')) {
$this->container->get('cache_warmer')->warmUp($this->container->getParameter('kernel.cache_dir'));
}
}
     protected function prepareContainer(ContainerBuilder $container)
{
$extensions = array();
foreach ($this->bundles as $bundle) {
//加载DependencyInjection下的Extension,所有Extension必需实现Extension接口
if ($extension = $bundle->getContainerExtension()) {
$container->registerExtension($extension);
$extensions[] = $extension->getAlias();
} //开启debug的情况下,把bundles添加到recourses
if ($this->debug) {
$container->addObjectResource($bundle);
}
}
foreach ($this->bundles as $bundle) {
//通常用来添加compiler pass
$bundle->build($container);
} // ensure these extensions are implicitly loaded
$container->getCompilerPassConfig()->setMergePass(new MergeExtensionConfigurationPass($extensions));
}

    从AppKernel::initializeContainer可以看出Bundle和container是Symfony2框架的基础核心,container是Symfony2框架的所有组件的统一管理中心,Bundle就是一个功能模块的组织。

    如果你好奇service、配置参数是怎样被加载的,可以详细去了解Symfony2的Extension;如果你好奇怎么对已经加载了的service进一步完善和修改,可有详细了解Symfony2的compiler pass。

    到了这一步,Symfony2框架启动几乎完成,为后面的内核事件处理EventDispatcher::dispatch做好了准备。

    下一篇讲解Symfony2框架的内核事件处理。

上一篇:storm操作zookeeper源码分析-cluster.clj


下一篇:quartz2.x源码分析——启动过程