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; } }