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