schneespur/release/schneespur-1.0.2/app/Services/Extension/DashboardWidgetRegistry.php
Michael 7288b93500 Release v1.0.2: diagnostic infrastructure core
Add neutral diagnostic framework for future reporting modules:
- DiagnosticReporterInterface, Registry, Manager, PayloadSanitizer
- Laravel exception hook in bootstrap/app.php
- Module permission declarations (requires_permissions in module.json)
- Core diagnostic report points (module boot/install/update failures)
- Module documentation update (moduldoku.md)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-05-18 16:54:11 +00:00

76 lines
2.4 KiB
PHP

<?php
namespace App\Services\Extension;
use Illuminate\Support\Facades\Log;
class DashboardWidgetRegistry extends ExtensionRegistry
{
public function registerWidget(string $slug, array $config): void
{
$config = array_merge([
'slug' => $slug,
'label' => $slug,
'view' => null,
'dataCallback' => null,
'order' => 100,
'permission' => null,
'condition' => null,
'size' => 'full',
], $config, ['slug' => $slug]);
$this->register($slug, $config);
}
/**
* @return array<int, array{slug: string, label: string, view: string|null, data: mixed, order: int, permission: string|null, size: string, error: bool}>
*/
public function getWidgets(?string $userPermission = null): array
{
$widgets = [];
foreach ($this->items as $config) {
if ($config['permission'] !== null && $userPermission !== null && $config['permission'] !== $userPermission) {
continue;
}
if ($config['condition'] !== null) {
try {
if (! ($config['condition'])()) {
continue;
}
} catch (\Throwable $e) {
Log::warning("DashboardWidgetRegistry: condition callback failed for '{$config['slug']}': {$e->getMessage()}");
continue;
}
}
$data = null;
$error = false;
if ($config['dataCallback'] !== null) {
try {
$data = ($config['dataCallback'])();
} catch (\Throwable $e) {
Log::warning("DashboardWidgetRegistry: data callback exception for '{$config['slug']}': {$e->getMessage()}");
$error = true;
}
}
$widgets[] = [
'slug' => $config['slug'],
'label' => $config['label'],
'view' => $config['view'],
'data' => $data,
'order' => $config['order'],
'permission' => $config['permission'],
'size' => $config['size'],
'error' => $error,
];
}
usort($widgets, fn (array $a, array $b) => $a['order'] <=> $b['order']);
return $widgets;
}
}