1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaraCrafts\GeoRoutes; |
4
|
|
|
|
5
|
|
|
use BadMethodCallException; |
6
|
|
|
use Illuminate\Routing\Route; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use LaraCrafts\GeoRoutes\Support\Facades\CallbackRegistrar; |
|
|
|
|
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @mixin \Illuminate\Routing\Route |
12
|
|
|
*/ |
13
|
|
|
class GeoRoute |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Rule is applied. |
17
|
|
|
* |
18
|
|
|
* @var bool |
19
|
|
|
*/ |
20
|
|
|
protected $applied; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The callback to execute if the visitor |
24
|
|
|
* is not allowed. |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $callback; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The countries to apply the rule for. |
32
|
|
|
* |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $countries; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The route. |
39
|
|
|
* |
40
|
|
|
* @var \Illuminate\Routing\Route |
41
|
|
|
*/ |
42
|
|
|
protected $route; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The rule's strategy. |
46
|
|
|
* |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
protected $strategy; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Create a new GeoRoute instance. |
53
|
|
|
* |
54
|
|
|
* @param \Illuminate\Routing\Route $route |
55
|
|
|
* @param array $countries |
56
|
|
|
* @param string $strategy |
57
|
|
|
*/ |
58
|
70 |
|
public function __construct(Route $route, array $countries, string $strategy) |
59
|
|
|
{ |
60
|
70 |
|
$this->applied = false; |
61
|
70 |
|
$this->countries = array_map('strtoupper', $countries); |
62
|
70 |
|
$this->route = $route; |
63
|
70 |
|
$this->strategy = $strategy; |
64
|
70 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Dynamically call the underlying route. |
68
|
|
|
* |
69
|
|
|
* @param string $method |
70
|
|
|
* @param array $arguments |
71
|
|
|
* |
72
|
|
|
* @return mixed |
73
|
|
|
*/ |
74
|
|
|
public function __call(string $method, array $arguments) |
75
|
|
|
{ |
76
|
|
|
if (method_exists($this->route, $method) || Route::hasMacro($method)) { |
77
|
|
|
return $this->route->$method(...$arguments); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (CallbackRegistrar::hasProxy($method)) { |
|
|
|
|
81
|
|
|
return $this->setCallback(CallbackRegistrar::callback($method), $arguments); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
throw new BadMethodCallException("Undefined method '$method'"); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Destruct the GeoRoute instance and apply the middleware. |
89
|
|
|
*/ |
90
|
70 |
|
public function __destruct() |
91
|
|
|
{ |
92
|
70 |
|
$this->applyConstraint(); |
93
|
70 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Allow given countries. |
97
|
|
|
* |
98
|
|
|
* @return $this |
99
|
|
|
*/ |
100
|
|
|
public function allow() |
101
|
|
|
{ |
102
|
|
|
$this->strategy = 'allow'; |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Apply the geo-constraint to the route. |
109
|
|
|
*/ |
110
|
70 |
|
protected function applyConstraint() |
111
|
|
|
{ |
112
|
70 |
|
if ($this->applied || !$this->countries) { |
|
|
|
|
113
|
|
|
return; |
114
|
|
|
} |
115
|
|
|
|
116
|
70 |
|
$action = $this->route->getAction(); |
117
|
70 |
|
$action['middleware'][] = 'geo'; |
118
|
70 |
|
$action['geo'] = [ |
119
|
70 |
|
'strategy' => $this->strategy, |
120
|
70 |
|
'countries' => (array)$this->countries, |
121
|
70 |
|
'callback' => $this->callback, |
122
|
|
|
]; |
123
|
|
|
|
124
|
70 |
|
$this->route->setAction($action); |
|
|
|
|
125
|
|
|
|
126
|
70 |
|
$this->applied = true; |
127
|
70 |
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Deny given countries. |
131
|
|
|
* |
132
|
|
|
* @return $this |
133
|
|
|
*/ |
134
|
|
|
public function deny() |
135
|
|
|
{ |
136
|
|
|
$this->strategy = 'deny'; |
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Return a HTTP 404 error if access is denied. |
143
|
|
|
* |
144
|
|
|
* @return $this |
145
|
|
|
*/ |
146
|
14 |
|
public function orNotFound() |
147
|
|
|
{ |
148
|
14 |
|
return $this->setCallback('LaraCrafts\GeoRoutes\Callbacks::notFound', func_get_args()); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Redirect to given route if access is denied. |
153
|
|
|
* |
154
|
|
|
* @param string $routeName |
155
|
|
|
* |
156
|
|
|
* @return $this |
157
|
|
|
*/ |
158
|
14 |
|
public function orRedirectTo(string $routeName) |
159
|
|
|
{ |
160
|
14 |
|
return $this->setCallback('LaraCrafts\GeoRoutes\Callbacks::redirectTo', func_get_args()); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Return a HTTP 401 error if access is denied (this is the default behavior). |
165
|
|
|
* |
166
|
|
|
* @return $this |
167
|
|
|
*/ |
168
|
|
|
public function orUnauthorized() |
169
|
|
|
{ |
170
|
|
|
$this->callback = null; |
171
|
|
|
|
172
|
|
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Set the callback. |
177
|
|
|
* |
178
|
|
|
* @param callable $callback |
179
|
|
|
* @param array $arguments |
180
|
|
|
* |
181
|
|
|
* @return $this |
182
|
|
|
*/ |
183
|
28 |
|
protected function setCallback(callable $callback, array $arguments) |
184
|
|
|
{ |
185
|
28 |
|
if (is_string($callback) && Str::contains($callback, '@')) { |
186
|
|
|
$callback = Str::parseCallback($callback, '__invoke'); |
187
|
|
|
$callback[0] = resolve($callback[0]); |
188
|
|
|
} |
189
|
|
|
|
190
|
28 |
|
$this->callback = [$callback, $arguments]; |
191
|
|
|
|
192
|
28 |
|
return $this; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: