[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/symfony/http-kernel/DataCollector/ -> TimeDataCollector.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <fabien@symfony.com>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Symfony\Component\HttpKernel\DataCollector;
  13  
  14  use Symfony\Component\HttpFoundation\Request;
  15  use Symfony\Component\HttpFoundation\Response;
  16  use Symfony\Component\HttpKernel\KernelInterface;
  17  use Symfony\Component\Stopwatch\Stopwatch;
  18  use Symfony\Component\Stopwatch\StopwatchEvent;
  19  
  20  /**
  21   * @author Fabien Potencier <fabien@symfony.com>
  22   */
  23  class TimeDataCollector extends DataCollector implements LateDataCollectorInterface
  24  {
  25      protected $kernel;
  26      protected $stopwatch;
  27  
  28      public function __construct(KernelInterface $kernel = null, Stopwatch $stopwatch = null)
  29      {
  30          $this->kernel = $kernel;
  31          $this->stopwatch = $stopwatch;
  32      }
  33  
  34      /**
  35       * {@inheritdoc}
  36       */
  37      public function collect(Request $request, Response $response, \Exception $exception = null)
  38      {
  39          if (null !== $this->kernel) {
  40              $startTime = $this->kernel->getStartTime();
  41          } else {
  42              $startTime = $request->server->get('REQUEST_TIME_FLOAT');
  43          }
  44  
  45          $this->data = [
  46              'token' => $response->headers->get('X-Debug-Token'),
  47              'start_time' => $startTime * 1000,
  48              'events' => [],
  49              'stopwatch_installed' => class_exists(Stopwatch::class, false),
  50          ];
  51      }
  52  
  53      /**
  54       * {@inheritdoc}
  55       */
  56      public function reset()
  57      {
  58          $this->data = [];
  59  
  60          if (null !== $this->stopwatch) {
  61              $this->stopwatch->reset();
  62          }
  63      }
  64  
  65      /**
  66       * {@inheritdoc}
  67       */
  68      public function lateCollect()
  69      {
  70          if (null !== $this->stopwatch && isset($this->data['token'])) {
  71              $this->setEvents($this->stopwatch->getSectionEvents($this->data['token']));
  72          }
  73          unset($this->data['token']);
  74      }
  75  
  76      /**
  77       * Sets the request events.
  78       *
  79       * @param StopwatchEvent[] $events The request events
  80       */
  81      public function setEvents(array $events)
  82      {
  83          foreach ($events as $event) {
  84              $event->ensureStopped();
  85          }
  86  
  87          $this->data['events'] = $events;
  88      }
  89  
  90      /**
  91       * Gets the request events.
  92       *
  93       * @return StopwatchEvent[] The request events
  94       */
  95      public function getEvents()
  96      {
  97          return $this->data['events'];
  98      }
  99  
 100      /**
 101       * Gets the request elapsed time.
 102       *
 103       * @return float The elapsed time
 104       */
 105      public function getDuration()
 106      {
 107          if (!isset($this->data['events']['__section__'])) {
 108              return 0;
 109          }
 110  
 111          $lastEvent = $this->data['events']['__section__'];
 112  
 113          return $lastEvent->getOrigin() + $lastEvent->getDuration() - $this->getStartTime();
 114      }
 115  
 116      /**
 117       * Gets the initialization time.
 118       *
 119       * This is the time spent until the beginning of the request handling.
 120       *
 121       * @return float The elapsed time
 122       */
 123      public function getInitTime()
 124      {
 125          if (!isset($this->data['events']['__section__'])) {
 126              return 0;
 127          }
 128  
 129          return $this->data['events']['__section__']->getOrigin() - $this->getStartTime();
 130      }
 131  
 132      /**
 133       * Gets the request time.
 134       *
 135       * @return float
 136       */
 137      public function getStartTime()
 138      {
 139          return $this->data['start_time'];
 140      }
 141  
 142      /**
 143       * @return bool whether or not the stopwatch component is installed
 144       */
 145      public function isStopwatchInstalled()
 146      {
 147          return $this->data['stopwatch_installed'];
 148      }
 149  
 150      /**
 151       * {@inheritdoc}
 152       */
 153      public function getName()
 154      {
 155          return 'time';
 156      }
 157  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1