@@ -9,35 +9,35 @@ |
||
| 9 | 9 | |
| 10 | 10 | class ScenariosServiceProvider extends ServiceProvider |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * Bootstrap the application events. |
|
| 14 | - * |
|
| 15 | - * @return void |
|
| 16 | - */ |
|
| 17 | - public function boot(): void |
|
| 18 | - { |
|
| 19 | - if ($this->app->runningInConsole()) { |
|
| 20 | - $this->publishes( |
|
| 21 | - [ |
|
| 22 | - __DIR__.'/../config/scenarios.php' => config_path( |
|
| 23 | - 'scenarios.php' |
|
| 24 | - ), |
|
| 25 | - ], |
|
| 26 | - 'config' |
|
| 27 | - ); |
|
| 28 | - } |
|
| 29 | - } |
|
| 12 | + /** |
|
| 13 | + * Bootstrap the application events. |
|
| 14 | + * |
|
| 15 | + * @return void |
|
| 16 | + */ |
|
| 17 | + public function boot(): void |
|
| 18 | + { |
|
| 19 | + if ($this->app->runningInConsole()) { |
|
| 20 | + $this->publishes( |
|
| 21 | + [ |
|
| 22 | + __DIR__.'/../config/scenarios.php' => config_path( |
|
| 23 | + 'scenarios.php' |
|
| 24 | + ), |
|
| 25 | + ], |
|
| 26 | + 'config' |
|
| 27 | + ); |
|
| 28 | + } |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Register the application services. |
|
| 33 | - * |
|
| 34 | - * @return void |
|
| 35 | - */ |
|
| 36 | - public function register(): void |
|
| 37 | - { |
|
| 38 | - $this->mergeConfigFrom( |
|
| 39 | - __DIR__.'/../config/scenarios.php', |
|
| 40 | - 'scenarios' |
|
| 41 | - ); |
|
| 42 | - } |
|
| 31 | + /** |
|
| 32 | + * Register the application services. |
|
| 33 | + * |
|
| 34 | + * @return void |
|
| 35 | + */ |
|
| 36 | + public function register(): void |
|
| 37 | + { |
|
| 38 | + $this->mergeConfigFrom( |
|
| 39 | + __DIR__.'/../config/scenarios.php', |
|
| 40 | + 'scenarios' |
|
| 41 | + ); |
|
| 42 | + } |
|
| 43 | 43 | } |
@@ -10,132 +10,132 @@ |
||
| 10 | 10 | |
| 11 | 11 | trait Scenarios |
| 12 | 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 | - } |
|
| 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 | 141 | } |
@@ -85,8 +85,7 @@ discard block |
||
| 85 | 85 | public function currentControllerMethod(): ?string |
| 86 | 86 | { |
| 87 | 87 | return Route::current() !== null ? |
| 88 | - Route::current()->getActionMethod() : |
|
| 89 | - null; |
|
| 88 | + Route::current()->getActionMethod() : null; |
|
| 90 | 89 | } |
| 91 | 90 | |
| 92 | 91 | /** |
@@ -95,8 +94,7 @@ discard block |
||
| 95 | 94 | public function currentRequestUri(): ?string |
| 96 | 95 | { |
| 97 | 96 | return Route::getCurrentRequest() !== null ? |
| 98 | - Route::getCurrentRequest()->getRequestUri() : |
|
| 99 | - null; |
|
| 97 | + Route::getCurrentRequest()->getRequestUri() : null; |
|
| 100 | 98 | } |
| 101 | 99 | |
| 102 | 100 | /** |
@@ -3,11 +3,11 @@ |
||
| 3 | 3 | declare(strict_types=1); |
| 4 | 4 | |
| 5 | 5 | return [ |
| 6 | - 'features' => [ |
|
| 7 | - 'setMethodFromUrlSegment' => false, |
|
| 8 | - 'setMethodFromController' => true, |
|
| 9 | - ], |
|
| 10 | - 'methods' => [ |
|
| 11 | - 'pattern' => '/create|store|update|destroy/im', |
|
| 12 | - ], |
|
| 6 | + 'features' => [ |
|
| 7 | + 'setMethodFromUrlSegment' => false, |
|
| 8 | + 'setMethodFromController' => true, |
|
| 9 | + ], |
|
| 10 | + 'methods' => [ |
|
| 11 | + 'pattern' => '/create|store|update|destroy/im', |
|
| 12 | + ], |
|
| 13 | 13 | ]; |