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