TraceableEventDispatcher.php
89 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\Debug; |
| 12 | |
| 13 | use Matomo\Dependencies\Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher as BaseTraceableEventDispatcher; |
| 14 | use Matomo\Dependencies\Symfony\Component\HttpKernel\KernelEvents; |
| 15 | /** |
| 16 | * Collects some data about event listeners. |
| 17 | * |
| 18 | * This event dispatcher delegates the dispatching to another one. |
| 19 | * |
| 20 | * @author Fabien Potencier <fabien@symfony.com> |
| 21 | */ |
| 22 | class TraceableEventDispatcher extends BaseTraceableEventDispatcher |
| 23 | { |
| 24 | /** |
| 25 | * {@inheritdoc} |
| 26 | */ |
| 27 | protected function beforeDispatch(string $eventName, object $event) |
| 28 | { |
| 29 | switch ($eventName) { |
| 30 | case KernelEvents::REQUEST: |
| 31 | $event->getRequest()->attributes->set('_stopwatch_token', substr(hash('sha256', uniqid(mt_rand(), \true)), 0, 6)); |
| 32 | $this->stopwatch->openSection(); |
| 33 | break; |
| 34 | case KernelEvents::VIEW: |
| 35 | case KernelEvents::RESPONSE: |
| 36 | // stop only if a controller has been executed |
| 37 | if ($this->stopwatch->isStarted('controller')) { |
| 38 | $this->stopwatch->stop('controller'); |
| 39 | } |
| 40 | break; |
| 41 | case KernelEvents::TERMINATE: |
| 42 | $sectionId = $event->getRequest()->attributes->get('_stopwatch_token'); |
| 43 | if (null === $sectionId) { |
| 44 | break; |
| 45 | } |
| 46 | // There is a very special case when using built-in AppCache class as kernel wrapper, in the case |
| 47 | // of an ESI request leading to a `stale` response [B] inside a `fresh` cached response [A]. |
| 48 | // In this case, `$token` contains the [B] debug token, but the open `stopwatch` section ID |
| 49 | // is equal to the [A] debug token. Trying to reopen section with the [B] token throws an exception |
| 50 | // which must be caught. |
| 51 | try { |
| 52 | $this->stopwatch->openSection($sectionId); |
| 53 | } catch (\LogicException $e) { |
| 54 | } |
| 55 | break; |
| 56 | } |
| 57 | } |
| 58 | /** |
| 59 | * {@inheritdoc} |
| 60 | */ |
| 61 | protected function afterDispatch(string $eventName, object $event) |
| 62 | { |
| 63 | switch ($eventName) { |
| 64 | case KernelEvents::CONTROLLER_ARGUMENTS: |
| 65 | $this->stopwatch->start('controller', 'section'); |
| 66 | break; |
| 67 | case KernelEvents::RESPONSE: |
| 68 | $sectionId = $event->getRequest()->attributes->get('_stopwatch_token'); |
| 69 | if (null === $sectionId) { |
| 70 | break; |
| 71 | } |
| 72 | $this->stopwatch->stopSection($sectionId); |
| 73 | break; |
| 74 | case KernelEvents::TERMINATE: |
| 75 | // In the special case described in the `preDispatch` method above, the `$token` section |
| 76 | // does not exist, then closing it throws an exception which must be caught. |
| 77 | $sectionId = $event->getRequest()->attributes->get('_stopwatch_token'); |
| 78 | if (null === $sectionId) { |
| 79 | break; |
| 80 | } |
| 81 | try { |
| 82 | $this->stopwatch->stopSection($sectionId); |
| 83 | } catch (\LogicException $e) { |
| 84 | } |
| 85 | break; |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 |