1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\MissingPageRedirector; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Routing\Router; |
7
|
|
|
use Spatie\MissingPageRedirector\Events\RedirectNotFound; |
8
|
|
|
use Spatie\MissingPageRedirector\Events\RouteWasHit; |
9
|
|
|
use Spatie\MissingPageRedirector\Redirector\Redirector; |
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
11
|
|
|
use Symfony\Component\HttpFoundation\Response; |
12
|
|
|
|
13
|
|
|
class MissingPageRouter |
14
|
|
|
{ |
15
|
|
|
/** @var \Illuminate\Routing\Router */ |
16
|
|
|
protected $router; |
17
|
|
|
|
18
|
|
|
/** @var \Spatie\MissingPageRedirector\Redirector\Redirector */ |
19
|
|
|
protected $redirector; |
20
|
|
|
|
21
|
|
|
public function __construct(Router $router, Redirector $redirector) |
22
|
|
|
{ |
23
|
|
|
$this->router = $router; |
24
|
|
|
$this->redirector = $redirector; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
29
|
|
|
* |
30
|
|
|
* @return \Illuminate\Http\Response|null |
31
|
|
|
*/ |
32
|
|
|
public function getRedirectFor(Request $request) |
33
|
|
|
{ |
34
|
|
|
$redirects = $this->redirector->getRedirectsFor($request); |
35
|
|
|
|
36
|
|
|
collect($redirects)->each(function ($redirects, $missingUrl) { |
37
|
|
|
$this->router->get($missingUrl, function () use ($redirects, $missingUrl) { |
38
|
|
|
$redirectUrl = $this->determineRedirectUrl($redirects); |
39
|
|
|
$statusCode = $this->determineRedirectStatusCode($redirects); |
40
|
|
|
|
41
|
|
|
event(new RouteWasHit($redirectUrl, $missingUrl, $statusCode)); |
42
|
|
|
|
43
|
|
|
return redirect()->to( |
44
|
|
|
$redirectUrl, |
45
|
|
|
$statusCode |
46
|
|
|
); |
47
|
|
|
}); |
48
|
|
|
}); |
49
|
|
|
|
50
|
|
|
try { |
51
|
|
|
return $this->router->dispatch($request); |
|
|
|
|
52
|
|
|
} catch (Exception $e) { |
53
|
|
|
event(new RedirectNotFound($request)); |
54
|
|
|
|
55
|
|
|
return; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
protected function determineRedirectUrl($redirects): string |
60
|
|
|
{ |
61
|
|
|
if (is_array($redirects)) { |
62
|
|
|
return $this->resolveRouterParameters($redirects[0]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $this->resolveRouterParameters($redirects); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
protected function determineRedirectStatusCode($redirects): int |
69
|
|
|
{ |
70
|
|
|
return is_array($redirects) ? $redirects[1] : Response::HTTP_MOVED_PERMANENTLY; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
protected function resolveRouterParameters(string $redirectUrl): string |
74
|
|
|
{ |
75
|
|
|
foreach ($this->router->getCurrentRoute()->parameters() as $key => $value) { |
76
|
|
|
$redirectUrl = str_replace("{{$key}}", $value, $redirectUrl); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$redirectUrl = preg_replace('/\/{[\w-]+}/', '', $redirectUrl); |
80
|
|
|
|
81
|
|
|
return $redirectUrl; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.