Problem/Motivation

Since symfony/http-kernel 5.3: "Symfony\Component\HttpKernel\Event\KernelEvent::isMasterRequest()" is deprecated, use "isMainRequest()" instead.

Steps to reproduce

Update to Symfony 5.3 as in #3161889: [META] Symfony 6 compatibility

Proposed resolution

Replace the deprecated method with the replacement method.

Remaining tasks

None

User interface changes

None

API changes

The method Symfony\Component\HttpKernel\Event\KernelEvent::isMasterRequest() is deprecated in Symfony 5.3. The method has been replaced by Symfony\Component\HttpKernel\Event\KernelEvent::isMainRequest().

Data model changes

None

Release notes snippet

TBD

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

longwave created an issue. See original summary.

longwave’s picture

Title: [Symfony 6] Symfony\Component\HttpKernel\Event\KernelEvent::isMasterRequest() is deprecated, use isMainRequest() instead. » [Symfony 6] Symfony\Component\HttpKernel\Event\KernelEvent::isMasterRequest() is deprecated, use isMainRequest() instead
Parent issue: » #3161889: [META] Symfony 6 compatibility
catch’s picture

Should we combine this with #3209617: [Symfony 6] Symfony\Component\HttpFoundation\RequestStack::getMasterRequest() is deprecated, use getMainRequest() instead? Seems like it'll depend on the subclass used in the other issue.

longwave’s picture

@catch I thought about that but I think the two classes are fundamentally different, we can do the changes independently, and I can see how to make an FC shim for RequestStack but this one looks far more difficult:

As an example:

vendor/symfony/http-kernel/Event/TerminateEvent.php
26:final class TerminateEvent extends KernelEvent

vendor/symfony/http-kernel/HttpKernel.php
99:        $this->dispatcher->dispatch(new TerminateEvent($this, $request, $response), KernelEvents::TERMINATE);

We might have to override all the places the event classes are instantiated? But because the classes are final, we can't extend them and if we just replace them then existing type checks won't work:

core/lib/Drupal/Core/EventSubscriber/KernelDestructionSubscriber.php
8:use Symfony\Component\HttpKernel\Event\TerminateEvent;
45:  public function onKernelTerminate(TerminateEvent $event) {

So this one looks somewhat tricky.

catch’s picture

But because the classes are final, we can't extend them and if we just replace them then existing type checks won't work:

I can hear class_alias() calling from across the hills. It means we'll have to copy HttpKernel to our own version, but it should handle the replacement angle.

catch’s picture

Status: Active » Needs review
FileSize
3.22 KB

This sort of thing, completely untested patch.

andypost’s picture

Status: Needs review » Needs work
--- Commands Executed ---
core/scripts/dev/commit-code-check.sh --drupalci
Return Code: 1
--- Output ---
/var/www/html/core/lib/Drupal/Core/Http/KernelEvent.php:18:21 - Unknown word (Schussek)

CSpell: failed
catch’s picture

Status: Needs work » Needs review
FileSize
3.24 KB
catch’s picture

Now with some basic test coverage for the forward compatibility layer.

Status: Needs review » Needs work

The last submitted patch, 9: 3209618-9.patch, failed testing. View results

catch’s picture

Status: Needs work » Needs review
FileSize
8.71 KB
13.04 KB

Test failures are the new deprecation kicking in, this ought to fix hopefully.

longwave’s picture

Nice work!

  1. +++ b/core/lib/Drupal/Core/DrupalKernel.php
    @@ -39,6 +40,8 @@
    +class_alias(KernelEvent::class, 'Symfony\Component\HttpKernel\Event\KernelEvent', TRUE);
    

    Let's add an @todo to remove this when we use Symfony 6.

  2. +++ b/core/lib/Drupal/Core/Http/KernelEvent.php
    @@ -0,0 +1,89 @@
    +/*
    + * Symfony 6 bridge.
    + */
    

    I think this should be merged into the following docblock and say we copy the code directly from Symfony, again with the @todo to remove.

  3. +++ b/core/lib/Drupal/Core/Http/KernelEvent.php
    @@ -0,0 +1,89 @@
    +        @trigger_error('Symfony\Component\HttpKernel\Event\KernelEvent::isMasterRequest() is deprecated, use isMainRequest()', E_USER_DEPRECATED);
    

    Do we need a deprecation test for this?

  4. +++ b/core/tests/Drupal/KernelTests/Core/DrupalKernel/DrupalKernelTest.php
    @@ -58,6 +59,16 @@ protected function getTestKernel(Request $request, array $modules_enabled = NULL
    +   * Tests KernelEvent class_alias() override.
    

    And finally let's add the same @todo here.

catch’s picture

1. Made this 5.3 since the new method will be available then.
2. Yep. Tried to tidy this up a bit.
3. Yes probably although I'm failing to to get the test to pass locally, trying a 'hit and hope' with DrupalCI.
4. As with #2.

catch’s picture

Fixing the cs issues.

Status: Needs review » Needs work

The last submitted patch, 14: 3209618-14.patch, failed testing. View results

andypost’s picture

longwave’s picture

catch’s picture

Feel like I've seen the deprecation testing blow up like this before, but not seeing what the problem is with the test.

longwave’s picture

Status: Needs work » Needs review
FileSize
14.48 KB
3.41 KB

I can reproduce locally but also not sure why the deprecation isn't firing.

However we can convert the kernel test to a unit test, which seems to work.

catch’s picture

I double checked that that class_alias was working in the legacy test (just in case we were somehow loading Symfony's KernelEvent), but it wasn't that, so something to do with the deprecation listener itself I think - but didn't get far enough. Unit test is better anyway though and seems happy.

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

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

daffie’s picture

Status: Needs review » Needs work

The patch looks good. Just 2 nitpicks.

  1. +++ b/core/lib/Drupal/Core/Http/KernelEvent.php
    @@ -0,0 +1,90 @@
    +// @codingStandardsIgnoreFile
    +// cspell:disable
    +
    +
    +namespace Drupal\Core\Http;
    

    Nitpick: A single empty line is enough.

  2. +++ b/core/lib/Drupal/Core/Http/KernelEvent.php
    @@ -0,0 +1,90 @@
    +/*
    

    Nitpick: The slash is not needed here.

catch’s picture

Status: Needs work » Needs review
Issue tags: +Drupal 10, +Symfony 6
FileSize
557 bytes
14.47 KB

Addressing #22.

daffie’s picture

Issue summary: View changes
Status: Needs review » Reviewed & tested by the community

The added class is extending the Symfony class that in version 5.3 will throw a deprecation message.
The added class is being added by a class_alias call in the class DrupalKernel.
There is testing for class_alias call and the deprecation message.
All calls to the deprecated method have been replaced.
Added a CR and updated the IS.
For me it is RTBC.

daffie’s picture

Priority: Normal » Critical

All Symfony 6 related issues are critical.

Gábor Hojtsy’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/lib/Drupal/Core/DrupalKernel.php
@@ -39,6 +40,9 @@
+// @todo: remove this class alias once Drupal is running Symfony 5.3 or higher.
+class_alias(KernelEvent::class, 'Symfony\Component\HttpKernel\Event\KernelEvent', TRUE);

+++ b/core/tests/Drupal/KernelTests/Core/DrupalKernel/DrupalKernelTest.php
@@ -58,6 +59,18 @@ protected function getTestKernel(Request $request, array $modules_enabled = NULL
+   * @todo: remove this test once Drupal is using Symfony 5.3 or higher.

As per https://www.drupal.org/docs/develop/standards/api-documentation-and-comm...

Usually, a corresponding Drupal.org issue should be created for the @todo and referenced in it.

These should IMHO point to #3197482: Update Drupal 10 to depend on Symfony 5.4 (as a stepping stone to Symfony 6, for deprecation checking support). That would make it easier to find the corresponding issue and to find the todos referencing it as well.

Neslee Canil Pinto’s picture

Status: Needs work » Needs review
FileSize
14.6 KB
1.06 KB

Added @todo reference issue link

daffie’s picture

Status: Needs review » Reviewed & tested by the community

The point of @gabor has been addressed.
Back to RTBC.

alexpott’s picture

Version: 9.3.x-dev » 9.2.x-dev
Status: Reviewed & tested by the community » Fixed
+++ b/core/lib/Drupal/Core/DrupalKernel.php
@@ -39,6 +40,8 @@
+class_alias(KernelEvent::class, 'Symfony\Component\HttpKernel\Event\KernelEvent', TRUE);

I've felt for a while we should have a core/class_aliases.php which should be included by composer as this would mean that our aliases are decoupled from including this class. This has a big benefit of meaning that in unit tests we'll use our class even when DrupalKernel has not be included. I think we should open a follow-up to move this to such a file.

Committed and pushed 85ee5e164b to 9.3.x and b1fd5e7cc9 to 9.2.x. Thanks!

diff --git a/core/lib/Drupal/Core/DrupalKernel.php b/core/lib/Drupal/Core/DrupalKernel.php
index 6b9807e18b..d9af456bbd 100644
--- a/core/lib/Drupal/Core/DrupalKernel.php
+++ b/core/lib/Drupal/Core/DrupalKernel.php
@@ -40,8 +40,8 @@
 use TYPO3\PharStreamWrapper\Behavior as PharStreamWrapperBehavior;
 use TYPO3\PharStreamWrapper\PharStreamWrapper;
 
-// @todo: remove this class alias once Drupal is running Symfony 5.3 or higher.
-// @see https://www.drupal.org/project/drupal/issues/3197482
+// @todo https://www.drupal.org/project/drupal/issues/3197482 Remove this class
+//   alias once Drupal is running Symfony 5.3 or higher.
 class_alias(KernelEvent::class, 'Symfony\Component\HttpKernel\Event\KernelEvent', TRUE);
 
 /**
diff --git a/core/tests/Drupal/KernelTests/Core/DrupalKernel/DrupalKernelTest.php b/core/tests/Drupal/KernelTests/Core/DrupalKernel/DrupalKernelTest.php
index b139933ea2..056e0e82e3 100644
--- a/core/tests/Drupal/KernelTests/Core/DrupalKernel/DrupalKernelTest.php
+++ b/core/tests/Drupal/KernelTests/Core/DrupalKernel/DrupalKernelTest.php
@@ -62,8 +62,8 @@ protected function getTestKernel(Request $request, array $modules_enabled = NULL
   /**
    * Tests KernelEvent class_alias() override.
    *
-   * @todo: remove this test once Drupal is using Symfony 5.3 or higher.
-   * @see https://www.drupal.org/project/drupal/issues/3197482
+   * @todo https://www.drupal.org/project/drupal/issues/3197482 Remove this test
+   *   once Drupal is using Symfony 5.3 or higher.
    */
   public function testKernelEvent() {
     $request = Request::createFromGlobals();

removed the @see's on commit - the link can go in the @todo just fine.

  • alexpott committed 85ee5e1 on 9.3.x
    Issue #3209618 by catch, longwave, Neslee Canil Pinto, daffie, andypost...

  • alexpott committed b1fd5e7 on 9.2.x
    Issue #3209618 by catch, longwave, Neslee Canil Pinto, daffie, andypost...
alexpott’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.