|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Facade\Ignition\SolutionProviders; |
|
4
|
|
|
|
|
5
|
|
|
use Facade\Ignition\Exceptions\ViewException; |
|
6
|
|
|
use Facade\Ignition\Support\StringComparator; |
|
7
|
|
|
use Facade\IgnitionContracts\BaseSolution; |
|
8
|
|
|
use Facade\IgnitionContracts\HasSolutionsForThrowable; |
|
9
|
|
|
use Illuminate\Support\Arr; |
|
10
|
|
|
use Illuminate\Support\Facades\View; |
|
11
|
|
|
use InvalidArgumentException; |
|
12
|
|
|
use Symfony\Component\Finder\Finder; |
|
13
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
|
14
|
|
|
use Throwable; |
|
15
|
|
|
|
|
16
|
|
|
class ViewNotFoundSolutionProvider implements HasSolutionsForThrowable |
|
17
|
|
|
{ |
|
18
|
|
|
protected const REGEX = '/View \[(.*)\] not found/m'; |
|
19
|
|
|
|
|
20
|
|
|
public function canSolve(Throwable $throwable): bool |
|
21
|
|
|
{ |
|
22
|
|
|
if (! $throwable instanceof InvalidArgumentException && ! $throwable instanceof ViewException) { |
|
23
|
|
|
return false; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
return preg_match(self::REGEX, $throwable->getMessage(), $matches); |
|
|
|
|
|
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function getSolutions(Throwable $throwable): array |
|
30
|
|
|
{ |
|
31
|
|
|
preg_match(self::REGEX, $throwable->getMessage(), $matches); |
|
32
|
|
|
|
|
33
|
|
|
$missingView = $matches[1] ?? null; |
|
34
|
|
|
|
|
35
|
|
|
$suggestedView = $this->findRelatedView($missingView); |
|
36
|
|
|
|
|
37
|
|
|
if ($suggestedView) { |
|
38
|
|
|
return [ |
|
39
|
|
|
BaseSolution::create("{$missingView} was not found.") |
|
40
|
|
|
->setSolutionDescription("Did you mean `{$suggestedView}`?"), |
|
41
|
|
|
]; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return [ |
|
45
|
|
|
BaseSolution::create("{$missingView} was not found.") |
|
46
|
|
|
->setSolutionDescription('Are you sure the view exists and is a `.blade.php` file?'), |
|
47
|
|
|
]; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
protected function findRelatedView(string $missingView): ?string |
|
51
|
|
|
{ |
|
52
|
|
|
$views = $this->getAllViews(); |
|
53
|
|
|
|
|
54
|
|
|
return StringComparator::findClosestMatch($views, $missingView); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
protected function getAllViews(): array |
|
58
|
|
|
{ |
|
59
|
|
|
/** @var \Illuminate\View\FileViewFinder $fileViewFinder */ |
|
60
|
|
|
$fileViewFinder = View::getFinder(); |
|
61
|
|
|
|
|
62
|
|
|
$extensions = $fileViewFinder->getExtensions(); |
|
63
|
|
|
|
|
64
|
|
|
$viewsForHints = collect($fileViewFinder->getHints()) |
|
65
|
|
|
->flatMap(function ($paths, string $namespace) use ($extensions) { |
|
66
|
|
|
$paths = Arr::wrap($paths); |
|
67
|
|
|
|
|
68
|
|
|
return collect($paths) |
|
69
|
|
|
->flatMap(function (string $path) use ($extensions) { |
|
70
|
|
|
return $this->getViewsInPath($path, $extensions); |
|
71
|
|
|
}) |
|
72
|
|
|
->map(function (string $view) use ($namespace) { |
|
73
|
|
|
return "{$namespace}::{$view}"; |
|
74
|
|
|
}) |
|
75
|
|
|
->toArray(); |
|
76
|
|
|
}); |
|
77
|
|
|
|
|
78
|
|
|
$viewsForViewPaths = collect($fileViewFinder->getPaths()) |
|
79
|
|
|
->flatMap(function (string $path) use ($extensions) { |
|
80
|
|
|
return $this->getViewsInPath($path, $extensions); |
|
81
|
|
|
}); |
|
82
|
|
|
|
|
83
|
|
|
return $viewsForHints->merge($viewsForViewPaths)->toArray(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
protected function getViewsInPath(string $path, array $extensions): array |
|
87
|
|
|
{ |
|
88
|
|
|
$filePatterns = array_map(function (string $extension) { |
|
89
|
|
|
return "*.{$extension}"; |
|
90
|
|
|
}, $extensions); |
|
91
|
|
|
|
|
92
|
|
|
$extensionsWithDots = array_map(function (string $extension) { |
|
93
|
|
|
return ".{$extension}"; |
|
94
|
|
|
}, $extensions); |
|
95
|
|
|
|
|
96
|
|
|
$files = (new Finder()) |
|
97
|
|
|
->in($path) |
|
98
|
|
|
->files(); |
|
99
|
|
|
|
|
100
|
|
|
foreach ($filePatterns as $filePattern) { |
|
101
|
|
|
$files->name($filePattern); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$views = []; |
|
105
|
|
|
|
|
106
|
|
|
foreach ($files as $file) { |
|
107
|
|
|
if ($file instanceof SplFileInfo) { |
|
108
|
|
|
$view = $file->getRelativePathname(); |
|
109
|
|
|
$view = str_replace($extensionsWithDots, '', $view); |
|
110
|
|
|
$view = str_replace('/', '.', $view); |
|
111
|
|
|
$views[] = $view; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return $views; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: