1313use Yiisoft \Yii \Debug \StartupPolicy \Debugger \DebuggerStartupPolicyInterface ;
1414use Yiisoft \Yii \Debug \Storage \StorageInterface ;
1515
16+ /**
17+ * Debugger collects data from collectors and stores it in a storage.
18+ */
1619final class Debugger
1720{
1821 /**
22+ * @var CollectorInterface[] Collectors, indexed by their names.
23+ *
1924 * @psalm-var array<string, CollectorInterface>
2025 */
2126 private readonly array $ collectors ;
22- private readonly DataNormalizer $ dataNormalizer ;
2327
2428 /**
25- * @var string|null ID of the current request. Null if debugger is not active .
29+ * @var DataNormalizer Data normalizer that prepares data for storage .
2630 */
27- private ? string $ id = null ;
31+ private readonly DataNormalizer $ dataNormalizer ;
2832
2933 /**
30- * @param CollectorInterface[] $collectors
34+ * @param StorageInterface $storage The storage to store collected data.
35+ * @param CollectorInterface[] $collectors Collectors to be used.
36+ * @param DebuggerStartupPolicyInterface $debuggerStartupPolicy Policy to decide whether debugger should be started.
37+ * Default {@see AlwaysOnDebuggerPolicy} that always allows to startup debugger.
38+ * @param CollectorStartupPolicyInterface $collectorStartupPolicy Policy to decide whether collector should be
39+ * started. Default {@see AllowAllCollectorPolicy} that always allows to use all collectors.
40+ * @param array $excludedClasses List of classes to be excluded from collected data before storing.
3141 */
3242 public function __construct (
3343 private readonly StorageInterface $ storage ,
@@ -44,20 +54,42 @@ public function __construct(
4454
4555 $ this ->dataNormalizer = new DataNormalizer ($ excludedClasses );
4656
47- register_shutdown_function ([$ this , 'shutdown ' ]);
57+ register_shutdown_function ([$ this , 'stop ' ]);
4858 }
4959
60+ /**
61+ * @var string|null ID of the current request. `null` if debugger is not active.
62+ */
63+ private ?string $ id = null ;
64+
65+ /**
66+ * Returns whether debugger is active.
67+ *
68+ * @return bool Whether debugger is active.
69+ */
5070 public function isActive (): bool
5171 {
5272 return $ this ->id !== null ;
5373 }
5474
75+ /**
76+ * Returns ID of the current request.
77+ *
78+ * Throws `LogicException` if debugger is not started. Use {@see isActive()} to check if debugger is active.
79+ *
80+ * @return string ID of the current request.
81+ */
5582 public function getId (): string
5683 {
5784 return $ this ->id ?? throw new LogicException ('Debugger is not started. ' );
5885 }
5986
60- public function startup (object $ event ): void
87+ /**
88+ * Starts debugger and collectors.
89+ *
90+ * @param object $event Event that triggered debugger startup.
91+ */
92+ public function start (object $ event ): void
6193 {
6294 if (!$ this ->debuggerStartupPolicy ->satisfies ($ event )) {
6395 return ;
@@ -72,39 +104,58 @@ public function startup(object $event): void
72104 }
73105 }
74106
75- public function shutdown (): void
107+ /**
108+ * Stops the debugger for listening. Collected data will be flushed to storage.
109+ */
110+ public function stop (): void
76111 {
77112 if (!$ this ->isActive ()) {
78113 return ;
79114 }
80115
81116 try {
82- $ collectedData = array_map (
83- static fn (CollectorInterface $ collector ) => $ collector ->getCollected (),
84- $ this ->collectors
85- );
86-
87- /** @var array[] $data */
88- [$ data , $ objectsMap ] = $ this ->dataNormalizer ->prepareDataAndObjectsMap ($ collectedData , 30 );
89-
90- /** @var array $summary */
91- $ summary = $ this ->dataNormalizer ->prepareData ($ this ->collectSummaryData (), 30 );
92-
93- $ this ->storage ->write ($ this ->getId (), $ data , $ objectsMap , $ summary );
117+ $ this ->flush ();
94118 } finally {
95- foreach ($ this ->collectors as $ collector ) {
96- $ collector ->shutdown ();
97- }
98- $ this ->id = null ;
119+ $ this ->deactivate ();
99120 }
100121 }
101122
102- public function stop (): void
123+ /**
124+ * Stops the debugger from listening. Collected data will not be flushed to storage.
125+ */
126+ public function kill (): void
103127 {
104128 if (!$ this ->isActive ()) {
105129 return ;
106130 }
107131
132+ $ this ->deactivate ();
133+ }
134+
135+ /**
136+ * Collects data from collectors and stores it in a storage.
137+ */
138+ private function flush (): void
139+ {
140+ $ collectedData = array_map (
141+ static fn (CollectorInterface $ collector ) => $ collector ->getCollected (),
142+ $ this ->collectors
143+ );
144+
145+ /** @var array[] $data */
146+ [$ data , $ objectsMap ] = $ this ->dataNormalizer ->prepareDataAndObjectsMap ($ collectedData , 30 );
147+
148+ /** @var array $summary */
149+ $ summary = $ this ->dataNormalizer ->prepareData ($ this ->collectSummaryData (), 30 );
150+
151+ $ this ->storage ->write ($ this ->getId (), $ data , $ objectsMap , $ summary );
152+ }
153+
154+ /**
155+ * Stops debugger and collectors.
156+ */
157+ private function deactivate (): void
158+ {
108159 foreach ($ this ->collectors as $ collector ) {
109160 $ collector ->shutdown ();
110161 }
0 commit comments