Verified Commit e5fc18c5 authored by Dave Long's avatar Dave Long
Browse files

Issue #3412556 by catch, smustgrave, andypost, longwave: Allow...

Issue #3412556 by catch, smustgrave, andypost, longwave: Allow needs_destruction services to run on page cache hits
parent 80d83c68
Loading
Loading
Loading
Loading
Loading
+0 −4
Original line number Diff line number Diff line
@@ -1492,10 +1492,6 @@ services:
    class: Drupal\Core\StreamWrapper\TemporaryStream
    tags:
      - { name: stream_wrapper, scheme: temporary }
  kernel_destruct_subscriber:
    class: Drupal\Core\EventSubscriber\KernelDestructionSubscriber
    calls:
      - [setContainer, ['@service_container']]
  image.toolkit.manager:
    class: Drupal\Core\ImageToolkit\ImageToolkitManager
    arguments: ['@config.factory']
+2 −9
Original line number Diff line number Diff line
@@ -6,7 +6,7 @@
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;

/**
 * Adds services with specific tags to "kernel_destruct_subscriber" service.
 * Adds services to the "kernel.destructable_services" container parameter.
 *
 * Only services tagged with "needs_destruction" are added.
 *
@@ -18,15 +18,8 @@ class RegisterServicesForDestructionPass implements CompilerPassInterface {
   * {@inheritdoc}
   */
  public function process(ContainerBuilder $container) {
    if (!$container->hasDefinition('kernel_destruct_subscriber')) {
      return;
    }

    $definition = $container->getDefinition('kernel_destruct_subscriber');
    $services = $container->findTaggedServiceIds('needs_destruction');
    foreach ($services as $id => $attributes) {
      $definition->addMethodCall('registerService', [$id]);
    }
    $container->setParameter('kernel.destructable_services', array_keys($services));
  }

}
+14 −7
Original line number Diff line number Diff line
@@ -685,15 +685,22 @@ public function getServiceProviders($origin) {
   * @return void
   */
  public function terminate(Request $request, Response $response) {
    if ($this->getHttpKernel() instanceof TerminableInterface) {
      // Only run terminate() when essential services have been set up properly
      // by preHandle() before.
    if (FALSE === $this->prepared) {
      return;
    }

    if ($this->getHttpKernel() instanceof TerminableInterface) {
      if ($this->prepared === TRUE) {
        $this->getHttpKernel()->terminate($request, $response);
      }
      // For destructable services, always call the destruct method if they were
      // initialized during the request. Destruction is not necessary if the
      // service was not used.
      foreach ($this->container->getParameter('kernel.destructable_services') as $id) {
        if ($this->container->initialized($id)) {
          $service = $this->container->get($id);
          $service->destruct();
        }
      }
    }
  }

  /**
+3 −0
Original line number Diff line number Diff line
@@ -11,6 +11,9 @@
/**
 * Destructs services that are initiated and tagged with "needs_destruction".
 *
 * @deprecated in drupal:10.3.0 and is removed from drupal:11.0.0. There is no
 * replacement.
 * @see https://www.drupal.org/node/3416021
 * @see \Drupal\Core\DestructableInterface
 */
class KernelDestructionSubscriber implements EventSubscriberInterface, ContainerAwareInterface {
+1 −3
Original line number Diff line number Diff line
@@ -60,9 +60,7 @@ public function onKernelTerminate(TerminateEvent $event) {
   * {@inheritdoc}
   */
  public static function getSubscribedEvents(): array {
    // Should go before other subscribers start to write their caches. Notably
    // before \Drupal\Core\EventSubscriber\KernelDestructionSubscriber to
    // prevent instantiation of destructed services.
    // Should go before other subscribers start to write their caches.
    $events[KernelEvents::TERMINATE][] = ['onKernelTerminate', 300];
    return $events;
  }
Loading