Skip to content

Commit

Permalink
Add the container to the middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamison Bryant authored and ADmad committed Mar 25, 2024
1 parent cdbb253 commit abb32a2
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/Middleware/AuthenticationMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Authentication\Authenticator\StatelessInterface;
use Authentication\Authenticator\UnauthenticatedException;
use Cake\Core\ContainerApplicationInterface;
use Cake\Core\ContainerInterface;
use Laminas\Diactoros\Response;
use Laminas\Diactoros\Response\RedirectResponse;
use Laminas\Diactoros\Stream;
Expand All @@ -43,16 +44,26 @@ class AuthenticationMiddleware implements MiddlewareInterface
*/
protected AuthenticationServiceInterface|AuthenticationServiceProviderInterface $subject;

/**
* The container instance from the application
*
* @var \Cake\Core\ContainerInterface|null
*/
protected ?ContainerInterface $container;

/**
* Constructor
*
* @param \Authentication\AuthenticationServiceInterface|\Authentication\AuthenticationServiceProviderInterface $subject Authentication service or application instance.
* @param \Cake\Core\ContainerInterface|null $container The container instance from the application.
* @throws \InvalidArgumentException When invalid subject has been passed.
*/
public function __construct(
AuthenticationServiceInterface|AuthenticationServiceProviderInterface $subject
AuthenticationServiceInterface|AuthenticationServiceProviderInterface $subject,
?ContainerInterface $container = null
) {
$this->subject = $subject;
$this->container = $container;
}

/**
Expand All @@ -69,6 +80,8 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
if ($this->subject instanceof ContainerApplicationInterface) {
$container = $this->subject->getContainer();
$container->add(AuthenticationService::class, $service);
} elseif ($this->container) {
$this->container->add(AuthenticationService::class, $service);
}

try {
Expand Down

0 comments on commit abb32a2

Please sign in to comment.