|
5 | 5 | namespace Bag\DebugBar\Collectors; |
6 | 6 |
|
7 | 7 | use Bag\Bag; |
| 8 | +use DebugBar\DataCollector\DataCollectorInterface; |
8 | 9 | use DebugBar\DataCollector\MessagesCollector; |
9 | 10 |
|
10 | | -class BagCollector extends MessagesCollector |
11 | | -{ |
12 | | - /** |
13 | | - * @var array<string, array{bag: Bag, location: array{file: string|null, line: int|null}|false, type: string}> |
14 | | - */ |
15 | | - protected static array $bags = []; |
16 | | - |
17 | | - public static function add(Bag $bag): void |
| 11 | +if (!class_exists(MessagesCollector::class)) { |
| 12 | + class BagCollector |
18 | 13 | { |
19 | | - $location = false; |
20 | | - $trace = collect(\debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS)) |
21 | | - ->firstWhere(fn ($v) => |
22 | | - isset($v['file']) && |
23 | | - !str_contains($v['file'], '/vendor/') && |
24 | | - !\str_starts_with($v['function'], '__')); |
25 | | - |
26 | | - $type = 'unknown'; |
27 | | - if ($trace !== null) { |
28 | | - $location = ['file' => $trace['file'] ?? null, 'line' => $trace['line'] ?? null]; |
29 | | - $type = $trace['function']; |
| 14 | + public static function add(Bag $bag): void |
| 15 | + { |
| 16 | + // Do nothing |
30 | 17 | } |
31 | 18 |
|
32 | | - self::$bags[$bag::class . '#' . \spl_object_id($bag)] = ['bag' => $bag, 'location' => $location, 'type' => $type]; |
| 19 | + public static function init(): void |
| 20 | + { |
| 21 | + // Do nothing |
| 22 | + } |
33 | 23 | } |
34 | | - |
35 | | - /** |
36 | | - * @return array{messages: array<mixed>, count: int} |
37 | | - */ |
38 | | - public function collect(): array |
| 24 | +} else { |
| 25 | + class BagCollector extends MessagesCollector implements DataCollectorInterface |
39 | 26 | { |
40 | | - $localPath = config('debugbar.local_sites_path') ?: base_path(); |
41 | | - $remoteSitesPath = config('debugbar.remote_sites_path'); |
42 | | - $remotePaths = array_filter(explode(',', is_string($remoteSitesPath) ? $remoteSitesPath : '')) ?: [base_path()]; |
| 27 | + /** |
| 28 | + * @var array<string, array{bag: Bag, location: array{file: string|null, line: int|null}|false, type: string}> |
| 29 | + */ |
| 30 | + protected static array $bags = []; |
| 31 | + |
| 32 | + public static function add(Bag $bag): void |
| 33 | + { |
| 34 | + $location = false; |
| 35 | + $trace = collect(\debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS)) |
| 36 | + ->firstWhere(fn ($v) => isset($v['file']) && |
| 37 | + !str_contains($v['file'], '/vendor/') && |
| 38 | + !\str_starts_with($v['function'], '__')); |
43 | 39 |
|
44 | | - $this->setXdebugReplacements(array_fill_keys($remotePaths, $localPath)); |
45 | | - $editor = \config('debugbar.editor'); |
46 | | - if (is_string($editor)) { |
47 | | - $this->setEditorLinkTemplate($editor); |
| 40 | + $type = 'unknown'; |
| 41 | + if ($trace !== null) { |
| 42 | + $location = ['file' => $trace['file'] ?? null, 'line' => $trace['line'] ?? null]; |
| 43 | + $type = $trace['function']; |
| 44 | + } |
| 45 | + |
| 46 | + self::$bags[$bag::class . '#' . \spl_object_id($bag)] = ['bag' => $bag, 'location' => $location, 'type' => $type]; |
48 | 47 | } |
49 | 48 |
|
50 | 49 | /** |
51 | | - * @var array{bag: Bag, location: array{file: string, line: int|null}, type: string} $message |
| 50 | + * @return array{messages: array<mixed>, count: int} |
52 | 51 | */ |
53 | | - foreach (self::$bags as $message) { |
54 | | - $this->addMessage($message['bag'], $message['type']); |
55 | | - $xdebugLink = $this->getXdebugLink($message['location']['file'], $message['location']['line'] ?? null); |
56 | | - $this->messages[\array_key_last($this->messages)]['xdebug_link'] = $xdebugLink; |
57 | | - } |
| 52 | + public function collect(): array |
| 53 | + { |
| 54 | + $localPath = config('debugbar.local_sites_path') ?: base_path(); |
| 55 | + $remoteSitesPath = config('debugbar.remote_sites_path'); |
| 56 | + $remotePaths = array_filter(explode(',', is_string($remoteSitesPath) ? $remoteSitesPath : '')) ?: [base_path()]; |
58 | 57 |
|
59 | | - // @phpstan-ignore return.type |
60 | | - return parent::collect(); |
61 | | - } |
| 58 | + $this->setXdebugReplacements(array_fill_keys($remotePaths, $localPath)); |
| 59 | + $editor = \config('debugbar.editor'); |
| 60 | + if (is_string($editor)) { |
| 61 | + $this->setEditorLinkTemplate($editor); |
| 62 | + } |
62 | 63 |
|
63 | | - public function getName(): string |
64 | | - { |
65 | | - return 'bags'; |
66 | | - } |
| 64 | + /** |
| 65 | + * @var array{bag: Bag, location: array{file: string, line: int|null}, type: string} $message |
| 66 | + */ |
| 67 | + foreach (self::$bags as $message) { |
| 68 | + $this->addMessage($message['bag'], $message['type']); |
| 69 | + $xdebugLink = $this->getXdebugLink($message['location']['file'], $message['location']['line'] ?? null); |
| 70 | + $this->messages[\array_key_last($this->messages)]['xdebug_link'] = $xdebugLink; |
| 71 | + } |
67 | 72 |
|
68 | | - public static function init(): void |
69 | | - { |
70 | | - self::$bags = []; |
| 73 | + return parent::collect(); |
| 74 | + } |
| 75 | + |
| 76 | + public function getName(): string |
| 77 | + { |
| 78 | + return 'bags'; |
| 79 | + } |
| 80 | + |
| 81 | + public static function init(): void |
| 82 | + { |
| 83 | + self::$bags = []; |
| 84 | + } |
71 | 85 | } |
72 | 86 | } |
0 commit comments