我需要在 Laravel 文件中使用
ResultStore
。当我尝试初始化它时,出现以下错误:
无法实例化接口 Spatie\Health\ResultStores\ResultStore
我错过了什么?
/**
* Get health status
*/
public function getHealthStatus(): array
{
$schema = [
'label' => 'Healthy',
'state' => 'healthy',
'textClass' => 'white',
'bgClass' => 'success'
];
try {
$latestResults = (new ResultStore())->latestResults();
// health checks not found
if (! $latestResults) {
$schema['label'] = 'Progressing';
$schema['state'] = 'progressing';
$schema['textClass'] = 'white';
$schema['bgClass'] = 'info';
return $schema;
}
$failedChecks = collect($latestResults->storedCheckResults)->filter(function ($item) {
return $item->status == 'failed';
})->count();
$warnChecks = collect($latestResults->storedCheckResults)->filter(function ($item) {
return $item->status == 'warning';
})->count();
if ($warnChecks > 0) {
$schema['label'] = 'Degraded';
$schema['state'] = 'degraded';
$schema['textClass'] = 'dark';
$schema['bgClass'] = 'warning';
}
if ($failedChecks > 0) {
$schema['label'] = 'Critical';
$schema['state'] = 'critical';
$schema['textClass'] = 'white';
$schema['bgClass'] = 'danger';
}
} catch (Exception $e) {
// ...
}
return $schema;
}
这个函数在我的项目中进一步调用:
/**
* Get app content
*/
public function app(): array
{
$stats = [];
$stats['health_status'] = $this->getHealthStatus();
return $stats;
}
您正在尝试实例化一个接口,并且错误向您表明了这一点。 相反,您应该要求提供者为您获取绑定到该接口的具体类。 请更换:
$latestResults = (new ResultStore())->latestResults();
与:
$latestResults = app(ResultStore::class)->latestResults();