|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LaraCrafts\GeoRoutes; |
|
4
|
|
|
|
|
5
|
|
|
use BadMethodCallException; |
|
6
|
|
|
use Illuminate\Routing\Route; |
|
7
|
|
|
use LaraCrafts\GeoRoutes\Support\Facades\CallbackRegistrar; |
|
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @mixin \Illuminate\Routing\Route |
|
11
|
|
|
*/ |
|
12
|
|
|
class GeoRoute |
|
13
|
|
|
{ |
|
14
|
|
|
use Concerns\HasCallback; |
|
15
|
|
|
use Concerns\ControlsAccess; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Rule is applied. |
|
19
|
|
|
* |
|
20
|
|
|
* @var bool |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $applied; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The countries to apply the rule for. |
|
26
|
|
|
* |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $countries; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* The route. |
|
33
|
|
|
* |
|
34
|
|
|
* @var \Illuminate\Routing\Route |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $route; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* The rule's strategy. |
|
40
|
|
|
* |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $strategy; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Create a new GeoRoute instance. |
|
47
|
|
|
* |
|
48
|
|
|
* @param \Illuminate\Routing\Route $route |
|
49
|
|
|
* @param array $countries |
|
50
|
|
|
* @param string $strategy |
|
51
|
|
|
*/ |
|
52
|
70 |
|
public function __construct(Route $route, array $countries, string $strategy) |
|
53
|
|
|
{ |
|
54
|
70 |
|
$this->applied = false; |
|
55
|
70 |
|
$this->countries = array_map('strtoupper', $countries); |
|
56
|
70 |
|
$this->route = $route; |
|
57
|
70 |
|
$this->strategy = $strategy; |
|
58
|
70 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Dynamically call the underlying route. |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $method |
|
64
|
|
|
* @param array $arguments |
|
65
|
|
|
* |
|
66
|
|
|
* @return mixed |
|
67
|
|
|
*/ |
|
68
|
|
|
public function __call(string $method, array $arguments) |
|
69
|
|
|
{ |
|
70
|
|
|
if (method_exists($this->route, $method) || Route::hasMacro($method)) { |
|
71
|
|
|
return $this->route->$method(...$arguments); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if (CallbackRegistrar::hasProxy($method)) { |
|
|
|
|
|
|
75
|
|
|
return $this->setCallback(CallbackRegistrar::callback($method), $arguments); |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
throw new BadMethodCallException("Undefined method '$method'"); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Destruct the GeoRoute instance and apply the middleware. |
|
83
|
|
|
*/ |
|
84
|
70 |
|
public function __destruct() |
|
85
|
|
|
{ |
|
86
|
70 |
|
$this->applyConstraint(); |
|
87
|
70 |
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Apply the geo-constraint to the route. |
|
91
|
|
|
*/ |
|
92
|
70 |
|
protected function applyConstraint() |
|
93
|
|
|
{ |
|
94
|
70 |
|
if ($this->applied || !$this->countries) { |
|
|
|
|
|
|
95
|
|
|
return; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
70 |
|
$action = $this->route->getAction(); |
|
99
|
70 |
|
$action['middleware'][] = 'geo'; |
|
100
|
70 |
|
$action['geo'] = [ |
|
101
|
70 |
|
'strategy' => $this->strategy, |
|
102
|
70 |
|
'countries' => (array)$this->countries, |
|
103
|
70 |
|
'callback' => $this->callback, |
|
104
|
|
|
]; |
|
105
|
|
|
|
|
106
|
70 |
|
$this->route->setAction($action); |
|
|
|
|
|
|
107
|
|
|
|
|
108
|
70 |
|
$this->applied = true; |
|
109
|
70 |
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: