|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LaraCrafts\GeoRoutes; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Routing\Route; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @mixin \Illuminate\Routing\Route |
|
9
|
|
|
*/ |
|
10
|
|
|
class GeoRoute |
|
11
|
|
|
{ |
|
12
|
|
|
use CallbacksHandler; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Rule is applied. |
|
16
|
|
|
* |
|
17
|
|
|
* @var bool |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $applied; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* The countries to apply the rule for. |
|
23
|
|
|
* |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $countries; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* The route. |
|
30
|
|
|
* |
|
31
|
|
|
* @var \Illuminate\Routing\Route |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $route; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Determine if the requests are |
|
37
|
|
|
* allowed from the given countries |
|
38
|
|
|
* |
|
39
|
|
|
* @var boolean |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $allowed; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* The geo-constraint |
|
45
|
|
|
* |
|
46
|
|
|
* @var \LaraCrafts\GeoRoutes\GeoConstraint |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $constraint; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Create a new GeoRoute instance. |
|
52
|
|
|
* |
|
53
|
|
|
* @param \Illuminate\Routing\Route $route |
|
54
|
|
|
* @param array $countries |
|
55
|
|
|
* @param boolean $allowed |
|
56
|
|
|
*/ |
|
57
|
80 |
|
public function __construct(Route $route, array $countries, bool $allowed) |
|
58
|
|
|
{ |
|
59
|
80 |
|
$this->applied = false; |
|
60
|
80 |
|
$this->countries = $countries; |
|
61
|
80 |
|
$this->route = $route; |
|
62
|
80 |
|
$this->constraint = new GeoConstraint($allowed, $countries); |
|
63
|
80 |
|
$this->constraint->setAccess($allowed); |
|
64
|
|
|
|
|
65
|
80 |
|
CallbacksRegistrar::init(); |
|
66
|
80 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Destruct the GeoRoute instance and apply the middleware. |
|
70
|
|
|
*/ |
|
71
|
80 |
|
public function __destruct() |
|
72
|
|
|
{ |
|
73
|
80 |
|
$this->applyConstraint(); |
|
74
|
80 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Apply the GeoConstraint |
|
78
|
|
|
* |
|
79
|
|
|
* @return void |
|
80
|
|
|
*/ |
|
81
|
80 |
|
protected function applyConstraint() |
|
82
|
|
|
{ |
|
83
|
80 |
|
if ($this->applied || !$this->countries) { |
|
|
|
|
|
|
84
|
|
|
return; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
80 |
|
$action = $this->route->getAction(); |
|
88
|
80 |
|
$action['middleware'][] = 'geo'; |
|
89
|
|
|
|
|
90
|
80 |
|
$this->applied = true; |
|
91
|
80 |
|
$this->route->setAction($action); |
|
|
|
|
|
|
92
|
80 |
|
$this->route->GeoConstraint = $this->constraint; |
|
|
|
|
|
|
93
|
80 |
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
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.