| 1 | <?php namespace Arcanedev\LaravelSeo\Redirectors; |
||
| 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) |
|
| 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) |
|
| 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) |
|
|
1 ignored issue
–
show
|
|||
| 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) |
|
| 119 | |||
| 120 | /** |
||
| 121 | * @param string $redirectUrl |
||
| 122 | * |
||
| 123 | * @return string |
||
| 124 | */ |
||
| 125 | 10 | protected function resolveRouterParameters($redirectUrl) |
|
| 133 | } |
||
| 134 |