|
1
|
|
|
<?php namespace Arcanedev\LaravelSeo\Redirectors; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanedev\LaravelSeo\Contracts\Redirector; |
|
4
|
|
|
use Illuminate\Http\Request; |
|
5
|
|
|
use Illuminate\Support\Arr; |
|
6
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
7
|
|
|
use Illuminate\Contracts\Routing\Registrar as Router; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class AbstractRedirector |
|
11
|
|
|
* |
|
12
|
|
|
* @package Arcanedev\LaravelSeo\Redirectors |
|
13
|
|
|
* @author ARCANEDEV <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
abstract class AbstractRedirector implements Redirector |
|
16
|
|
|
{ |
|
17
|
|
|
/* ----------------------------------------------------------------- |
|
18
|
|
|
| Properties |
|
19
|
|
|
| ----------------------------------------------------------------- |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
/** @var \Illuminate\Contracts\Routing\Registrar */ |
|
23
|
|
|
protected $router; |
|
24
|
|
|
|
|
25
|
|
|
/** @var \Illuminate\Http\Request */ |
|
26
|
|
|
protected $request; |
|
27
|
|
|
|
|
28
|
|
|
/** @var array */ |
|
29
|
|
|
protected $options = []; |
|
30
|
|
|
|
|
31
|
|
|
/* ----------------------------------------------------------------- |
|
32
|
|
|
| Constructor |
|
33
|
|
|
| ----------------------------------------------------------------- |
|
34
|
|
|
*/ |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* AbstractRedirector constructor. |
|
38
|
|
|
* |
|
39
|
|
|
* @param \Illuminate\Contracts\Routing\Registrar $router |
|
40
|
|
|
* @param array $options |
|
41
|
|
|
*/ |
|
42
|
20 |
|
public function __construct(Router $router, array $options) |
|
43
|
|
|
{ |
|
44
|
20 |
|
$this->router = $router; |
|
45
|
20 |
|
$this->options = $options; |
|
46
|
20 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/* ----------------------------------------------------------------- |
|
49
|
|
|
| Getters & Setters |
|
50
|
|
|
| ----------------------------------------------------------------- |
|
51
|
|
|
*/ |
|
52
|
|
|
/** |
|
53
|
|
|
* Get an option. |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $key |
|
56
|
|
|
* @param mixed|null $default |
|
57
|
|
|
* |
|
58
|
|
|
* @return mixed |
|
59
|
|
|
*/ |
|
60
|
16 |
|
public function getOption($key, $default = null) |
|
61
|
|
|
{ |
|
62
|
16 |
|
return Arr::get($this->options, $key, $default); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/* ----------------------------------------------------------------- |
|
66
|
|
|
| Main Methods |
|
67
|
|
|
| ----------------------------------------------------------------- |
|
68
|
|
|
*/ |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Get the redirect url. |
|
72
|
|
|
* |
|
73
|
|
|
* @param \Illuminate\Http\Request $request |
|
74
|
|
|
* |
|
75
|
|
|
* @return \Illuminate\Http\Response|null |
|
76
|
|
|
*/ |
|
77
|
12 |
|
public function getRedirectFor(Request $request) |
|
|
|
|
|
|
78
|
|
|
{ |
|
79
|
12 |
|
$this->request = $request; |
|
80
|
|
|
|
|
81
|
|
|
collect($this->getRedirectedUrls())->each(function ($redirects, $missingUrl) { |
|
82
|
12 |
|
$this->router->get($missingUrl, function () use ($redirects) { |
|
83
|
10 |
|
return redirect()->to( |
|
84
|
10 |
|
$this->determineRedirectUrl($redirects), |
|
85
|
10 |
|
$this->determineRedirectStatusCode($redirects) |
|
86
|
|
|
); |
|
87
|
12 |
|
}); |
|
88
|
12 |
|
}); |
|
89
|
|
|
|
|
90
|
|
|
try { |
|
91
|
12 |
|
return $this->router->dispatch($request); |
|
92
|
|
|
} |
|
93
|
2 |
|
catch (\Exception $e) { |
|
94
|
2 |
|
return null; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param array|string $redirects |
|
100
|
|
|
* |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
10 |
|
protected function determineRedirectUrl($redirects) |
|
104
|
|
|
{ |
|
105
|
10 |
|
return $this->resolveRouterParameters( |
|
106
|
10 |
|
is_array($redirects) ? reset($redirects) : $redirects |
|
107
|
|
|
); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param mixed $redirects |
|
112
|
|
|
* |
|
113
|
|
|
* @return int |
|
114
|
|
|
*/ |
|
115
|
10 |
|
protected function determineRedirectStatusCode($redirects) |
|
116
|
|
|
{ |
|
117
|
10 |
|
return is_array($redirects) ? end($redirects) : Response::HTTP_MOVED_PERMANENTLY; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param string $redirectUrl |
|
122
|
|
|
* |
|
123
|
|
|
* @return string |
|
124
|
|
|
*/ |
|
125
|
10 |
|
protected function resolveRouterParameters($redirectUrl) |
|
126
|
|
|
{ |
|
127
|
10 |
|
foreach ($this->router->getCurrentRoute()->parameters() as $key => $value) { |
|
128
|
6 |
|
$redirectUrl = str_replace("{{$key}}", $value, $redirectUrl); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
10 |
|
return $redirectUrl; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|