Skip to content

Commit 85a5793

Browse files
committed
feat(debug): add debug configuration and middleware for plugin system
Add comprehensive debug configuration options including profiling, logging, and caching settings. Implement PluginDebugMiddleware to handle debug routes, sanitize sensitive data, and log debug information. Update PluginDebugCommand to collect and display debug data for plugins.
1 parent 6cdf1d8 commit 85a5793

File tree

3 files changed

+108
-23
lines changed

3 files changed

+108
-23
lines changed

config/laravel-plugin-system.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,85 @@
176176
'ttl' => env('PLUGIN_HEALTH_CACHE_TTL', 3600), // 1 hour
177177
],
178178
],
179+
180+
/*
181+
|--------------------------------------------------------------------------
182+
| Debug Configuration
183+
|--------------------------------------------------------------------------
184+
|
185+
| Configuration for plugin debugging and profiling tools.
186+
|
187+
*/
188+
'debug' => [
189+
/*
190+
|--------------------------------------------------------------------------
191+
| Enable Debug Mode
192+
|--------------------------------------------------------------------------
193+
|
194+
| Whether to enable plugin debugging features.
195+
| This should be disabled in production.
196+
|
197+
*/
198+
'enabled' => env('PLUGIN_DEBUG_ENABLED', env('APP_DEBUG', false)),
199+
200+
/*
201+
|--------------------------------------------------------------------------
202+
| Debug Routes
203+
|--------------------------------------------------------------------------
204+
|
205+
| Specific routes that should trigger debug middleware.
206+
| Leave empty to debug all routes when debug headers/params are present.
207+
|
208+
*/
209+
'routes' => [
210+
// 'plugin.example.index',
211+
// 'plugin.*.show',
212+
],
213+
214+
/*
215+
|--------------------------------------------------------------------------
216+
| Profiling Configuration
217+
|--------------------------------------------------------------------------
218+
|
219+
| Settings for plugin profiling and performance analysis.
220+
|
221+
*/
222+
'profiling' => [
223+
'enabled' => env('PLUGIN_PROFILING_ENABLED', true),
224+
'memory_tracking' => env('PLUGIN_PROFILING_MEMORY', true),
225+
'query_tracking' => env('PLUGIN_PROFILING_QUERIES', true),
226+
'slow_query_threshold' => env('PLUGIN_SLOW_QUERY_THRESHOLD', 100), // milliseconds
227+
],
228+
229+
/*
230+
|--------------------------------------------------------------------------
231+
| Logging Configuration
232+
|--------------------------------------------------------------------------
233+
|
234+
| Settings for debug logging.
235+
|
236+
*/
237+
'logging' => [
238+
'enabled' => env('PLUGIN_DEBUG_LOGGING', true),
239+
'log_channel' => env('PLUGIN_DEBUG_LOG_CHANNEL', 'single'),
240+
'log_requests' => env('PLUGIN_DEBUG_LOG_REQUESTS', true),
241+
'log_responses' => env('PLUGIN_DEBUG_LOG_RESPONSES', true),
242+
'sanitize_sensitive_data' => env('PLUGIN_DEBUG_SANITIZE', true),
243+
],
244+
245+
/*
246+
|--------------------------------------------------------------------------
247+
| Cache Configuration
248+
|--------------------------------------------------------------------------
249+
|
250+
| Settings for debug data caching.
251+
|
252+
*/
253+
'cache' => [
254+
'enabled' => env('PLUGIN_DEBUG_CACHE', true),
255+
'cache_ttl' => env('PLUGIN_DEBUG_CACHE_TTL', 3600), // 1 hour
256+
'max_history' => env('PLUGIN_DEBUG_MAX_HISTORY', 100),
257+
'max_entries' => env('PLUGIN_DEBUG_MAX_ENTRIES', 1000),
258+
],
259+
],
179260
];

src/Commands/PluginDebugCommand.php

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ protected function debugAllPlugins(): int
6767
$this->newLine();
6868

6969
$pluginsPath = config('laravel-plugin-system.plugins_path', app_path('Plugins'));
70-
70+
7171
if (!File::exists($pluginsPath)) {
7272
$this->error('Plugins directory not found!');
7373
return 1;
@@ -84,8 +84,11 @@ protected function debugAllPlugins(): int
8484

8585
foreach ($plugins as $plugin) {
8686
$this->line("Debugging: {$plugin}");
87+
8788
$debugData = $this->collectDebugData($plugin);
89+
8890
$this->displaySummaryResults($plugin, $debugData);
91+
8992
$this->newLine();
9093
}
9194

@@ -100,7 +103,7 @@ protected function handleWatchMode(string $pluginName = null): int
100103
try {
101104
while (true) {
102105
$this->line('[' . now()->format('H:i:s') . '] Collecting debug data...');
103-
106+
104107
if ($pluginName) {
105108
$debugData = $this->collectDebugData($pluginName);
106109
$this->displayWatchResults($pluginName, $debugData);
@@ -131,7 +134,7 @@ protected function collectDebugData(string $pluginName): array
131134

132135
try {
133136
$pluginData = $this->getPluginInformation($pluginName);
134-
137+
135138
if ($this->option('trace')) {
136139
$traceData = $this->collectTraceData($pluginName);
137140
$pluginData['trace'] = $traceData;
@@ -141,7 +144,7 @@ protected function collectDebugData(string $pluginName): array
141144
$queries = DB::getQueryLog();
142145
$queryCount = count($queries);
143146
$slowQueryThreshold = (int) $this->option('slow-queries');
144-
147+
145148
$slowQueries = collect($queries)
146149
->filter(fn($query) => $query['time'] > $slowQueryThreshold)
147150
->values()
@@ -208,7 +211,7 @@ protected function getPluginInformation(string $pluginName): array
208211
protected function collectTraceData(string $pluginName): array
209212
{
210213
$trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 10);
211-
214+
212215
return collect($trace)
213216
->map(function ($item) {
214217
return [
@@ -270,11 +273,11 @@ protected function getPluginServices(string $pluginName): array
270273
{
271274
$services = [];
272275
$namespace = config('laravel-plugin-system.plugin_namespace', 'App\\Plugins') . '\\' . $pluginName . '\\Services';
273-
276+
274277
try {
275278
$serviceInterface = $namespace . '\\' . $pluginName . 'ServiceInterface';
276279
$serviceClass = $namespace . '\\' . $pluginName . 'Service';
277-
280+
278281
if (app()->bound($serviceInterface)) {
279282
$services[] = [
280283
'interface' => $serviceInterface,
@@ -385,15 +388,15 @@ protected function displaySummaryResults(string $pluginName, array $debugData):
385388
$memory = $debugData['memory_usage']['peak'];
386389
$time = $debugData['execution_time'];
387390
$queries = $debugData['query_count'];
388-
391+
389392
$this->line(" {$status} {$pluginName} | {$time}ms | {$memory} | {$queries} queries");
390393
}
391394

392395
protected function displayWatchResults(string $pluginName, array $debugData): void
393396
{
394397
$status = isset($debugData['plugin_data']['error']) ? '❌ ERROR' : '✅ OK';
395398
$this->line("[{$pluginName}] {$status} | {$debugData['execution_time']}ms | {$debugData['memory_usage']['peak']} | {$debugData['query_count']} queries");
396-
399+
397400
if (isset($debugData['plugin_data']['error'])) {
398401
$this->error(" Error: {$debugData['plugin_data']['error']}");
399402
}
@@ -407,7 +410,7 @@ protected function pluginExists(string $pluginName): bool
407410

408411
protected function isPluginEnabled(string $pluginName): bool
409412
{
410-
return (bool) config("{$pluginName}.enabled", true);
413+
return (bool) config("{$pluginName}.enabled", false);
411414
}
412415

413416
protected function formatBytes(int $bytes): string
@@ -416,9 +419,9 @@ protected function formatBytes(int $bytes): string
416419
$bytes = max($bytes, 0);
417420
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
418421
$pow = min($pow, count($units) - 1);
419-
422+
420423
$bytes /= (1 << (10 * $pow));
421-
424+
422425
return round($bytes, 2) . ' ' . $units[$pow];
423426
}
424-
}
427+
}

src/Middleware/PluginDebugMiddleware.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ protected function shouldDebug(Request $request): bool
5454
}
5555

5656
$debugRoutes = config('laravel-plugin-system.debug.routes', []);
57+
5758
if (!empty($debugRoutes)) {
5859
$currentRoute = $request->route()?->getName();
5960
return in_array($currentRoute, $debugRoutes);
@@ -140,7 +141,7 @@ protected function finishDebugging(Request $request, $response, array $plugins):
140141
protected function sanitizeHeaders(array $headers): array
141142
{
142143
$sensitiveHeaders = ['authorization', 'cookie', 'x-api-key', 'x-auth-token'];
143-
144+
144145
foreach ($headers as $key => $value) {
145146
if (in_array(strtolower($key), $sensitiveHeaders)) {
146147
$headers[$key] = '[REDACTED]';
@@ -153,7 +154,7 @@ protected function sanitizeHeaders(array $headers): array
153154
protected function sanitizeInput(array $input): array
154155
{
155156
$sensitiveFields = ['password', 'password_confirmation', 'token', 'api_key', 'secret'];
156-
157+
157158
foreach ($input as $key => $value) {
158159
if (in_array(strtolower($key), $sensitiveFields)) {
159160
$input[$key] = '[REDACTED]';
@@ -182,7 +183,7 @@ protected function storeDebugData(): void
182183
{
183184
$cacheKey = 'plugin_debug_' . $this->debugData['request_id'];
184185
$ttl = config('laravel-plugin-system.debug.cache_ttl', 3600);
185-
186+
186187
Cache::put($cacheKey, $this->debugData, $ttl);
187188

188189
$this->storeDebugHistory();
@@ -192,7 +193,7 @@ protected function storeDebugHistory(): void
192193
{
193194
$historyKey = 'plugin_debug_history';
194195
$history = Cache::get($historyKey, []);
195-
196+
196197
$history[] = [
197198
'request_id' => $this->debugData['request_id'],
198199
'timestamp' => $this->debugData['timestamp'],
@@ -218,7 +219,7 @@ protected function addDebugHeaders($response): void
218219
$response->headers->set('X-Plugin-Debug-Execution-Time', $this->debugData['performance']['execution_time_ms'] . 'ms');
219220
$response->headers->set('X-Plugin-Debug-Memory-Usage', $this->debugData['performance']['memory_usage_mb'] . 'MB');
220221
$response->headers->set('X-Plugin-Debug-Query-Count', $this->debugData['performance']['query_count']);
221-
222+
222223
if (!empty($this->debugData['plugins'])) {
223224
$response->headers->set('X-Plugin-Debug-Plugins', implode(',', $this->debugData['plugins']));
224225
}
@@ -227,7 +228,7 @@ protected function addDebugHeaders($response): void
227228
protected function logDebugInfo(string $message, array $data): void
228229
{
229230
$logChannel = config('laravel-plugin-system.debug.log_channel', 'single');
230-
231+
231232
Log::channel($logChannel)->info($message, [
232233
'request_id' => $data['request_id'],
233234
'url' => $data['url'],
@@ -252,17 +253,17 @@ public static function getDebugHistory(int $limit = 50): array
252253
public static function clearDebugHistory(): void
253254
{
254255
Cache::forget('plugin_debug_history');
255-
256+
256257
try {
257258
$pluginsPath = config('laravel-plugin-system.plugins_path', app_path('Plugins'));
258259
$pluginDirectories = \Illuminate\Support\Facades\File::directories($pluginsPath);
259-
260+
260261
foreach ($pluginDirectories as $pluginDir) {
261262
$pluginName = basename($pluginDir);
262263
$cacheKey = 'plugin_debug_' . $pluginName;
263264
Cache::forget($cacheKey);
264265
}
265-
266+
266267
for ($i = 0; $i < 1000; $i++) {
267268
$cacheKey = 'plugin_debug_req_' . str_pad($i, 6, '0', STR_PAD_LEFT);
268269
if (!Cache::has($cacheKey)) {
@@ -274,4 +275,4 @@ public static function clearDebugHistory(): void
274275
Log::warning("Error clearing debug history: " . $e->getMessage());
275276
}
276277
}
277-
}
278+
}

0 commit comments

Comments
 (0)