| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace BEAR\Resource\SemanticLog\Profile\Verbose; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use JsonSerializable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Koriym\SemanticLogger\AbstractContext; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Koriym\SemanticLogger\Profiler\PhpProfile; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Koriym\SemanticLogger\Profiler\Profile; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Koriym\SemanticLogger\Profiler\XdebugTrace; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Koriym\SemanticLogger\Profiler\XHProfResult; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Override; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Throwable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use function crc32; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use function dechex; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | final class ErrorContext extends AbstractContext implements JsonSerializable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** @psalm-suppress InvalidClassConstantType */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     public const TYPE = 'bear_resource_error'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** @psalm-suppress InvalidClassConstantType */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     public const SCHEMA_URL = 'https://bearsunday.github.io/BEAR.Resource/schemas/error-context.json'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     public readonly string $exceptionId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     public readonly string $exceptionAsString; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     public readonly Profile $profile; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         Throwable $exception, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         string $exceptionId = '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         OpenContext|null $openContext = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $this->exceptionAsString = (string) $exception; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $this->exceptionId = $exceptionId !== '' ? $exceptionId : $this->createExceptionId(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         if ($openContext === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             // Create empty profile if no open context provided | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             $this->profile = new Profile(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         // Stop profiling and capture final profile data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         // Note: start() was called in OpenContext, now we stop and capture data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $xhprofResult = (new XHProfResult())->stop($openContext->uri); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $xdebugTrace = (new XdebugTrace())->stop(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $phpProfile = PhpProfile::capture(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $this->profile = new Profile( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             xhprof: $xhprofResult, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             xdebug: $xdebugTrace, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             php: $phpProfile, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |     public static function create( | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         Throwable $exception, | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         string $exceptionId = '', | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         OpenContext|null $openContext = null, | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |     ): self { | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |         return new self($exception, $exceptionId, $openContext); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     private function createExceptionId(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $crc = crc32($this->exceptionAsString); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $crcHex = dechex($crc & 0xFFFFFFFF); // Ensure positive hex value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         return 'e-bear-resource-' . $crcHex; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     /** @return array<string, mixed> */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     #[Override] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     public function jsonSerialize(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             'exceptionId' => $this->exceptionId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             'exceptionAsString' => $this->exceptionAsString, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             'profile' => $this->profile, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 85 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 86 |  |  |  |