|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Facade\Ignition\SolutionProviders; |
|
4
|
|
|
|
|
5
|
|
|
use Throwable; |
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
use Illuminate\Support\Facades\Route; |
|
8
|
|
|
use Facade\IgnitionContracts\BaseSolution; |
|
9
|
|
|
use Facade\Ignition\Support\StringComparator; |
|
10
|
|
|
use Facade\Ignition\Exceptions\ViewException; |
|
11
|
|
|
use Facade\IgnitionContracts\HasSolutionsForThrowable; |
|
12
|
|
|
|
|
13
|
|
|
class RouteNotDefinedSolutionProvider implements HasSolutionsForThrowable |
|
14
|
|
|
{ |
|
15
|
|
|
protected const REGEX = '/Route \[(.*)\] not defined/m'; |
|
16
|
|
|
|
|
17
|
|
|
public function canSolve(Throwable $throwable): bool |
|
18
|
|
|
{ |
|
19
|
|
|
if (! $throwable instanceof InvalidArgumentException && ! $throwable instanceof ViewException) { |
|
20
|
|
|
return false; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
return preg_match(self::REGEX, $throwable->getMessage(), $matches); |
|
|
|
|
|
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function getSolutions(Throwable $throwable): array |
|
27
|
|
|
{ |
|
28
|
|
|
preg_match(self::REGEX, $throwable->getMessage(), $matches); |
|
29
|
|
|
|
|
30
|
|
|
$missingRoute = $matches[1] ?? null; |
|
31
|
|
|
|
|
32
|
|
|
$suggestedRoute = $this->findRelatedRoute($missingRoute); |
|
33
|
|
|
|
|
34
|
|
|
if ($suggestedRoute) { |
|
35
|
|
|
return [ |
|
36
|
|
|
BaseSolution::create("{$missingRoute} was not defined.") |
|
37
|
|
|
->setSolutionDescription("Did you mean `{$suggestedRoute}`?"), |
|
38
|
|
|
]; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
return [ |
|
42
|
|
|
BaseSolution::create("{$missingRoute} was not defined.") |
|
43
|
|
|
->setSolutionDescription('Are you sure that the route is defined'), |
|
44
|
|
|
]; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
protected function findRelatedRoute(string $missingRoute): ?string |
|
48
|
|
|
{ |
|
49
|
|
|
Route::getRoutes()->refreshNameLookups(); |
|
50
|
|
|
|
|
51
|
|
|
return StringComparator::findClosestMatch(array_keys(Route::getRoutes()->getRoutesByName()), $missingRoute); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
|
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: