|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace SolumDeSignum\Scenarios; |
|
6
|
|
|
|
|
7
|
|
|
use function config; |
|
|
|
|
|
|
8
|
|
|
use Illuminate\Support\Facades\Route; |
|
9
|
|
|
use function is_bool; |
|
10
|
|
|
|
|
11
|
|
|
trait Scenarios |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
public $scenario; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
public $setMethodFromController; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
public $setMethodFromUrl; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Create a new rule instance. |
|
30
|
|
|
* |
|
31
|
|
|
* Scenarios constructor. |
|
32
|
|
|
* |
|
33
|
|
|
* @throws \Exception |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct() |
|
36
|
|
|
{ |
|
37
|
|
|
// Set Config |
|
38
|
|
|
$this->setMethodFromController = config( |
|
|
|
|
|
|
39
|
|
|
'scenarios.features.setMethodFromController', |
|
40
|
|
|
true |
|
41
|
|
|
); |
|
42
|
|
|
$this->setMethodFromUrl = config( |
|
43
|
|
|
'scenarios.features.setMethodFromUrlSegment', |
|
44
|
|
|
false |
|
45
|
|
|
); |
|
46
|
|
|
|
|
47
|
|
|
// Detect package abuse |
|
48
|
|
|
$this->exceptionOneSetMethod(); |
|
49
|
|
|
$this->exceptionOnlyOneSetMethod(); |
|
50
|
|
|
|
|
51
|
|
|
// setMethod based on Controller function |
|
52
|
|
|
if ($this->setMethodFromController) { |
|
53
|
|
|
$this->scenario = $this->patternFilter($this->currentControllerMethod()); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
// setMethod based on Request segment based on $controllerMethodPattern |
|
57
|
|
|
if ($this->setMethodFromUrl) { |
|
58
|
|
|
$this->scenario = $this->patternFilter($this->currentRequestUri()); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param $method |
|
64
|
|
|
* |
|
65
|
|
|
* @throws \Exception |
|
66
|
|
|
* |
|
67
|
|
|
* @return string |
|
68
|
|
|
*/ |
|
69
|
|
|
public function patternFilter($method): string |
|
70
|
|
|
{ |
|
71
|
|
|
preg_match_all( |
|
72
|
|
|
config('scenarios.methods.pattern'), |
|
|
|
|
|
|
73
|
|
|
strtolower($method), |
|
74
|
|
|
$matches |
|
75
|
|
|
); |
|
76
|
|
|
|
|
77
|
|
|
$this->exceptionScenarioPattern($matches[0][0]); |
|
78
|
|
|
|
|
79
|
|
|
return $matches[0][0]; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return string|null |
|
84
|
|
|
*/ |
|
85
|
|
|
public function currentControllerMethod(): ?string |
|
86
|
|
|
{ |
|
87
|
|
|
return Route::current() !== null ? |
|
88
|
|
|
Route::current()->getActionMethod() : |
|
89
|
|
|
null; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return string|null |
|
94
|
|
|
*/ |
|
95
|
|
|
public function currentRequestUri(): ?string |
|
96
|
|
|
{ |
|
97
|
|
|
return Route::getCurrentRequest() !== null ? |
|
98
|
|
|
Route::getCurrentRequest()->getRequestUri() : |
|
99
|
|
|
null; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param mixed $matches |
|
104
|
|
|
* |
|
105
|
|
|
* @throws \Exception |
|
106
|
|
|
*/ |
|
107
|
|
|
private function exceptionScenarioPattern($matches): void |
|
108
|
|
|
{ |
|
109
|
|
|
if (isset($matches) === false) { |
|
110
|
|
|
throw new \Exception( |
|
111
|
|
|
'Scenarios patternFilter failed finding match, check $scenarioPattern , LIKE RIGHT NOW !!!' |
|
112
|
|
|
); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
private function exceptionOneSetMethod(): void |
|
117
|
|
|
{ |
|
118
|
|
|
if ( |
|
119
|
|
|
is_bool($this->setMethodFromController) === false || |
|
|
|
|
|
|
120
|
|
|
is_bool($this->setMethodFromUrl) === false || |
|
121
|
|
|
($this->setMethodFromController === false && $this->setMethodFromUrl === false) |
|
122
|
|
|
) { |
|
123
|
|
|
throw new \Exception( |
|
124
|
|
|
'Please enable at least one setMethod function, LIKE RIGHT NOW !!!' |
|
125
|
|
|
); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
private function exceptionOnlyOneSetMethod(): void |
|
130
|
|
|
{ |
|
131
|
|
|
if ( |
|
132
|
|
|
is_bool($this->setMethodFromController) === false || |
|
|
|
|
|
|
133
|
|
|
is_bool($this->setMethodFromUrl) === false || |
|
134
|
|
|
($this->setMethodFromController === true && $this->setMethodFromUrl === true) |
|
135
|
|
|
) { |
|
136
|
|
|
throw new \Exception( |
|
137
|
|
|
'Please enable only one setMethod function, LIKE RIGHT NOW !!!' |
|
138
|
|
|
); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|