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
|
|
|
use Concerns\HasCallback; |
16
|
|
|
use Concerns\ControlsAccess; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Rule is applied. |
20
|
|
|
* |
21
|
|
|
* @var bool |
22
|
|
|
*/ |
23
|
|
|
protected $applied; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The countries to apply the rule for. |
27
|
|
|
* |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $countries; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The route. |
34
|
|
|
* |
35
|
|
|
* @var \Illuminate\Routing\Route |
36
|
|
|
*/ |
37
|
|
|
protected $route; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The rule's strategy. |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $strategy; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Create a new GeoRoute instance. |
48
|
|
|
* |
49
|
|
|
* @param \Illuminate\Routing\Route $route |
50
|
|
|
* @param array $countries |
51
|
|
|
* @param string $strategy |
52
|
|
|
*/ |
53
|
70 |
|
public function __construct(Route $route, array $countries, string $strategy) |
54
|
|
|
{ |
55
|
70 |
|
$this->applied = false; |
56
|
70 |
|
$this->countries = array_map('strtoupper', $countries); |
57
|
70 |
|
$this->route = $route; |
58
|
70 |
|
$this->strategy = $strategy; |
59
|
70 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Dynamically call the underlying route. |
63
|
|
|
* |
64
|
|
|
* @param string $method |
65
|
|
|
* @param array $arguments |
66
|
|
|
* |
67
|
|
|
* @return mixed |
68
|
|
|
*/ |
69
|
|
|
public function __call(string $method, array $arguments) |
70
|
|
|
{ |
71
|
|
|
if (method_exists($this->route, $method) || Route::hasMacro($method)) { |
72
|
|
|
return $this->route->$method(...$arguments); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if (CallbackRegistrar::hasProxy($method)) { |
|
|
|
|
76
|
|
|
return $this->setCallback(CallbackRegistrar::callback($method), $arguments); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
throw new BadMethodCallException("Undefined method '$method'"); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Destruct the GeoRoute instance and apply the middleware. |
84
|
|
|
*/ |
85
|
70 |
|
public function __destruct() |
86
|
|
|
{ |
87
|
70 |
|
$this->applyConstraint(); |
88
|
70 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Apply the geo-constraint to the route. |
92
|
|
|
*/ |
93
|
70 |
|
protected function applyConstraint() |
94
|
|
|
{ |
95
|
70 |
|
if ($this->applied || !$this->countries) { |
|
|
|
|
96
|
|
|
return; |
97
|
|
|
} |
98
|
|
|
|
99
|
70 |
|
$action = $this->route->getAction(); |
100
|
70 |
|
$action['middleware'][] = 'geo'; |
101
|
70 |
|
$action['geo'] = [ |
102
|
70 |
|
'strategy' => $this->strategy, |
103
|
70 |
|
'countries' => (array)$this->countries, |
104
|
70 |
|
'callback' => $this->callback, |
105
|
|
|
]; |
106
|
|
|
|
107
|
70 |
|
$this->route->setAction($action); |
|
|
|
|
108
|
|
|
|
109
|
70 |
|
$this->applied = true; |
110
|
70 |
|
} |
111
|
|
|
} |
112
|
|
|
|
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: