| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Spatie\Ignition\Solutions\SolutionProviders;
|
|
|
4 |
|
|
|
5 |
use Illuminate\Support\Collection;
|
|
|
6 |
use Spatie\Ignition\Contracts\HasSolutionsForThrowable;
|
|
|
7 |
use Spatie\Ignition\Contracts\ProvidesSolution;
|
|
|
8 |
use Spatie\Ignition\Contracts\Solution;
|
|
|
9 |
use Spatie\Ignition\Contracts\SolutionProviderRepository as SolutionProviderRepositoryContract;
|
|
|
10 |
use Throwable;
|
|
|
11 |
|
|
|
12 |
class SolutionProviderRepository implements SolutionProviderRepositoryContract
|
|
|
13 |
{
|
|
|
14 |
/** @var Collection<int, class-string<HasSolutionsForThrowable>|HasSolutionsForThrowable> */
|
|
|
15 |
protected Collection $solutionProviders;
|
|
|
16 |
|
|
|
17 |
/** @param array<int, class-string<HasSolutionsForThrowable>|HasSolutionsForThrowable> $solutionProviders */
|
|
|
18 |
public function __construct(array $solutionProviders = [])
|
|
|
19 |
{
|
|
|
20 |
$this->solutionProviders = Collection::make($solutionProviders);
|
|
|
21 |
}
|
|
|
22 |
|
|
|
23 |
public function registerSolutionProvider(string|HasSolutionsForThrowable $solutionProvider): SolutionProviderRepositoryContract
|
|
|
24 |
{
|
|
|
25 |
$this->solutionProviders->push($solutionProvider);
|
|
|
26 |
|
|
|
27 |
return $this;
|
|
|
28 |
}
|
|
|
29 |
|
|
|
30 |
public function registerSolutionProviders(array $solutionProviderClasses): SolutionProviderRepositoryContract
|
|
|
31 |
{
|
|
|
32 |
$this->solutionProviders = $this->solutionProviders->merge($solutionProviderClasses);
|
|
|
33 |
|
|
|
34 |
return $this;
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
public function getSolutionsForThrowable(Throwable $throwable): array
|
|
|
38 |
{
|
|
|
39 |
$solutions = [];
|
|
|
40 |
|
|
|
41 |
if ($throwable instanceof Solution) {
|
|
|
42 |
$solutions[] = $throwable;
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
if ($throwable instanceof ProvidesSolution) {
|
|
|
46 |
$solutions[] = $throwable->getSolution();
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
$providedSolutions = $this
|
|
|
50 |
->initialiseSolutionProviderRepositories()
|
|
|
51 |
->filter(function (HasSolutionsForThrowable $solutionProvider) use ($throwable) {
|
|
|
52 |
try {
|
|
|
53 |
return $solutionProvider->canSolve($throwable);
|
|
|
54 |
} catch (Throwable $exception) {
|
|
|
55 |
return false;
|
|
|
56 |
}
|
|
|
57 |
})
|
|
|
58 |
->map(function (HasSolutionsForThrowable $solutionProvider) use ($throwable) {
|
|
|
59 |
try {
|
|
|
60 |
return $solutionProvider->getSolutions($throwable);
|
|
|
61 |
} catch (Throwable $exception) {
|
|
|
62 |
return [];
|
|
|
63 |
}
|
|
|
64 |
})
|
|
|
65 |
->flatten()
|
|
|
66 |
->toArray();
|
|
|
67 |
|
|
|
68 |
return array_merge($solutions, $providedSolutions);
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
public function getSolutionForClass(string $solutionClass): ?Solution
|
|
|
72 |
{
|
|
|
73 |
if (! class_exists($solutionClass)) {
|
|
|
74 |
return null;
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
if (! in_array(Solution::class, class_implements($solutionClass) ?: [])) {
|
|
|
78 |
return null;
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
if (! function_exists('app')) {
|
|
|
82 |
return null;
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
return app($solutionClass);
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
/** @return Collection<int, HasSolutionsForThrowable> */
|
|
|
89 |
protected function initialiseSolutionProviderRepositories(): Collection
|
|
|
90 |
{
|
|
|
91 |
return $this->solutionProviders
|
|
|
92 |
->filter(fn (HasSolutionsForThrowable|string $provider) => in_array(HasSolutionsForThrowable::class, class_implements($provider) ?: []))
|
|
|
93 |
->map(function (string|HasSolutionsForThrowable $provider): HasSolutionsForThrowable {
|
|
|
94 |
if (is_string($provider)) {
|
|
|
95 |
return new $provider;
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
return $provider;
|
|
|
99 |
});
|
|
|
100 |
}
|
|
|
101 |
}
|