vendor/bugsnag/bugsnag-symfony/EventListener/BugsnagShutdown.php line 54

Open in your IDE?
  1. <?php
  2. namespace Bugsnag\BugsnagBundle\EventListener;
  3. use Bugsnag\Client;
  4. use Bugsnag\Shutdown\ShutdownStrategyInterface;
  5. use Symfony\Component\Console\ConsoleEvents;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. use Symfony\Contracts\EventDispatcher\Event;
  9. /**
  10.  * A Shutdown strategy that uses Symfony's TERMINATE event to trigger calls to Client::flush().
  11.  *
  12.  * This is preferred to the default PhpShutdownStrategy for two primary reasons:
  13.  *
  14.  * 1) No memory leaks When running tests: memory used by the Bugsnag\Client can be garbage collected (this is not
  15.  *    possible when register_shutdown_function is used).
  16.  *
  17.  * 2) Better performance: Symfony uses fastcgi_finish_request(), so if PHP-FPM is being used flush() calls take place
  18.  *    after the HTTP connection has closed.
  19.  */
  20. class BugsnagShutdown implements EventSubscriberInterfaceShutdownStrategyInterface
  21. {
  22.     /**
  23.      * @var Client
  24.      */
  25.     private $client;
  26.     /**
  27.      * Indicate which events we wish to subscribe to.
  28.      *
  29.      * @return array
  30.      */
  31.     public static function getSubscribedEvents()
  32.     {
  33.         $listeners = [
  34.             KernelEvents::TERMINATE => ['onTerminate'10],
  35.         ];
  36.         // Added ConsoleEvents in Symfony 2.3
  37.         if (class_exists('Symfony\Component\Console\ConsoleEvents')) {
  38.             $listeners[ConsoleEvents::TERMINATE] = ['onTerminate'10];
  39.         }
  40.         return $listeners;
  41.     }
  42.     /**
  43.      * Called when Symfony shuts down the kernel (after response has been sent).
  44.      *
  45.      * @return void
  46.      */
  47.     public function onTerminate()
  48.     {
  49.         if ($this->client) {
  50.             $this->client->flush();
  51.         }
  52.     }
  53.     /**
  54.      * Implement the ShutdownStrategyInterface.
  55.      *
  56.      * @param \Bugsnag\Client $client
  57.      *
  58.      * @return void
  59.      */
  60.     public function registerShutdownStrategy(Client $client)
  61.     {
  62.         /*
  63.          * Set a reference to the client. This "enables" the onTerminate event.
  64.          */
  65.         $this->client $client;
  66.     }
  67. }