DebugStack.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace Doctrine\DBAL\Logging;
  3. use function microtime;
  4. /**
  5. * Includes executed SQLs in a Debug Stack.
  6. */
  7. class DebugStack implements SQLLogger
  8. {
  9. /**
  10. * Executed SQL queries.
  11. *
  12. * @var mixed[][]
  13. */
  14. public $queries = [];
  15. /**
  16. * If Debug Stack is enabled (log queries) or not.
  17. *
  18. * @var bool
  19. */
  20. public $enabled = true;
  21. /** @var float|null */
  22. public $start = null;
  23. /** @var int */
  24. public $currentQuery = 0;
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function startQuery($sql, ?array $params = null, ?array $types = null)
  29. {
  30. if (! $this->enabled) {
  31. return;
  32. }
  33. $this->start = microtime(true);
  34. $this->queries[++$this->currentQuery] = ['sql' => $sql, 'params' => $params, 'types' => $types, 'executionMS' => 0];
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function stopQuery()
  40. {
  41. if (! $this->enabled) {
  42. return;
  43. }
  44. $this->queries[$this->currentQuery]['executionMS'] = microtime(true) - $this->start;
  45. }
  46. }