PHP-DI / Slim-Bridge

PHP-DI integration with the Slim framework
http://php-di.org/doc/frameworks/slim.html
MIT License
176 stars 38 forks source link

injecting a middleware as callable array (non-static) #91

Open webmaster777 opened 3 months ago

webmaster777 commented 3 months ago

Just a stub for now, I'll try to expand this later.

Bit related: https://github.com/slimphp/Slim/issues/2780

I was trying to do this:

    $app->get('/register-platform', [RegisterPlatform::class, 'getRegister'])
      ->add([RegisterPlatform::class, 'assertValidRegistrationRequest']);

assertValidRegistrationRequest is a public (non-static) method with a valid middleware signature:

public function assertValidRegistrationRequest(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface

This however results in a

Uncaught RuntimeException: A middleware must be an object/class name referencing an implementation of MiddlewareInterface or a callable with a matching signature. in /app/vendor/slim/slim/Slim/MiddlewareDispatcher.php:92

My current workaround/solution is:

$app->get('/register-platform', [RegisterPlatform::class, 'getRegister'])
      ->add($callableResolver->resolve([RegisterPlatform::class, 'assertValidRegistrationRequest']));

where $callableResolver is of type DI\Bridge\Slim\CallableResolver. This however feels very verbose.

// or

$app->get('/register-platform', [RegisterPlatform::class, 'getRegister'])
      ->add(RegisterPlatform::class . '::assertValidRegistrationRequest');

but this is ugly.

Is it too extreme to override the MiddlewareDispatcher here to allow these notations? Otherwise the problem needs to be fixed in Slim's MiddlewareDispatcher to defer all middleware notations to the CallableResolver instead of only string variants.