1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of emanci/lumen-request package. |
5
|
|
|
* |
6
|
|
|
* (c) emanci <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Emanci\LumenRequest; |
13
|
|
|
|
14
|
|
|
use Emanci\LumenRequest\Console\RequestMakeCommand; |
15
|
|
|
use Illuminate\Contracts\Validation\ValidatesWhenResolved; |
16
|
|
|
use Illuminate\Support\ServiceProvider; |
17
|
|
|
use Symfony\Component\HttpFoundation\Request; |
18
|
|
|
|
19
|
|
|
class InputRequestServiceProvider extends ServiceProvider |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Bootstrap the application events. |
23
|
|
|
*/ |
24
|
|
|
public function boot() |
25
|
|
|
{ |
26
|
3 |
|
$this->app->afterResolving(ValidatesWhenResolved::class, function ($resolved) { |
27
|
3 |
|
if (method_exists($resolved, 'validate')) { |
28
|
3 |
|
$resolved->validate(); |
29
|
|
|
} else { |
30
|
|
|
$resolved->validateResolved(); |
31
|
|
|
} |
32
|
3 |
|
}); |
33
|
|
|
|
34
|
3 |
|
$this->app->resolving(InputRequest::class, function ($request, $app) { |
35
|
3 |
|
$this->initializeRequest($request, $app['request']); |
36
|
|
|
|
37
|
3 |
|
$request->setContainer($app); |
38
|
3 |
|
}); |
39
|
3 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
*/ |
44
|
3 |
|
public function register() |
45
|
|
|
{ |
46
|
3 |
|
$this->registerCommands(); |
47
|
3 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Register the given commands. |
51
|
|
|
*/ |
52
|
3 |
|
protected function registerCommands() |
53
|
|
|
{ |
54
|
3 |
|
$this->registerRequestMakeCommand(); |
55
|
|
|
|
56
|
3 |
|
$this->commands('command.request.make'); |
57
|
3 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Register the command. |
61
|
|
|
*/ |
62
|
|
|
protected function registerRequestMakeCommand() |
63
|
|
|
{ |
64
|
3 |
|
$this->app->singleton('command.request.make', function ($app) { |
65
|
|
|
return new RequestMakeCommand($app['files']); |
66
|
3 |
|
}); |
67
|
3 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Initialize the form request with data from the given request. |
71
|
|
|
* |
72
|
|
|
* @param InputRequest $form |
73
|
|
|
* @param Request $current |
74
|
|
|
*/ |
75
|
3 |
|
protected function initializeRequest(InputRequest $form, Request $current) |
76
|
|
|
{ |
77
|
3 |
|
$files = $current->files->all(); |
78
|
|
|
|
79
|
3 |
|
$files = is_array($files) ? array_filter($files) : $files; |
80
|
|
|
|
81
|
3 |
|
$form->initialize( |
82
|
3 |
|
$current->query->all(), $current->request->all(), $current->attributes->all(), |
83
|
3 |
|
$current->cookies->all(), $files, $current->server->all(), $current->getContent() |
84
|
|
|
); |
85
|
|
|
|
86
|
3 |
|
$form->setJson($current->json()); |
|
|
|
|
87
|
|
|
|
88
|
3 |
|
$form->setUserResolver($current->getUserResolver()); |
|
|
|
|
89
|
|
|
|
90
|
3 |
|
$form->setRouteResolver($current->getRouteResolver()); |
|
|
|
|
91
|
3 |
|
} |
92
|
|
|
} |
93
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: