Profile.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. declare(strict_types = 1);
  3. /**
  4. * @copyright 2022 Carl Schwan <carl@carlschwan.eu>
  5. *
  6. * @author Carl Schwan <carl@carlschwan.eu>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\Profiler;
  25. use OCP\DataCollector\IDataCollector;
  26. use OCP\Profiler\IProfile;
  27. class Profile implements \JsonSerializable, IProfile {
  28. private string $token;
  29. private ?int $time = null;
  30. private ?string $url = null;
  31. private ?string $method = null;
  32. private ?int $statusCode = null;
  33. /** @var array<string, IDataCollector> */
  34. private array $collectors = [];
  35. private ?IProfile $parent = null;
  36. /** @var IProfile[] */
  37. private array $children = [];
  38. public function __construct(string $token) {
  39. $this->token = $token;
  40. }
  41. public function getToken(): string {
  42. return $this->token;
  43. }
  44. public function setToken(string $token): void {
  45. $this->token = $token;
  46. }
  47. public function getTime(): ?int {
  48. return $this->time;
  49. }
  50. public function setTime(int $time): void {
  51. $this->time = $time;
  52. }
  53. public function getUrl(): ?string {
  54. return $this->url;
  55. }
  56. public function setUrl(string $url): void {
  57. $this->url = $url;
  58. }
  59. public function getMethod(): ?string {
  60. return $this->method;
  61. }
  62. public function setMethod(string $method): void {
  63. $this->method = $method;
  64. }
  65. public function getStatusCode(): ?int {
  66. return $this->statusCode;
  67. }
  68. public function setStatusCode(int $statusCode): void {
  69. $this->statusCode = $statusCode;
  70. }
  71. public function addCollector(IDataCollector $collector) {
  72. $this->collectors[$collector->getName()] = $collector;
  73. }
  74. public function getParent(): ?IProfile {
  75. return $this->parent;
  76. }
  77. public function setParent(?IProfile $parent): void {
  78. $this->parent = $parent;
  79. }
  80. public function getParentToken(): ?string {
  81. return $this->parent ? $this->parent->getToken() : null;
  82. }
  83. /** @return IProfile[] */
  84. public function getChildren(): array {
  85. return $this->children;
  86. }
  87. /**
  88. * @param IProfile[] $children
  89. */
  90. public function setChildren(array $children): void {
  91. $this->children = [];
  92. foreach ($children as $child) {
  93. $this->addChild($child);
  94. }
  95. }
  96. public function addChild(IProfile $profile): void {
  97. $this->children[] = $profile;
  98. $profile->setParent($this);
  99. }
  100. /**
  101. * @return IDataCollector[]
  102. */
  103. public function getCollectors(): array {
  104. return $this->collectors;
  105. }
  106. /**
  107. * @param IDataCollector[] $collectors
  108. */
  109. public function setCollectors(array $collectors): void {
  110. $this->collectors = $collectors;
  111. }
  112. public function __sleep(): array {
  113. return ['token', 'parent', 'children', 'collectors', 'method', 'url', 'time', 'statusCode'];
  114. }
  115. #[\ReturnTypeWillChange]
  116. public function jsonSerialize() {
  117. // Everything but parent
  118. return [
  119. 'token' => $this->token,
  120. 'method' => $this->method,
  121. 'children' => $this->children,
  122. 'url' => $this->url,
  123. 'statusCode' => $this->statusCode,
  124. 'time' => $this->time,
  125. 'collectors' => $this->collectors,
  126. ];
  127. }
  128. public function getCollector(string $collectorName): ?IDataCollector {
  129. if (!array_key_exists($collectorName, $this->collectors)) {
  130. return null;
  131. }
  132. return $this->collectors[$collectorName];
  133. }
  134. }