Problem/Motivation

  /**
   * Prepare the kernel for handling a request without handling the request.
[..]
   * @deprecated in Drupal 8.0.x and will be removed before 9.0.0. Only used by
   *   legacy front-controller scripts.
  */

The one line summary is not useful information, and the deprecation message is not true (which is kind of hilarious: #2556273-4: Fix @deprecation docs of Drupal\Core\DrupalKernelInterface::prepareLegacyRequest())

prepareLegacyRequest() is used extensively by the testing system.

It's also used by drupal_install_system() and drupal_rebuild().

Found 13 matches of prepareLegacyRequest in 11 files.	
authorize.php	
  $kernel->prepareLegacyRequest($request);      [position 68:12]	
install.core.inc	
  // \Drupal\Core\DrupalKernel::prepareLegacyRequest() since normal routing      [position 474:33]	
install.inc	
  $kernel->prepareLegacyRequest($request);      [position 619:12]	
utility.inc	
  $kernel->prepareLegacyRequest($request);      [position 43:12]	
DrupalKernel.php	
  public function prepareLegacyRequest(Request $request) {      [position 707:19]	
DrupalKernelInterface.php	
  public function prepareLegacyRequest(Request $request);      [position 128:19]	
FunctionalTestSetupTrait.php	
    $this->kernel->prepareLegacyRequest($request);      [position 389:20]	
    $this->kernel->prepareLegacyRequest(\Drupal::request());      [position 442:20]	
    //   DrupalKernel::prepareLegacyRequest() -> DrupalKernel::boot() but that      [position 446:24]	
InstallerTestBase.php	
      $this->kernel->prepareLegacyRequest($request);      [position 151:22]	
run-tests.sh	
  $kernel->prepareLegacyRequest($request);      [position 58:12]	
KernelTestBase.php	
    $kernel->prepareLegacyRequest($request);      [position 369:14]	
BrowserTestBase.php	
    $kernel->prepareLegacyRequest($request);      [position 1019:14]	

Proposed resolution

Remove the usages and replace them with calls to DrupalKernel::boot() and DrupalKernel::preHandle().

Remaining tasks

User interface changes

API changes

Data model changes

Comments

Mile23 created an issue. See original summary.

mile23’s picture

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

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now 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.

mile23’s picture

Component: other » base system
Status: Active » Needs review
StatusFileSize
new699 bytes

Curious what this tells us.

Status: Needs review » Needs work

The last submitted patch, 4: 2869573_4_test_only.patch, failed testing. View results

mile23’s picture

Status: Needs work » Needs review
StatusFileSize
new8.22 KB

Some work on this. WIP.

Status: Needs review » Needs work

The last submitted patch, 6: 2869573_6.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

mile23’s picture

Status: Needs work » Needs review
StatusFileSize
new10.74 KB

Getting closer. I'm not clear on why half of these occurrences are there instead of boot() and preHandle(). This whole system needs clean up.

Also not clear on why preHandle() isn't part of boot() or handle(), but that's out of scope here.

Status: Needs review » Needs work

The last submitted patch, 8: 2869573_8.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

mile23’s picture

Your moment of zen:

There was 1 failure:

1) Drupal\KernelTests\Core\Theme\ThemeRenderAndAutoescapeTest::testBubblingMetadataWithRenderable
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-''
+''

(Answer: The strings have HTML tags in them which were stripped for display.)

mile23’s picture

Status: Needs work » Needs review
StatusFileSize
new9.95 KB
new3.87 KB

More work...

Status: Needs review » Needs work

The last submitted patch, 11: 2869573_11.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

mile23’s picture

Status: Needs work » Needs review
StatusFileSize
new9.99 KB
new2.98 KB

Another try. This looks pretty complete.

I would love to be able to test run-tests.sh but I can't.

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

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now 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.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now 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.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

berdir’s picture

Status: Needs review » Needs work

Doesn't apply anymore.

mile23’s picture

Status: Needs work » Needs review
StatusFileSize
new10.16 KB

Reroll.

Status: Needs review » Needs work

The last submitted patch, 18: 2869573_18.patch, failed testing. View results

mile23’s picture

Status: Needs work » Needs review
Issue tags: +Needs followup
StatusFileSize
new11.77 KB
new1.61 KB

Fixing usages in Drupal\FunctionalTests\Installer\InstallerTestBase and \Drupal\TestSite\Commands\TestSiteUserLoginCommand.

There's also a @todo in FunctionalTestSetupTrait::rebuildAll() that will need some attention. Calling it a follow-up for now.

    // Explicitly call register() again on the container registered in \Drupal.
    // @todo This should already be called through
    //   DrupalKernel::prepareLegacyRequest() -> DrupalKernel::boot() but that
    //   appears to be calling a different container.
alexpott’s picture

  1. This is looking good - nice work!
  2. +++ b/core/authorize.php
    @@ -65,7 +67,17 @@ function authorize_access_allowed(Request $request) {
    +  $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, new Route('<none>'));
    +  $request->attributes->set(RouteObjectInterface::ROUTE_NAME, '<none>');
    +  $container->get('request_stack')->push($request);
    

    The pushing the request onto the stack is done in preHandle() already :) So we could move the request setting stuff above $kernel->preHandle().

    Also we should see how much of this is really needed.

  3. +++ b/core/lib/Drupal/Core/DrupalKernel.php
    @@ -753,6 +753,7 @@ public function prepareLegacyRequest(Request $request) {
    +    @trigger_error(__METHOD__ . ' is deprecated Drupal 8.0.x and will be removed before 9.0.0. Use DrupalKernel::boot() and DrupalKernel::preHandle() instead.', E_USER_DEPRECATED);
    

    __NAMESPACE__ . '\DrupalKernel::prepareLegacyRequest...' is the usual construction for this.

  4. +++ b/core/lib/Drupal/Core/Test/FunctionalTestSetupTrait.php
    @@ -386,13 +388,17 @@ protected function initUserSession() {
         $this->kernel = DrupalKernel::createFromRequest($request, $this->classLoader, 'prod', TRUE);
    -
    +    $this->kernel->boot();
    +    $this->kernel->preHandle($request);
         // Force the container to be built from scratch instead of loaded from the
         // disk. This forces us to not accidentally load the parent site.
    -    $this->kernel->invalidateContainer();
    -
    -    $this->kernel->prepareLegacyRequest($request);
    -    return \Drupal::getContainer();
    +    $container = $this->kernel->rebuildContainer();
    +    // Add our request to the stack and route context.
    +    $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, new Route('<none>'));
    +    $request->attributes->set(RouteObjectInterface::ROUTE_NAME, '<none>');
    +    $container->get('request_stack')->push($request);
    +    $container->get('router.request_context')->fromRequest($request);
    +    return $container;
    

    booting and then rebuilding feels really odd - I'm sure this can be done in a more optimal way.

  5. +++ b/core/tests/Drupal/KernelTests/KernelTestBase.php
    @@ -341,10 +343,14 @@ private function bootKernel() {
    +    $kernel->preHandle($request);
    ...
    +    $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, new Route('<none>'));
    +    $request->attributes->set(RouteObjectInterface::ROUTE_NAME, '<none>');
    

    Let's move these above the preHandle and then we can remove the push to the stack.

alexpott’s picture

StatusFileSize
new4.62 KB
new11.63 KB

I've tried to address #21. Things might break.

alexpott’s picture

StatusFileSize
new2.4 KB
new13.08 KB

Let's add some test coverage because we can.

alexpott’s picture

StatusFileSize
new690 bytes
new13.76 KB

At the moment in kernel tests the request stack has 3 requests (all the same mind you). But that seems worth testing that we don't mess that up again.

The interdiff is the test only patch.

The last submitted patch, 24: 2869573-24.test-only.patch, failed testing. View results

mile23’s picture

Also we should see how much of this is really needed.

That pretty much sums up the scope of the issue. :-)

  /**
   * Helper method that does request related initialization.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The current request.
   */
  public function preHandle(Request $request);

It would be great if we could expand this documentation a little, so we know why a preHandle() is necessary and why it's separate from handle(). Otherwise it still kind of looks a lot like prepareLegacyRequest().

+++ b/core/tests/Drupal/KernelTests/Core/DrupalKernel/DrupalKernelTest.php
@@ -191,4 +191,26 @@ public function testPreventChangeOfSitePath() {
+    if (isset($modules_enabled)) {
+      $kernel->updateModules($modules_enabled);
+    }

I don't see where $modules_enabled is coming from.

alexpott’s picture

StatusFileSize
new841 bytes
new13.67 KB

I don't think it's the job of this issue to do add documentation to DrupalKernel::preHandle() - also the situations where we are calling it are not really to be duplicated in contrib or anywhere. Usually preHandle() is called by part of the handle() - ie. by \Drupal\Core\StackMiddleware\KernelPreHandle - as that class says it's really about preparing the environment if page caching as not kicked in.

Nice spot on the copy pasta. Fix in the patch.

andypost’s picture

Related issues: +#2613044: Requests are pushed onto the request stack twice, popped once

This request manipulations reminds me about related #2613044: Requests are pushed onto the request stack twice, popped once

jibran’s picture

jibran’s picture

alexpott’s picture

@jibran both of them already need an update. This is not deprecating the method. It already is deprecated.

wim leers’s picture

#28 hah I came here too to point to #2613044: Requests are pushed onto the request stack twice, popped once :)

+++ b/core/tests/Drupal/KernelTests/KernelTestBase.php
@@ -358,8 +362,6 @@ private function bootKernel() {
-    $this->container->get('request_stack')->push($request);

This is the only push/pop-related change and definitely won't fix #2613044: Requests are pushed onto the request stack twice, popped once. I was sneakily hoping this would also fix #2613044…

moshe weitzman’s picture

This method may already be deprecated, but it was never clear how to replace it. This patch answers that question - thanks. I will update both Drush and DTT accordingly.

moshe weitzman’s picture

Drush and Drupal Test Traits have both been updated for this change. Releases will come soonish.

voleger’s picture

Status: Needs review » Needs work
+++ b/core/lib/Drupal/Core/DrupalKernel.php
@@ -753,6 +753,7 @@ public function prepareLegacyRequest(Request $request) {
+    @trigger_error(__NAMESPACE__ . '\DrupalKernel::prepareLegacyRequest is deprecated Drupal 8.0.x and will be removed before 9.0.0. Use DrupalKernel::boot() and DrupalKernel::preHandle() instead.', E_USER_DEPRECATED);

+++ b/core/lib/Drupal/Core/DrupalKernelInterface.php
@@ -132,8 +132,8 @@ public function invalidateContainer();
+   * @deprecated in Drupal 8.0.x and will be removed before 9.0.0. Use
+   *   DrupalKernel::boot() and DrupalKernel::preHandle() instead.

+++ b/core/tests/Drupal/KernelTests/Core/DrupalKernel/DrupalKernelTest.php
@@ -191,4 +191,24 @@ public function testPreventChangeOfSitePath() {
+   * @expectedDeprecation Drupal\Core\DrupalKernel::prepareLegacyRequest is deprecated Drupal 8.0.x and will be removed before 9.0.0. Use DrupalKernel::boot() and DrupalKernel::preHandle() instead.

Deprecation message needs to be updated regarding CS

mikelutz’s picture

Status: Needs work » Needs review
StatusFileSize
new13.8 KB
new2.6 KB

updating deprecations, adding a quick CR.

lendude’s picture

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

Updated the IS with the current fix.

This looks ready to me, all feedback has been addressed and all tests are green.

wim leers’s picture

@moshe weitzman++ for updating Drush & Drupal Test Traits!

And yay for RTBC! #24 added explicit backwards compatibility test coverage. I was first confused by all the

$request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, new Route('<none>'));

lines, but that's what the deprecated method used to do. So this makes sense.

+++ b/core/lib/Drupal/Core/Test/FunctionalTestSetupTrait.php
@@ -446,7 +450,6 @@ protected function rebuildAll() {
     // @todo This should already be called through

This was the reason #20 added a Needs followup issue tag, and since this line is still there, we still need that follow-up.

larowlan’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new2.93 KB
new13.81 KB
+++ b/core/authorize.php
@@ -65,7 +67,15 @@ function authorize_access_allowed(Request $request) {
+  $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, new Route('<none>'));
+  $request->attributes->set(RouteObjectInterface::ROUTE_NAME, '<none>');

I wonder whether we could put this in Kernel::preHandle - it feels like knowledge that outside consumers should not need to know

Let's see whether that's do-able

If this works, our recommended approach would just be ->boot()->preHandle($request) because of chaining

mile23’s picture

I wonder whether we could put this in Kernel::preHandle - it feels like knowledge that outside consumers should not need to know

The idea of preHandle() seems to fall into that category, too. That's why I wanted to update the docblocks in #26.

Status: Needs review » Needs work

The last submitted patch, 39: 2869573-kernel-39.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

larowlan’s picture

Status: Needs work » Reviewed & tested by the community

Guess not, back to RTBC for the patch at #36

larowlan’s picture

larowlan’s picture

Status: Reviewed & tested by the community » Fixed

Committed the patch at #36

Committed 8181767 and pushed to 8.8.x. Thanks!

  • larowlan committed 8181767 on 8.8.x
    Issue #2869573 by Mile23, alexpott, mikelutz, voleger: Remove usages of...

Status: Fixed » Closed (fixed)

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

quietone’s picture

Published change record

andypost’s picture