Skip to content

Commit e70e026

Browse files
committed
Merge branch 'master' into 3.1-merge
2 parents e049280 + a35fe9b commit e70e026

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

src/Middleware/ValidationMiddleware.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ protected function prepareHandler(array|string $handler): array
137137
return explode('@', $handler);
138138
}
139139
$array = explode('::', $handler);
140+
if (! isset($array[1]) && class_exists($handler) && method_exists($handler, '__invoke')) {
141+
$array[1] = '__invoke';
142+
}
140143
return [$array[0], $array[1] ?? null];
141144
}
142145
if (is_array($handler) && isset($handler[0], $handler[1])) {

tests/Cases/Stub/DemoController.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313

1414
class DemoController
1515
{
16+
public function __invoke(DemoRequest $request)
17+
{
18+
return $request->all();
19+
}
20+
1621
public function signUp(DemoRequest $request)
1722
{
1823
return $request->all();

tests/Cases/ValidationMiddlewareTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use Hyperf\Translation\Translator;
3232
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
3333
use Hyperf\Validation\Middleware\ValidationMiddleware;
34+
use Hyperf\Validation\ValidationException;
3435
use Hyperf\Validation\ValidatorFactory;
3536
use HyperfTest\Validation\Cases\Stub\DemoController;
3637
use HyperfTest\Validation\Cases\Stub\DemoRequest;
@@ -169,4 +170,27 @@ public function createContainer()
169170

170171
return $container;
171172
}
173+
174+
public function testValidationOfInvocableControllerRoutingRequests()
175+
{
176+
$this->expectException(ValidationException::class);
177+
178+
$container = $this->createContainer();
179+
$factory = $container->get(DispatcherFactory::class);
180+
181+
$router = $factory->getRouter('http');
182+
$router->addRoute('POST', '/invokable', 'HyperfTest\Validation\Cases\Stub\DemoController');
183+
184+
$dispatcher = $factory->getDispatcher('http');
185+
$middleware = new ValidationMiddleware($container);
186+
$coreMiddleware = new CoreMiddleware($container, 'http');
187+
$handler = new HttpRequestHandler([$middleware], $coreMiddleware, $container);
188+
Context::set(ResponseInterface::class, new Response());
189+
190+
$request = (new Request('POST', new Uri('/invokable')))
191+
->withParsedBody(['username' => 'Hyperf']);
192+
$routes = $dispatcher->dispatch($request->getMethod(), $request->getUri()->getPath());
193+
$request = Context::set(ServerRequestInterface::class, $request->withAttribute(Dispatched::class, new Dispatched($routes)));
194+
$middleware->process($request, $handler);
195+
}
172196
}

0 commit comments

Comments
 (0)