|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Fetch\Concerns; |
| 6 | + |
| 7 | +use Fetch\Support\DebugInfo; |
| 8 | +use Fetch\Support\FetchProfiler; |
| 9 | + |
| 10 | +/** |
| 11 | + * Trait ManagesDebugAndProfiling |
| 12 | + * |
| 13 | + * Provides debugging and profiling capabilities for HTTP requests. |
| 14 | + */ |
| 15 | +trait ManagesDebugAndProfiling |
| 16 | +{ |
| 17 | + /** |
| 18 | + * Whether debug mode is enabled. |
| 19 | + */ |
| 20 | + protected bool $debugEnabled = false; |
| 21 | + |
| 22 | + /** |
| 23 | + * Debug options configuration. |
| 24 | + * |
| 25 | + * @var array<string, mixed> |
| 26 | + */ |
| 27 | + protected array $debugOptions = []; |
| 28 | + |
| 29 | + /** |
| 30 | + * The profiler instance for performance tracking. |
| 31 | + */ |
| 32 | + protected ?FetchProfiler $profiler = null; |
| 33 | + |
| 34 | + /** |
| 35 | + * The last debug info from the most recent request. |
| 36 | + */ |
| 37 | + protected ?DebugInfo $lastDebugInfo = null; |
| 38 | + |
| 39 | + /** |
| 40 | + * Enable debug mode with specified options. |
| 41 | + * |
| 42 | + * @param array<string, mixed>|bool $options Debug options or true to enable all |
| 43 | + * @return $this |
| 44 | + */ |
| 45 | + public function withDebug(array|bool $options = true): static |
| 46 | + { |
| 47 | + $this->debugEnabled = $options !== false; |
| 48 | + |
| 49 | + if (is_array($options)) { |
| 50 | + $this->debugOptions = array_merge(DebugInfo::getDefaultOptions(), $options); |
| 51 | + } else { |
| 52 | + $this->debugOptions = DebugInfo::getDefaultOptions(); |
| 53 | + } |
| 54 | + |
| 55 | + return $this; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Set a profiler for performance tracking. |
| 60 | + * |
| 61 | + * @param FetchProfiler $profiler The profiler instance |
| 62 | + * @return $this |
| 63 | + */ |
| 64 | + public function withProfiler(FetchProfiler $profiler): static |
| 65 | + { |
| 66 | + $this->profiler = $profiler; |
| 67 | + |
| 68 | + return $this; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Get the profiler instance if set. |
| 73 | + */ |
| 74 | + public function getProfiler(): ?FetchProfiler |
| 75 | + { |
| 76 | + return $this->profiler; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Check if debug mode is enabled. |
| 81 | + */ |
| 82 | + public function isDebugEnabled(): bool |
| 83 | + { |
| 84 | + return $this->debugEnabled; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Get the debug options. |
| 89 | + * |
| 90 | + * @return array<string, mixed> |
| 91 | + */ |
| 92 | + public function getDebugOptions(): array |
| 93 | + { |
| 94 | + return $this->debugOptions; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Get the last debug info from the most recent request. |
| 99 | + */ |
| 100 | + public function getLastDebugInfo(): ?DebugInfo |
| 101 | + { |
| 102 | + return $this->lastDebugInfo; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Create debug info for the current request. |
| 107 | + * |
| 108 | + * @param string $method HTTP method |
| 109 | + * @param string $uri Request URI |
| 110 | + * @param array<string, mixed> $options Request options |
| 111 | + * @param \Psr\Http\Message\ResponseInterface|null $response The response |
| 112 | + * @param array<string, float> $timings Timing information |
| 113 | + * @param int $memoryUsage Memory usage in bytes |
| 114 | + */ |
| 115 | + protected function createDebugInfo( |
| 116 | + string $method, |
| 117 | + string $uri, |
| 118 | + array $options, |
| 119 | + ?\Psr\Http\Message\ResponseInterface $response = null, |
| 120 | + array $timings = [], |
| 121 | + int $memoryUsage = 0 |
| 122 | + ): DebugInfo { |
| 123 | + $this->lastDebugInfo = DebugInfo::create( |
| 124 | + $method, |
| 125 | + $uri, |
| 126 | + $options, |
| 127 | + $response, |
| 128 | + $timings, |
| 129 | + $memoryUsage |
| 130 | + ); |
| 131 | + |
| 132 | + return $this->lastDebugInfo; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Start profiling for a request. |
| 137 | + * |
| 138 | + * @param string $method HTTP method |
| 139 | + * @param string $uri Request URI |
| 140 | + * @return string|null The request ID if profiling is enabled |
| 141 | + */ |
| 142 | + protected function startProfiling(string $method, string $uri): ?string |
| 143 | + { |
| 144 | + if ($this->profiler === null || ! $this->profiler->isEnabled()) { |
| 145 | + return null; |
| 146 | + } |
| 147 | + |
| 148 | + $requestId = FetchProfiler::generateRequestId($method, $uri); |
| 149 | + $this->profiler->startProfile($requestId); |
| 150 | + |
| 151 | + return $requestId; |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Record a profiling event. |
| 156 | + * |
| 157 | + * @param string|null $requestId The request ID |
| 158 | + * @param string $event The event name |
| 159 | + */ |
| 160 | + protected function recordProfilingEvent(?string $requestId, string $event): void |
| 161 | + { |
| 162 | + if ($requestId === null || $this->profiler === null) { |
| 163 | + return; |
| 164 | + } |
| 165 | + |
| 166 | + $this->profiler->recordEvent($requestId, $event); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * End profiling for a request. |
| 171 | + * |
| 172 | + * @param string|null $requestId The request ID |
| 173 | + * @param int|null $statusCode HTTP status code |
| 174 | + */ |
| 175 | + protected function endProfiling(?string $requestId, ?int $statusCode = null): void |
| 176 | + { |
| 177 | + if ($requestId === null || $this->profiler === null) { |
| 178 | + return; |
| 179 | + } |
| 180 | + |
| 181 | + $this->profiler->endProfile($requestId, $statusCode); |
| 182 | + } |
| 183 | +} |
0 commit comments