The DrupalKernel currently has too many responsibilities, and has many qualities of a "god class".
Maybe the Kernel already is too big in Symfony, but it certainly is in Drupal.

There is a number of issues that deal with DrupalKernel refactoring, but they do not really name this problem, and they do not provide a big picture.

The responsibilities will change with issues like #2016629: Refactor bootstrap to better utilize the kernel and #2021959: Refactor module handling responsibilities out of DrupalKernel. It is also likely to grow further as we split up the procedural files.
Here is a list of responsibilities that DrupalKernel combined from different versions:

Bootstrap / shutdown:
- bootEnvironment() (#2016629)
- initializeRequestGlobals() (#2016629)
- initializeCookieGlobals() (#2016629)
- boot()
- shutdown(), terminate()

Site management:
- findSitePath()
- manage site directory
- initialization of site config

Site config management:
- storage() (#2016629)
- getConfigStorage() (#2016629)

Request handling:
- preHandle() (#2016629)
- handlePageCache() (#2016629)
- handle()
- prepareLegacyRequest() (#3453216: Clean up outdated mentions of prepareLegacyRequest)

Container management:
- service provider management
- service provider discovery
- container initialization
- container dumping
- service persistance

Module management:
- moduleData()
- module update
- module namespaces for the container
- module namespaces registration in the class loader
(While #2021959 proposes to moves most of this into the module handler, I think that could also be split up further)

This is too much for one class to handle. Seriously.
The term "Kernel" itself already suggests that it is going to have too many responsibilities..
(I could go on about how small a class should be for my taste, but I would prefer to leave that to IRC or other places)

So, what about the Symfony Kernel?
DrupalKernel does not have much in common with the symfony kernel anymore, so who cares.
But as a comparison, here are the responsibilities of abstract class \Symfony\Component\HttpKernel\Kernel:
- boot, terminate, shutdown
- request handling
- locateResource()
- bundle management
- container managment
- stripComments() (ouch, this one seems totally out of place)

This is already too much for my taste. But the implementation of these aspects look a lot simpler in Symfony. I assume it is ok, assuming that the average developer won't spend too much time debugging this class.

In Drupal, the Kernel is much bigger, it is intertwined with an ugly environment full of legacy global state, and there are a lot more unknowns and runtime dynamics.

Proposed resolution:

Slice it up, step by step, into components such as ContainerManager, ServiceProviderManager, ServiceProviderDiscovery, RequestHandler, SitePathFinder, etc.
There does not even need to be a DrupalKernel class at all, if all the tasks go into separate components.

To let these components play together, I would personally favor a solution like #2272129: [meta] Introduce a low-level DIC to organize low-level services, but I am open to other ideas, and I would like to keep these two issues separate.

Another solution would be to keep the DrupalKernel class, but only use it as a "hub" to get to dedicated separate components.

Comments

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev
joachim’s picture

+1

Additionally, some of DrupalKernel's functionality is intended to be called statically, BEFORE you've instantiated it -- site management methods in particular.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

bradjones1’s picture

The DrupalKernel class itself is also... problematic. It is extended (e.g., InstallerKernel, UpdateKernel, and more) however the "base" DrupalKernel is a super-duper privileged class that is instantiated by name in two key places - index.php and drupal_rebuild(). In a perfect world, the base class would be abstract and the current DrupalKernel would be renamed something like DrupalAppKernel.

But I digress - the current arrangement makes it difficult to do wild things like customizing your own kernel (e.g., to do some forward-compatibility stuff in Symfony) because even if you update index.php to use your kernel class, it's still calling the privileged DrupalKernel by name in the rebuild function. (Ideally there should be only one way to resolve the kernel class.)

I understand this is rather esoteric and only affects the rare customer who is doing crazy custom stuff like this... but it also is a legitimate code smell.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

andypost’s picture

Issue summary: View changes

Updated IS as few methods gone already

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.