1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Form Request Service Provider. |
4
|
|
|
* |
5
|
|
|
* @package App\Providers |
6
|
|
|
* |
7
|
|
|
* @author Nick Menke <[email protected]> |
8
|
|
|
* @copyright 2018-2020 Nick Menke |
9
|
|
|
* |
10
|
|
|
* @link https://github.com/nlmenke/vertebrae |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
declare(strict_types=1); |
14
|
|
|
|
15
|
|
|
namespace App\Providers; |
16
|
|
|
|
17
|
|
|
use App\Http\Requests\AbstractFormRequest; |
18
|
|
|
use Illuminate\Support\ServiceProvider; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The Form Request service provider. |
22
|
|
|
* |
23
|
|
|
* This service provider is responsible for bootstrapping and registering the |
24
|
|
|
* application's form requests. |
25
|
|
|
* |
26
|
|
|
* @since x.x.x introduced |
27
|
|
|
*/ |
28
|
|
|
class FormRequestServiceProvider extends ServiceProvider |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Register the service provider. |
32
|
|
|
* |
33
|
|
|
* @return void |
34
|
|
|
*/ |
35
|
33 |
|
public function register(): void |
36
|
|
|
{ |
37
|
|
|
$this->app->singleton(AbstractFormRequest::class, function ($app) { |
38
|
16 |
|
$routeParts = explode('@', $app['router']->currentRouteAction()); |
39
|
16 |
|
$routeAction = end($routeParts); |
40
|
|
|
|
41
|
16 |
|
$classParts = explode('\\', reset($routeParts)); |
42
|
16 |
|
$className = end($classParts); |
43
|
16 |
|
$modelName = str_replace(['ApiController', 'Controller'], '', $className); |
44
|
|
|
|
45
|
16 |
|
$formRequestVerb = ''; |
46
|
16 |
|
if ($routeAction === 'store') { |
47
|
8 |
|
$formRequestVerb = 'Create'; |
48
|
8 |
|
} elseif ($routeAction === 'update') { |
49
|
8 |
|
$formRequestVerb = 'Update'; |
50
|
|
|
} |
51
|
|
|
|
52
|
16 |
|
$formRequestName = $formRequestVerb . $modelName . 'Request'; |
53
|
|
|
|
54
|
16 |
|
return $app->make('\App\Http\Requests\\' . $modelName . '\\' . $formRequestName); |
55
|
33 |
|
}); |
56
|
33 |
|
} |
57
|
|
|
} |
58
|
|
|
|