1
0

Profile.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. declare(strict_types = 1);
  3. /**
  4. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Profiler;
  8. use OCP\DataCollector\IDataCollector;
  9. use OCP\Profiler\IProfile;
  10. class Profile implements \JsonSerializable, IProfile {
  11. private string $token;
  12. private ?int $time = null;
  13. private ?string $url = null;
  14. private ?string $method = null;
  15. private ?int $statusCode = null;
  16. /** @var array<string, IDataCollector> */
  17. private array $collectors = [];
  18. private ?IProfile $parent = null;
  19. /** @var IProfile[] */
  20. private array $children = [];
  21. public function __construct(string $token) {
  22. $this->token = $token;
  23. }
  24. public function getToken(): string {
  25. return $this->token;
  26. }
  27. public function setToken(string $token): void {
  28. $this->token = $token;
  29. }
  30. public function getTime(): ?int {
  31. return $this->time;
  32. }
  33. public function setTime(int $time): void {
  34. $this->time = $time;
  35. }
  36. public function getUrl(): ?string {
  37. return $this->url;
  38. }
  39. public function setUrl(string $url): void {
  40. $this->url = $url;
  41. }
  42. public function getMethod(): ?string {
  43. return $this->method;
  44. }
  45. public function setMethod(string $method): void {
  46. $this->method = $method;
  47. }
  48. public function getStatusCode(): ?int {
  49. return $this->statusCode;
  50. }
  51. public function setStatusCode(int $statusCode): void {
  52. $this->statusCode = $statusCode;
  53. }
  54. public function addCollector(IDataCollector $collector) {
  55. $this->collectors[$collector->getName()] = $collector;
  56. }
  57. public function getParent(): ?IProfile {
  58. return $this->parent;
  59. }
  60. public function setParent(?IProfile $parent): void {
  61. $this->parent = $parent;
  62. }
  63. public function getParentToken(): ?string {
  64. return $this->parent ? $this->parent->getToken() : null;
  65. }
  66. /** @return IProfile[] */
  67. public function getChildren(): array {
  68. return $this->children;
  69. }
  70. /**
  71. * @param IProfile[] $children
  72. */
  73. public function setChildren(array $children): void {
  74. $this->children = [];
  75. foreach ($children as $child) {
  76. $this->addChild($child);
  77. }
  78. }
  79. public function addChild(IProfile $profile): void {
  80. $this->children[] = $profile;
  81. $profile->setParent($this);
  82. }
  83. /**
  84. * @return IDataCollector[]
  85. */
  86. public function getCollectors(): array {
  87. return $this->collectors;
  88. }
  89. /**
  90. * @param IDataCollector[] $collectors
  91. */
  92. public function setCollectors(array $collectors): void {
  93. $this->collectors = $collectors;
  94. }
  95. public function __sleep(): array {
  96. return ['token', 'parent', 'children', 'collectors', 'method', 'url', 'time', 'statusCode'];
  97. }
  98. #[\ReturnTypeWillChange]
  99. public function jsonSerialize() {
  100. // Everything but parent
  101. return [
  102. 'token' => $this->token,
  103. 'method' => $this->method,
  104. 'children' => $this->children,
  105. 'url' => $this->url,
  106. 'statusCode' => $this->statusCode,
  107. 'time' => $this->time,
  108. 'collectors' => $this->collectors,
  109. ];
  110. }
  111. public function getCollector(string $collectorName): ?IDataCollector {
  112. if (!array_key_exists($collectorName, $this->collectors)) {
  113. return null;
  114. }
  115. return $this->collectors[$collectorName];
  116. }
  117. }