AjaxDataCollector.php
2 years ago
ConfigDataCollector.php
1 year ago
DataCollector.php
2 years ago
DataCollectorInterface.php
2 years ago
DumpDataCollector.php
1 year ago
EventDataCollector.php
2 years ago
ExceptionDataCollector.php
2 years ago
LateDataCollectorInterface.php
2 years ago
LoggerDataCollector.php
1 year ago
MemoryDataCollector.php
1 year ago
RequestDataCollector.php
1 year ago
RouterDataCollector.php
1 year ago
TimeDataCollector.php
1 year ago
TimeDataCollector.php
121 lines
| 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 | namespace Matomo\Dependencies\Symfony\Component\HttpKernel\DataCollector; |
| 12 | |
| 13 | use Matomo\Dependencies\Symfony\Component\HttpFoundation\Request; |
| 14 | use Matomo\Dependencies\Symfony\Component\HttpFoundation\Response; |
| 15 | use Matomo\Dependencies\Symfony\Component\HttpKernel\KernelInterface; |
| 16 | use Symfony\Component\Stopwatch\Stopwatch; |
| 17 | use Symfony\Component\Stopwatch\StopwatchEvent; |
| 18 | /** |
| 19 | * @author Fabien Potencier <fabien@symfony.com> |
| 20 | * |
| 21 | * @final |
| 22 | */ |
| 23 | class TimeDataCollector extends DataCollector implements LateDataCollectorInterface |
| 24 | { |
| 25 | private $kernel; |
| 26 | private $stopwatch; |
| 27 | public function __construct(?KernelInterface $kernel = null, ?Stopwatch $stopwatch = null) |
| 28 | { |
| 29 | $this->kernel = $kernel; |
| 30 | $this->stopwatch = $stopwatch; |
| 31 | $this->data = ['events' => [], 'stopwatch_installed' => \false, 'start_time' => 0]; |
| 32 | } |
| 33 | /** |
| 34 | * {@inheritdoc} |
| 35 | */ |
| 36 | public function collect(Request $request, Response $response, ?\Throwable $exception = null) |
| 37 | { |
| 38 | if (null !== $this->kernel) { |
| 39 | $startTime = $this->kernel->getStartTime(); |
| 40 | } else { |
| 41 | $startTime = $request->server->get('REQUEST_TIME_FLOAT'); |
| 42 | } |
| 43 | $this->data = ['token' => $request->attributes->get('_stopwatch_token'), 'start_time' => $startTime * 1000, 'events' => [], 'stopwatch_installed' => class_exists(Stopwatch::class, \false)]; |
| 44 | } |
| 45 | /** |
| 46 | * {@inheritdoc} |
| 47 | */ |
| 48 | public function reset() |
| 49 | { |
| 50 | $this->data = ['events' => [], 'stopwatch_installed' => \false, 'start_time' => 0]; |
| 51 | if (null !== $this->stopwatch) { |
| 52 | $this->stopwatch->reset(); |
| 53 | } |
| 54 | } |
| 55 | /** |
| 56 | * {@inheritdoc} |
| 57 | */ |
| 58 | public function lateCollect() |
| 59 | { |
| 60 | if (null !== $this->stopwatch && isset($this->data['token'])) { |
| 61 | $this->setEvents($this->stopwatch->getSectionEvents($this->data['token'])); |
| 62 | } |
| 63 | unset($this->data['token']); |
| 64 | } |
| 65 | /** |
| 66 | * @param StopwatchEvent[] $events The request events |
| 67 | */ |
| 68 | public function setEvents(array $events) |
| 69 | { |
| 70 | foreach ($events as $event) { |
| 71 | $event->ensureStopped(); |
| 72 | } |
| 73 | $this->data['events'] = $events; |
| 74 | } |
| 75 | /** |
| 76 | * @return StopwatchEvent[] |
| 77 | */ |
| 78 | public function getEvents() : array |
| 79 | { |
| 80 | return $this->data['events']; |
| 81 | } |
| 82 | /** |
| 83 | * Gets the request elapsed time. |
| 84 | */ |
| 85 | public function getDuration() : float |
| 86 | { |
| 87 | if (!isset($this->data['events']['__section__'])) { |
| 88 | return 0; |
| 89 | } |
| 90 | $lastEvent = $this->data['events']['__section__']; |
| 91 | return $lastEvent->getOrigin() + $lastEvent->getDuration() - $this->getStartTime(); |
| 92 | } |
| 93 | /** |
| 94 | * Gets the initialization time. |
| 95 | * |
| 96 | * This is the time spent until the beginning of the request handling. |
| 97 | */ |
| 98 | public function getInitTime() : float |
| 99 | { |
| 100 | if (!isset($this->data['events']['__section__'])) { |
| 101 | return 0; |
| 102 | } |
| 103 | return $this->data['events']['__section__']->getOrigin() - $this->getStartTime(); |
| 104 | } |
| 105 | public function getStartTime() : float |
| 106 | { |
| 107 | return $this->data['start_time']; |
| 108 | } |
| 109 | public function isStopwatchInstalled() : bool |
| 110 | { |
| 111 | return $this->data['stopwatch_installed']; |
| 112 | } |
| 113 | /** |
| 114 | * {@inheritdoc} |
| 115 | */ |
| 116 | public function getName() : string |
| 117 | { |
| 118 | return 'time'; |
| 119 | } |
| 120 | } |
| 121 |