|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Sentry\Laravel\Tracing; |
| 4 | + |
| 5 | +use Sentry\Frame; |
| 6 | +use Sentry\Options; |
| 7 | +use Sentry\FrameBuilder; |
| 8 | +use Illuminate\Support\Str; |
| 9 | +use Sentry\Serializer\RepresentationSerializerInterface; |
| 10 | + |
| 11 | +class BacktraceHelper |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @var Options The SDK client options |
| 15 | + */ |
| 16 | + private $options; |
| 17 | + |
| 18 | + /** |
| 19 | + * @var FrameBuilder An instance of the builder of {@see Frame} objects |
| 20 | + */ |
| 21 | + private $frameBuilder; |
| 22 | + |
| 23 | + /** |
| 24 | + * Constructor. |
| 25 | + * |
| 26 | + * @param Options $options The SDK client options |
| 27 | + * @param RepresentationSerializerInterface $representationSerializer The representation serializer |
| 28 | + */ |
| 29 | + public function __construct(Options $options, RepresentationSerializerInterface $representationSerializer) |
| 30 | + { |
| 31 | + $this->options = $options; |
| 32 | + $this->frameBuilder = new FrameBuilder($options, $representationSerializer); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Find the first in app frame for a given backtrace. |
| 37 | + * |
| 38 | + * @param array<int, array<string, mixed>> $backtrace The backtrace |
| 39 | + * |
| 40 | + * @phpstan-param list<array{ |
| 41 | + * line?: integer, |
| 42 | + * file?: string, |
| 43 | + * }> $backtrace |
| 44 | + */ |
| 45 | + public function findFirstInAppFrameForBacktrace(array $backtrace): ?Frame |
| 46 | + { |
| 47 | + $file = Frame::INTERNAL_FRAME_FILENAME; |
| 48 | + $line = 0; |
| 49 | + |
| 50 | + foreach ($backtrace as $backtraceFrame) { |
| 51 | + $frame = $this->frameBuilder->buildFromBacktraceFrame($file, $line, $backtraceFrame); |
| 52 | + |
| 53 | + if ($frame->isInApp()) { |
| 54 | + return $frame; |
| 55 | + } |
| 56 | + |
| 57 | + $file = $backtraceFrame['file'] ?? Frame::INTERNAL_FRAME_FILENAME; |
| 58 | + $line = $backtraceFrame['line'] ?? 0; |
| 59 | + } |
| 60 | + |
| 61 | + return null; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Takes a frame and if it's a compiled view path returns the original view path. |
| 66 | + * |
| 67 | + * @param \Sentry\Frame $frame |
| 68 | + * |
| 69 | + * @return string|null |
| 70 | + */ |
| 71 | + public function getOriginalViewPathForFrameOfCompiledViewPath(Frame $frame): ?string |
| 72 | + { |
| 73 | + // Check if we are dealing with a frame for a cached view path |
| 74 | + if (!Str::startsWith($frame->getFile(), '/storage/framework/views/')) { |
| 75 | + return null; |
| 76 | + } |
| 77 | + |
| 78 | + // If for some reason the file does not exists, skip resolving |
| 79 | + if (!file_exists($frame->getAbsoluteFilePath())) { |
| 80 | + return null; |
| 81 | + } |
| 82 | + |
| 83 | + $viewFileContents = file_get_contents($frame->getAbsoluteFilePath()); |
| 84 | + |
| 85 | + preg_match('/PATH (?<originalPath>.*?) ENDPATH/', $viewFileContents, $matches); |
| 86 | + |
| 87 | + // No path comment found in the file, must be a very old Laravel version |
| 88 | + if (empty($matches['originalPath'])) { |
| 89 | + return null; |
| 90 | + } |
| 91 | + |
| 92 | + return $this->stripPrefixFromFilePath($matches['originalPath']); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Removes from the given file path the specified prefixes. |
| 97 | + * |
| 98 | + * @param string $filePath The path to the file |
| 99 | + */ |
| 100 | + private function stripPrefixFromFilePath(string $filePath): string |
| 101 | + { |
| 102 | + foreach ($this->options->getPrefixes() as $prefix) { |
| 103 | + if (Str::startsWith($filePath, $prefix)) { |
| 104 | + return mb_substr($filePath, mb_strlen($prefix)); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return $filePath; |
| 109 | + } |
| 110 | +} |
0 commit comments