Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Also looking at 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

$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; 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.

So I guess my questions are:

  1. Is there any specific reason that in the tutorial the Framework class extends HttpKernel instead of 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? I do have parameters and services I have to add, how do I handle?
like image 473
mr1031011 Avatar asked Oct 19 '12 15:10

mr1031011


1 Answers

The Kernel is the outermost shell of your application. It is used in Symfony full-stack to configure the debug mode and tie together bundles, classmap autoloading, container creation. 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.

The HttpKernel manages the request/response lifecycle. 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.

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.

Is there any specific reason that in the tutorial the Framework class extends HttpKernel instead of Kernel?

Yes. The tutorial is on how to build your own framework with Symfony2 components. Kernel is the outer shell of the Symfony2 full stack framework. If you use it, you're not using your own framework. You're using Symfony2 full stack.

Should I also follow suit?

It depends. If you want the things Kernel provides, you might as well just use Symfony full stack. But then you're not creating your own framework.

And if so, how do I get around the compile() issue? 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. This happens before the compilation step, so that the definitions can then be compiled and dumped to a file.

like image 146
igorw Avatar answered Sep 22 '22 12:09

igorw