1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Debug; |
6
|
|
|
|
7
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
8
|
|
|
use Yiisoft\Strings\WildcardPattern; |
9
|
|
|
use Yiisoft\Yii\Console\Event\ApplicationStartup; |
10
|
|
|
use Yiisoft\Yii\Debug\Collector\CollectorInterface; |
11
|
|
|
use Yiisoft\Yii\Debug\Storage\StorageInterface; |
12
|
|
|
use Yiisoft\Yii\Http\Event\BeforeRequest; |
13
|
|
|
|
14
|
|
|
final class Debugger |
15
|
|
|
{ |
16
|
|
|
private bool $skipCollect = false; |
17
|
|
|
|
18
|
6 |
|
public function __construct( |
19
|
|
|
private DebuggerIdGenerator $idGenerator, |
20
|
|
|
private StorageInterface $target, |
21
|
|
|
/** |
22
|
|
|
* @var CollectorInterface[] |
23
|
|
|
*/ |
24
|
|
|
private array $collectors, |
25
|
|
|
private array $optionalRequests = [], |
26
|
|
|
private array $optionalCommands = [], |
27
|
|
|
) { |
28
|
|
|
} |
29
|
|
|
|
30
|
1 |
|
public function getId(): string |
31
|
|
|
{ |
32
|
1 |
|
return $this->idGenerator->getId(); |
33
|
|
|
} |
34
|
|
|
|
35
|
3 |
|
public function startup(object $event): void |
36
|
|
|
{ |
37
|
3 |
|
if ($event instanceof BeforeRequest && $this->isOptionalRequest($event->getRequest())) { |
38
|
1 |
|
$this->skipCollect = true; |
39
|
1 |
|
return; |
40
|
|
|
} |
41
|
|
|
|
42
|
2 |
|
if ($event instanceof ApplicationStartup && $this->isOptionalCommand($event->arguments)) { |
|
|
|
|
43
|
|
|
$this->skipCollect = true; |
44
|
|
|
return; |
45
|
|
|
} |
46
|
|
|
|
47
|
2 |
|
$this->idGenerator->reset(); |
48
|
2 |
|
foreach ($this->collectors as $collector) { |
49
|
2 |
|
$this->target->addCollector($collector); |
50
|
2 |
|
$collector->startup(); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
2 |
|
private function isOptionalRequest(ServerRequestInterface $request): bool |
55
|
|
|
{ |
56
|
2 |
|
$path = $request->getUri()->getPath(); |
57
|
2 |
|
foreach ($this->optionalRequests as $pattern) { |
58
|
2 |
|
if ((new WildcardPattern($pattern))->match($path)) { |
59
|
1 |
|
return true; |
60
|
|
|
} |
61
|
|
|
} |
62
|
1 |
|
return false; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private function isOptionalCommand(array $arguments): bool |
66
|
|
|
{ |
67
|
|
|
if ($arguments === []) { |
68
|
|
|
return true; |
69
|
|
|
} |
70
|
|
|
$command = $arguments[0]; |
71
|
|
|
|
72
|
|
|
foreach ($this->optionalCommands as $pattern) { |
73
|
|
|
if ((new WildcardPattern($pattern))->match($command)) { |
74
|
|
|
return true; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
return false; |
78
|
|
|
} |
79
|
|
|
|
80
|
2 |
|
public function shutdown(): void |
81
|
|
|
{ |
82
|
|
|
try { |
83
|
2 |
|
if (!$this->skipCollect) { |
84
|
1 |
|
$this->target->flush(); |
85
|
|
|
} |
86
|
2 |
|
} finally { |
87
|
2 |
|
foreach ($this->collectors as $collector) { |
88
|
2 |
|
$collector->shutdown(); |
89
|
|
|
} |
90
|
2 |
|
$this->skipCollect = false; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param array $optionalRequests Patterns for optional request URLs. |
96
|
|
|
* |
97
|
|
|
* @see WildcardPattern |
98
|
|
|
* |
99
|
|
|
* @return self |
100
|
|
|
*/ |
101
|
1 |
|
public function withOptionalRequests(array $optionalRequests): self |
102
|
|
|
{ |
103
|
1 |
|
$new = clone $this; |
104
|
1 |
|
$new->optionalRequests = $optionalRequests; |
105
|
1 |
|
return $new; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|