class KernelDestructionSubscriber

Same name in other branches
  1. 9 core/lib/Drupal/Core/EventSubscriber/KernelDestructionSubscriber.php \Drupal\Core\EventSubscriber\KernelDestructionSubscriber
  2. 10 core/lib/Drupal/Core/EventSubscriber/KernelDestructionSubscriber.php \Drupal\Core\EventSubscriber\KernelDestructionSubscriber
  3. 11.x core/lib/Drupal/Core/EventSubscriber/KernelDestructionSubscriber.php \Drupal\Core\EventSubscriber\KernelDestructionSubscriber

Destructs services that are initiated and tagged with "needs_destruction".

Hierarchy

  • class \Drupal\Core\EventSubscriber\KernelDestructionSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface, \Symfony\Component\DependencyInjection\ContainerAwareInterface uses \Symfony\Component\DependencyInjection\ContainerAwareTrait

Expanded class hierarchy of KernelDestructionSubscriber

See also

\Drupal\Core\DestructableInterface

1 string reference to 'KernelDestructionSubscriber'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses KernelDestructionSubscriber
kernel_destruct_subscriber in core/core.services.yml
Drupal\Core\EventSubscriber\KernelDestructionSubscriber

File

core/lib/Drupal/Core/EventSubscriber/KernelDestructionSubscriber.php, line 16

Namespace

Drupal\Core\EventSubscriber
View source
class KernelDestructionSubscriber implements EventSubscriberInterface, ContainerAwareInterface {
    use ContainerAwareTrait;
    
    /**
     * Holds an array of service ID's that will require destruction.
     *
     * @var array
     */
    protected $services = [];
    
    /**
     * Registers a service for destruction.
     *
     * Calls to this method are set up in
     * RegisterServicesForDestructionPass::process().
     *
     * @param string $id
     *   Name of the service.
     */
    public function registerService($id) {
        $this->services[] = $id;
    }
    
    /**
     * Invoked by the terminate kernel event.
     *
     * @param \Symfony\Component\HttpKernel\Event\PostResponseEvent $event
     *   The event object.
     */
    public function onKernelTerminate(PostResponseEvent $event) {
        foreach ($this->services as $id) {
            // Check if the service was initialized during this request, destruction
            // is not necessary if the service was not used.
            if ($this->container
                ->initialized($id)) {
                $service = $this->container
                    ->get($id);
                $service->destruct();
            }
        }
    }
    
    /**
     * Registers the methods in this class that should be listeners.
     *
     * @return array
     *   An array of event listener definitions.
     */
    public static function getSubscribedEvents() {
        $events[KernelEvents::TERMINATE][] = [
            'onKernelTerminate',
            100,
        ];
        return $events;
    }

}

Members

Title Sort descending Modifiers Object type Summary
KernelDestructionSubscriber::$services protected property Holds an array of service ID's that will require destruction.
KernelDestructionSubscriber::getSubscribedEvents public static function Registers the methods in this class that should be listeners.
KernelDestructionSubscriber::onKernelTerminate public function Invoked by the terminate kernel event.
KernelDestructionSubscriber::registerService public function Registers a service for destruction.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.