vendor/symfony/mercure/src/Debug/TraceablePublisher.php line 21

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the Mercure Component project.
  4. *
  5. * (c) Kévin Dunglas <dunglas@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. declare(strict_types=1);
  11. namespace Symfony\Component\Mercure\Debug;
  12. use Symfony\Component\Mercure\PublisherInterface;
  13. use Symfony\Component\Mercure\Update;
  14. use Symfony\Component\Stopwatch\Stopwatch;
  15. use Symfony\Contracts\Service\ResetInterface;
  16. trigger_deprecation('symfony/mercure', '0.5', 'Class "%s" is deprecated, use "%s" instead.', TraceablePublisher::class, TraceableHub::class);
  17. /**
  18. * Traces updates for profiler.
  19. *
  20. * @author Vincent Chalamon <vincentchalamon@gmail.com>
  21. *
  22. * @experimental
  23. *
  24. * @deprecated
  25. */
  26. final class TraceablePublisher implements PublisherInterface, ResetInterface
  27. {
  28. private $publisher;
  29. private $stopwatch;
  30. private $messages = [];
  31. public function __construct(PublisherInterface $publisher, Stopwatch $stopwatch)
  32. {
  33. $this->publisher = $publisher;
  34. $this->stopwatch = $stopwatch;
  35. }
  36. public function __invoke(Update $update): string
  37. {
  38. $this->stopwatch->start(__CLASS__);
  39. $content = ($this->publisher)($update);
  40. $e = $this->stopwatch->stop(__CLASS__);
  41. $this->messages[] = [
  42. 'object' => $update,
  43. 'duration' => $e->getDuration(),
  44. 'memory' => $e->getMemory(),
  45. ];
  46. return $content;
  47. }
  48. public function reset(): void
  49. {
  50. $this->messages = [];
  51. }
  52. public function count(): int
  53. {
  54. return \count($this->messages);
  55. }
  56. public function getMessages(): array
  57. {
  58. return $this->messages;
  59. }
  60. public function getDuration(): float
  61. {
  62. return array_sum(array_map(function ($a) {
  63. return $a['duration'];
  64. }, $this->messages));
  65. }
  66. public function getMemory(): int
  67. {
  68. return (int) array_sum(array_map(function ($a) {
  69. return $a['memory'];
  70. }, $this->messages));
  71. }
  72. }