|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LaraCrafts\GeoRoutes; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use Illuminate\Support\Arr; |
|
7
|
|
|
|
|
8
|
|
|
class GeoGroup |
|
9
|
|
|
{ |
|
10
|
|
|
use Concerns\HasCallback; |
|
11
|
|
|
use Concerns\ControlsAccess; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Determines if the geo rule is applied. |
|
15
|
|
|
* |
|
16
|
|
|
* @var bool |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $applied; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The routes closure. |
|
22
|
|
|
* |
|
23
|
|
|
* @var \Closure |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $routes; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The routes group shared attributes. |
|
29
|
|
|
* |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $attributes; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* The router instance. |
|
36
|
|
|
* |
|
37
|
|
|
* @var \Illuminate\Routing\Router |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $router; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* The attributes that can be set through this class. |
|
43
|
|
|
* |
|
44
|
|
|
* @var array |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $allowedAttributes = [ |
|
47
|
|
|
'as', 'domain', 'middleware', 'name', 'namespace', 'prefix', 'where', |
|
48
|
|
|
]; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* The attributes that are aliased. |
|
52
|
|
|
* |
|
53
|
|
|
* @var array |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $aliases = [ |
|
56
|
|
|
'name' => 'as', |
|
57
|
|
|
]; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Create a new GeoGroup instance. |
|
61
|
|
|
* |
|
62
|
|
|
* @param array $attributes |
|
63
|
|
|
* @param \Closure $routes |
|
64
|
|
|
*/ |
|
65
|
98 |
|
public function __construct(array $attributes, Closure $routes) |
|
66
|
|
|
{ |
|
67
|
98 |
|
$this->attributes = $attributes; |
|
68
|
98 |
|
$this->routes = $routes; |
|
69
|
98 |
|
$this->router = app('router'); |
|
70
|
98 |
|
$this->applied = false; |
|
71
|
|
|
|
|
72
|
98 |
|
static::loadProxies(); |
|
73
|
98 |
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Destruct the GeoGroup instance and apply the rule. |
|
77
|
|
|
*/ |
|
78
|
98 |
|
public function __destruct() |
|
79
|
|
|
{ |
|
80
|
98 |
|
$this->applyConstraint(); |
|
81
|
98 |
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Set the array of countries covered by the rule. |
|
85
|
|
|
* |
|
86
|
|
|
* @param string ...$countries |
|
87
|
|
|
* |
|
88
|
|
|
* @return $this |
|
89
|
|
|
*/ |
|
90
|
|
|
public function from(string ...$countries) |
|
91
|
|
|
{ |
|
92
|
|
|
$this->countries = $countries; |
|
93
|
|
|
|
|
94
|
|
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Apply the geo-constraint to the routes group. |
|
99
|
|
|
*/ |
|
100
|
98 |
|
protected function applyConstraint() |
|
101
|
|
|
{ |
|
102
|
98 |
|
if ($this->applied || !$this->countries) { |
|
|
|
|
|
|
103
|
|
|
return; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
98 |
|
$attributes = $this->attributes; |
|
107
|
98 |
|
$attributes['middleware'][] = 'geo'; |
|
108
|
98 |
|
$attributes['geo'] = [ |
|
109
|
98 |
|
'strategy' => $this->strategy, |
|
110
|
98 |
|
'countries' => (array)$this->countries, |
|
111
|
98 |
|
'callback' => $this->callback, |
|
112
|
|
|
]; |
|
113
|
|
|
|
|
114
|
98 |
|
$this->router->group($attributes, $this->routes); |
|
115
|
|
|
|
|
116
|
98 |
|
$this->applied = true; |
|
117
|
98 |
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Dynamically call the router methods. |
|
121
|
|
|
* |
|
122
|
|
|
* @param string $method |
|
123
|
|
|
* @param array $arguments |
|
124
|
|
|
* |
|
125
|
|
|
* @return mixed |
|
126
|
|
|
*/ |
|
127
|
14 |
|
public function __call(string $method, array $arguments) |
|
128
|
|
|
{ |
|
129
|
14 |
|
if ($this->callbackExists($method)) { |
|
130
|
|
|
return $this->setCallback(static::$proxies[$method], $arguments); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
14 |
|
if ($this->router::hasMacro($method)) { |
|
134
|
|
|
return $this->macroCall($method, $arguments); |
|
|
|
|
|
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
14 |
|
if ($method === 'middleware') { |
|
138
|
|
|
return $this->attribute($method, is_array($arguments[0]) ? $arguments[0] : $arguments); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
14 |
|
return ($this)->attribute($method, $arguments[0]); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Set the value for a given attribute. |
|
146
|
|
|
* |
|
147
|
|
|
* @param string $key |
|
148
|
|
|
* @param mixed $value |
|
149
|
|
|
* |
|
150
|
|
|
* @return $this |
|
151
|
|
|
* |
|
152
|
|
|
* @throws \InvalidArgumentException |
|
153
|
|
|
*/ |
|
154
|
28 |
|
public function attribute($key, $value) |
|
155
|
|
|
{ |
|
156
|
28 |
|
if (! in_array($key, $this->allowedAttributes)) { |
|
157
|
|
|
throw new \InvalidArgumentException("Attribute [{$key}] does not exist."); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
28 |
|
$this->attributes[Arr::get($this->aliases, $key, $key)] = $value; |
|
161
|
|
|
|
|
162
|
28 |
|
return $this; |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.