简体   繁体   English

Symfony2内核与HttpKernel

[英]Symfony2 Kernel vs HttpKernel

I'm trying to follow this article: 我正在尝试关注这篇文章:

http://fabien.potencier.org/article/62/create-your-own-framework-on-top-of-the-symfony2-components-part-12 http://fabien.potencier.org/article/62/create-your-own-framework-on-top-of-the-symfony2-components-part-12

Also looking at HttpKernel https://github.com/symfony/HttpKernel 另外看看HttpKernel https://github.com/symfony/HttpKernel

And I'm quite confused. 我很困惑。 It seems to me that the Kernel is really something much more than the HttpKernel class here, and even the standard Symfony app.php has 在我看来,内核实际上不仅仅是HttpKernel类,甚至标准的Symfony app.php也有

$kernel = new AppKernel('prod', false);
$kernel->loadClassCache();

//$kernel = new AppCache($kernel);
$request = Request::createFromGlobals();
$response = $kernel->handle($request);

The Kernel, in turns, will call the HttpKernel->handle() within $kernel->handle($request) anyhow; 反过来, $kernel->handle($request)无论如何都会调用$kernel->handle($request)HttpKernel->handle() ; plus it also seems to be taking care of loading bundles as well? 加上它似乎也照顾加载捆绑?

However, when the kernel creates the service container through boot() within handle() it also compile the container making it impossible to add more parameters and services. 但是,当内核通过handle() boot()创建服务容器时,它还会编译容器,从而无法添加更多参数和服务。

So I guess my questions are: 所以我想我的问题是:

  1. Is there any specific reason that in the tutorial the Framework class extends HttpKernel instead of Kernel? 是否有任何具体原因在教程中Framework类扩展了HttpKernel而不是Kernel?
  2. Should I also follow suit? 我也应该效仿吗? Or should I use Kernel as my core. 或者我应该使用内核作为我的核心。 And if so, how do I get around the compile() issue? 如果是这样,我如何解决compile()问题? I do have parameters and services I have to add, how do I handle? 我有必须添加的参数和服务,我该如何处理?

The Kernel is the outermost shell of your application. Kernel是应用程序的最外层shell。 It is used in Symfony full-stack to configure the debug mode and tie together bundles, classmap autoloading, container creation. 它在Symfony全栈中用于配置调试模式并将bundle,classmap自动加载,容器创建联系在一起。 Anything that is "global" to the application is configured in here. 这里配置了对应用程序“全局”的任何内容。 It also wraps an HttpKernel instance for convenience, and delegates all calls to it. 为方便起见,它还包装了一个HttpKernel实例,并将所有调用委托给它。

The HttpKernel manages the request/response lifecycle. HttpKernel管理请求/响应生命周期。 It is a standalone class that dispatches events on an event dispatcher. 它是一个独立的类,用于在事件调度程序上调度事件。 You modify it's behaviour by adding listeners which respond to those events. 您可以通过添加响应这些事件的侦听器来修改它的行为。 Thus, it is not at all application specific. 因此,它根本不是特定于应用的。 The configuration is what a particular instance of the HttpKernel specific to your application. 配置是特定于您的应用程序的HttpKernel特定实例。

Let's talk about container compilation. 我们来谈谈容器编译。 The container is compiled. 容器已编译。 This compilation process applies some optimizations to it, and also adds some functionality (modifying the container based on tags). 此编译过程对其应用了一些优化,并且还添加了一些功能(基于标记修改容器)。 Once it is compiled, it cannot be modified (modifications would break those optimizations). 一旦编译,就无法修改(修改会破坏这些优化)。 When you have a compiled container, you can dump it to disk. 如果有已编译的容器,则可以将其转储到磁盘。 By using the PhpDumper you can dump it to a generated PHP class which is a lot more performant than building it up every time. 通过使用PhpDumper您可以将其转储到生成的PHP类,这比每次构建它更PhpDumper

Is there any specific reason that in the tutorial the Framework class extends HttpKernel instead of Kernel? 是否有任何具体原因在教程中Framework类扩展了HttpKernel而不是Kernel?

Yes. 是。 The tutorial is on how to build your own framework with Symfony2 components. 本教程介绍如何使用Symfony2组件构建自己的框架。 Kernel is the outer shell of the Symfony2 full stack framework. Kernel是Symfony2完整堆栈框架的外壳。 If you use it, you're not using your own framework. 如果您使用它,则不使用自己的框架。 You're using Symfony2 full stack. 你正在使用Symfony2完整堆栈。

Should I also follow suit? 我也应该效仿吗?

It depends. 这取决于。 If you want the things Kernel provides, you might as well just use Symfony full stack. 如果你想要Kernel提供的东西,你也可以只使用Symfony完整堆栈。 But then you're not creating your own framework. 但是你没有创建自己的框架。

And if so, how do I get around the compile() issue? 如果是这样,我如何解决compile()问题? I do have parameters and services I have to add, how do I handle? 我有必须添加的参数和服务,我该如何处理?

If you want a compiled container, you'll need to add those parameters and services as extensions (bundles allow you to tie in extensions), and those extensions register services and parameters. 如果你想要一个编译容器,你需要添加这些参数和服务作为扩展(bundle允许你绑定扩展),这些扩展注册服务和参数。 This happens before the compilation step, so that the definitions can then be compiled and dumped to a file. 这在编译步骤之前发生,因此可以编译定义并将其转储到文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM