comparison core/lib/Drupal/Core/EventSubscriber/KernelDestructionSubscriber.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Core\EventSubscriber;
4
5 use Symfony\Component\DependencyInjection\ContainerAwareInterface;
6 use Symfony\Component\DependencyInjection\ContainerAwareTrait;
7 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
8 use Symfony\Component\HttpKernel\Event\PostResponseEvent;
9 use Symfony\Component\HttpKernel\KernelEvents;
10
11 /**
12 * Destructs services that are initiated and tagged with "needs_destruction".
13 *
14 * @see \Drupal\Core\DestructableInterface
15 */
16 class KernelDestructionSubscriber implements EventSubscriberInterface, ContainerAwareInterface {
17
18 use ContainerAwareTrait;
19 /**
20 * Holds an array of service ID's that will require destruction.
21 *
22 * @var array
23 */
24 protected $services = [];
25
26 /**
27 * Registers a service for destruction.
28 *
29 * Calls to this method are set up in
30 * RegisterServicesForDestructionPass::process().
31 *
32 * @param string $id
33 * Name of the service.
34 */
35 public function registerService($id) {
36 $this->services[] = $id;
37 }
38
39 /**
40 * Invoked by the terminate kernel event.
41 *
42 * @param \Symfony\Component\HttpKernel\Event\PostResponseEvent $event
43 * The event object.
44 */
45 public function onKernelTerminate(PostResponseEvent $event) {
46 foreach ($this->services as $id) {
47 // Check if the service was initialized during this request, destruction
48 // is not necessary if the service was not used.
49 if ($this->container->initialized($id)) {
50 $service = $this->container->get($id);
51 $service->destruct();
52 }
53 }
54 }
55
56 /**
57 * Registers the methods in this class that should be listeners.
58 *
59 * @return array
60 * An array of event listener definitions.
61 */
62 public static function getSubscribedEvents() {
63 // Run this subscriber after others as those might use services that need
64 // to be terminated as well or run code that needs to run before
65 // termination.
66 $events[KernelEvents::TERMINATE][] = ['onKernelTerminate', -100];
67 return $events;
68 }
69
70 }