1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arcanedev\MissingUrlsRedirector\Helpers; |
6
|
|
|
|
7
|
|
|
use Arcanedev\MissingUrlsRedirector\Entities\{Redirection, RedirectionCollection}; |
8
|
|
|
use Arcanedev\MissingUrlsRedirector\Events\RouteRedirected; |
9
|
|
|
use Illuminate\Routing\{Route, RouteAction, RouteCollection}; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class RouteMaker |
13
|
|
|
* |
14
|
|
|
* @package Arcanedev\MissingUrlsRedirector\Helpers |
15
|
|
|
* @author ARCANEDEV <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class RouteMaker |
18
|
|
|
{ |
19
|
|
|
/* ----------------------------------------------------------------- |
20
|
|
|
| Main Methods |
21
|
|
|
| ----------------------------------------------------------------- |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Make routes collection. |
26
|
|
|
* |
27
|
|
|
* @param \Arcanedev\MissingUrlsRedirector\Entities\RedirectionCollection $redirections |
28
|
|
|
* |
29
|
|
|
* @return \Illuminate\Routing\RouteCollection |
30
|
|
|
*/ |
31
|
66 |
|
public static function makeCollection(RedirectionCollection $redirections): RouteCollection |
32
|
|
|
{ |
33
|
66 |
|
$routes = new RouteCollection; |
34
|
|
|
|
35
|
66 |
|
foreach ($redirections as $group) { |
36
|
60 |
|
foreach ($group as $redirection) { |
37
|
|
|
/** @var \Arcanedev\MissingUrlsRedirector\Entities\Redirection $redirection */ |
38
|
60 |
|
$routes->add($route = static::makeRoute($redirection)); |
39
|
60 |
|
$redirection->setRoute($route); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
66 |
|
return $routes; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Make a route. |
48
|
|
|
* |
49
|
|
|
* @param \Arcanedev\MissingUrlsRedirector\Entities\Redirection $redirection |
50
|
|
|
* |
51
|
|
|
* @return \Illuminate\Routing\Route |
52
|
|
|
*/ |
53
|
60 |
|
public static function makeRoute(Redirection $redirection): Route |
54
|
|
|
{ |
55
|
60 |
|
$route = new Route(['GET', 'HEAD'], $redirection->getFrom(), []); |
56
|
|
|
|
57
|
60 |
|
return $route->setAction( |
58
|
60 |
|
static::parseAction($route, $redirection) |
|
|
|
|
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/* ----------------------------------------------------------------- |
63
|
|
|
| Other Methods |
64
|
|
|
| ----------------------------------------------------------------- |
65
|
|
|
*/ |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Resolve the router parameters. |
69
|
|
|
* |
70
|
|
|
* @param \Illuminate\Routing\Route $route |
71
|
|
|
* @param \Arcanedev\MissingUrlsRedirector\Entities\Redirection $redirection |
72
|
|
|
* |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
60 |
|
private static function parseAction(Route $route, Redirection $redirection): array |
76
|
|
|
{ |
77
|
|
|
return RouteAction::parse($route->uri(), function () use ($redirection, $route) { |
78
|
60 |
|
event(new RouteRedirected($redirection)); |
79
|
|
|
|
80
|
60 |
|
return redirect()->to( |
81
|
60 |
|
$redirection->getResolvedUrl(), |
82
|
60 |
|
$redirection->getStatus() |
83
|
|
|
); |
84
|
60 |
|
}); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: