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