1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaraCrafts\GeoRoutes; |
4
|
|
|
|
5
|
|
|
class GeoGroup |
6
|
|
|
{ |
7
|
|
|
use CallbacksHandler; |
|
|
|
|
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* The route group attributes |
11
|
|
|
* |
12
|
|
|
* @var array |
13
|
|
|
*/ |
14
|
|
|
protected $attributes; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The raw routes |
18
|
|
|
* |
19
|
|
|
* @var callable |
20
|
|
|
*/ |
21
|
|
|
protected $routes; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The geo-constraint |
25
|
|
|
* |
26
|
|
|
* @var GeoConstraint |
27
|
|
|
*/ |
28
|
|
|
protected $constraint; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The countries covered by the constraint |
32
|
|
|
* |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $countries; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Create a new GeoGroup instance |
39
|
|
|
* |
40
|
|
|
* @param array $attributes |
41
|
|
|
* @param callable $routes |
42
|
|
|
*/ |
43
|
50 |
|
public function __construct(array $attributes, callable $routes) |
44
|
|
|
{ |
45
|
50 |
|
$this->attributes = $attributes; |
46
|
50 |
|
$this->routes = $routes; |
47
|
|
|
|
48
|
50 |
|
$this->constraint = new GeoConstraint(false, []); |
49
|
|
|
|
50
|
50 |
|
CallbacksRegistrar::init(); |
51
|
50 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Destruct the GeoGroup instance |
55
|
|
|
*/ |
56
|
50 |
|
public function __destruct() |
57
|
|
|
{ |
58
|
50 |
|
$this->constraint->setCountries(...$this->countries ?? []); |
59
|
|
|
|
60
|
50 |
|
$this->attributes = array_merge($this->attributes, [ |
61
|
50 |
|
'GeoConstraint' => $this->constraint, |
62
|
50 |
|
'middleware' => 'geo', |
63
|
|
|
]); |
64
|
|
|
|
65
|
50 |
|
app('router')->group($this->attributes, $this->routes); |
66
|
50 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Allow access from given countries |
70
|
|
|
* |
71
|
|
|
* @param string ...$countries |
72
|
|
|
* |
73
|
|
|
* @return $this |
74
|
|
|
*/ |
75
|
20 |
|
public function allowFrom(string ...$countries) |
76
|
|
|
{ |
77
|
20 |
|
$this->countries = $countries; |
78
|
20 |
|
$this->constraint->setAccess(true); |
79
|
|
|
|
80
|
20 |
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Deny access from given countries |
85
|
|
|
* |
86
|
|
|
* @param string ...$countries |
87
|
|
|
* |
88
|
|
|
* @return $this |
89
|
|
|
*/ |
90
|
30 |
|
public function denyFrom(string ...$countries) |
91
|
|
|
{ |
92
|
30 |
|
$this->countries = $countries; |
93
|
30 |
|
$this->constraint->setAccess(false); |
94
|
|
|
|
95
|
30 |
|
return $this; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|