13
13
use Yiisoft \Definitions \ArrayDefinition ;
14
14
use Yiisoft \Definitions \Exception \InvalidConfigException ;
15
15
use Yiisoft \Definitions \Helpers \DefinitionValidator ;
16
- use Yiisoft \Injector \Injector ;
17
16
18
17
use function in_array ;
19
18
use function is_array ;
27
26
final class MiddlewareFactory implements MiddlewareFactoryInterface
28
27
{
29
28
private ContainerInterface $ container ;
29
+ private WrapperFactoryInterface $ wrapperFactory ;
30
30
31
31
/**
32
32
* @param ContainerInterface $container Container to use for resolving definitions.
33
33
*/
34
- public function __construct (ContainerInterface $ container )
34
+ public function __construct (ContainerInterface $ container, WrapperFactoryInterface $ wrapperFactory )
35
35
{
36
36
$ this ->container = $ container ;
37
+ $ this ->wrapperFactory = $ wrapperFactory ;
37
38
}
38
39
39
40
/**
@@ -62,7 +63,8 @@ public function create($middlewareDefinition): MiddlewareInterface
62
63
}
63
64
64
65
if ($ this ->isCallableDefinition ($ middlewareDefinition )) {
65
- return $ this ->wrapCallableDefinition ($ middlewareDefinition );
66
+ /** @var array{0:class-string, 1:string}|Closure $middlewareDefinition */
67
+ return $ this ->wrapperFactory ->create ($ middlewareDefinition );
66
68
}
67
69
68
70
if ($ this ->isArrayDefinition ($ middlewareDefinition )) {
@@ -77,78 +79,6 @@ public function create($middlewareDefinition): MiddlewareInterface
77
79
throw new InvalidMiddlewareDefinitionException ($ middlewareDefinition );
78
80
}
79
81
80
- /**
81
- * @param array|Closure $callback
82
- */
83
- private function wrapCallableDefinition ($ callback ): MiddlewareInterface
84
- {
85
- if (is_array ($ callback )) {
86
- return new class ($ this ->container , $ callback ) implements MiddlewareInterface {
87
- private string $ class ;
88
- private string $ method ;
89
- private ContainerInterface $ container ;
90
- private array $ callback ;
91
-
92
- public function __construct (ContainerInterface $ container , array $ callback )
93
- {
94
- [$ this ->class , $ this ->method ] = $ callback ;
95
- $ this ->container = $ container ;
96
- $ this ->callback = $ callback ;
97
- }
98
-
99
- public function process (
100
- ServerRequestInterface $ request ,
101
- RequestHandlerInterface $ handler
102
- ): ResponseInterface {
103
- /** @var mixed $controller */
104
- $ controller = $ this ->container ->get ($ this ->class );
105
-
106
- /** @var mixed $response */
107
- $ response = (new Injector ($ this ->container ))
108
- ->invoke ([$ controller , $ this ->method ], [$ request , $ handler ]);
109
- if ($ response instanceof ResponseInterface) {
110
- return $ response ;
111
- }
112
-
113
- throw new InvalidMiddlewareDefinitionException ($ this ->callback );
114
- }
115
-
116
- public function __debugInfo ()
117
- {
118
- return [
119
- 'callback ' => $ this ->callback ,
120
- ];
121
- }
122
- };
123
- }
124
-
125
- return new class ($ callback , $ this ->container ) implements MiddlewareInterface {
126
- private ContainerInterface $ container ;
127
- private $ callback ;
128
-
129
- public function __construct (callable $ callback , ContainerInterface $ container )
130
- {
131
- $ this ->callback = $ callback ;
132
- $ this ->container = $ container ;
133
- }
134
-
135
- public function process (
136
- ServerRequestInterface $ request ,
137
- RequestHandlerInterface $ handler
138
- ): ResponseInterface {
139
- /** @var mixed $response */
140
- $ response = (new Injector ($ this ->container ))->invoke ($ this ->callback , [$ request , $ handler ]);
141
- if ($ response instanceof ResponseInterface) {
142
- return $ response ;
143
- }
144
- if ($ response instanceof MiddlewareInterface) {
145
- return $ response ->process ($ request , $ handler );
146
- }
147
- throw new InvalidMiddlewareDefinitionException ($ this ->callback );
148
- }
149
- };
150
- }
151
-
152
82
/**
153
83
* @param mixed $definition
154
84
*
0 commit comments