MissingPackageSolutionProvider.php
1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
namespace Facade\Ignition\SolutionProviders;
use Facade\Ignition\Solutions\MissingPackageSolution;
use Facade\Ignition\Support\Packagist\Package;
use Facade\Ignition\Support\Packagist\Packagist;
use Facade\IgnitionContracts\HasSolutionsForThrowable;
use Illuminate\Support\Str;
use Throwable;
class MissingPackageSolutionProvider implements HasSolutionsForThrowable
{
/** @var \Facade\Ignition\Support\Packagist\Package|null */
protected $package;
public function canSolve(Throwable $throwable): bool
{
$pattern = '/Class \'([^\s]+)\' not found/m';
if (! preg_match($pattern, $throwable->getMessage(), $matches)) {
return false;
}
$class = $matches[1];
if (Str::startsWith($class, app()->getNamespace())) {
return false;
}
$this->package = $this->findPackageFromClassName($class);
return ! is_null($this->package);
}
public function getSolutions(Throwable $throwable): array
{
return [new MissingPackageSolution($this->package)];
}
protected function findPackageFromClassName(string $missingClassName): ?Package
{
if (! $package = $this->findComposerPackageForClassName($missingClassName)) {
return null;
}
return $package->hasNamespaceThatContainsClassName($missingClassName)
? $package
: null;
}
protected function findComposerPackageForClassName(string $className): ?Package
{
$packages = Packagist::findPackagesForClassName($className);
return $packages[0] ?? null;
}
}