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
|
80 |
|
public function __construct(Route $route, array $countries, string $strategy) |
53
|
|
|
{ |
54
|
80 |
|
$this->applied = false; |
55
|
80 |
|
$this->countries = array_map('strtoupper', $countries); |
56
|
80 |
|
$this->route = $route; |
57
|
80 |
|
$this->strategy = $strategy; |
58
|
80 |
|
} |
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
|
80 |
|
public function __destruct() |
85
|
|
|
{ |
86
|
80 |
|
$this->applyConstraint(); |
87
|
80 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Apply the geo-constraint to the route. |
91
|
|
|
*/ |
92
|
80 |
|
protected function applyConstraint() |
93
|
|
|
{ |
94
|
80 |
|
if ($this->applied || !$this->countries) { |
|
|
|
|
95
|
|
|
return; |
96
|
|
|
} |
97
|
|
|
|
98
|
80 |
|
$action = $this->route->getAction(); |
99
|
80 |
|
$action['middleware'][] = 'geo'; |
100
|
80 |
|
$action['geo'] = [ |
101
|
80 |
|
'strategy' => $this->strategy, |
102
|
80 |
|
'countries' => (array)$this->countries, |
103
|
80 |
|
'callback' => $this->callback, |
104
|
|
|
]; |
105
|
|
|
|
106
|
80 |
|
$this->route->setAction($action); |
|
|
|
|
107
|
|
|
|
108
|
80 |
|
$this->applied = true; |
109
|
80 |
|
} |
110
|
|
|
} |
111
|
|
|
|
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: