Skip to content

Commit 55330b5

Browse files
Merge pull request #665 from cakephp/3.x-dic-port-from-2.x
3.next: add ability to set custom container instance
2 parents cdbb253 + 67d3f82 commit 55330b5

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/Middleware/AuthenticationMiddleware.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Authentication\Authenticator\StatelessInterface;
2424
use Authentication\Authenticator\UnauthenticatedException;
2525
use Cake\Core\ContainerApplicationInterface;
26+
use Cake\Core\ContainerInterface;
2627
use Laminas\Diactoros\Response;
2728
use Laminas\Diactoros\Response\RedirectResponse;
2829
use Laminas\Diactoros\Stream;
@@ -43,16 +44,26 @@ class AuthenticationMiddleware implements MiddlewareInterface
4344
*/
4445
protected AuthenticationServiceInterface|AuthenticationServiceProviderInterface $subject;
4546

47+
/**
48+
* The container instance from the application
49+
*
50+
* @var \Cake\Core\ContainerInterface|null
51+
*/
52+
protected ?ContainerInterface $container;
53+
4654
/**
4755
* Constructor
4856
*
4957
* @param \Authentication\AuthenticationServiceInterface|\Authentication\AuthenticationServiceProviderInterface $subject Authentication service or application instance.
58+
* @param \Cake\Core\ContainerInterface|null $container The container instance from the application.
5059
* @throws \InvalidArgumentException When invalid subject has been passed.
5160
*/
5261
public function __construct(
53-
AuthenticationServiceInterface|AuthenticationServiceProviderInterface $subject
62+
AuthenticationServiceInterface|AuthenticationServiceProviderInterface $subject,
63+
?ContainerInterface $container = null
5464
) {
5565
$this->subject = $subject;
66+
$this->container = $container;
5667
}
5768

5869
/**
@@ -69,6 +80,8 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
6980
if ($this->subject instanceof ContainerApplicationInterface) {
7081
$container = $this->subject->getContainer();
7182
$container->add(AuthenticationService::class, $service);
83+
} elseif ($this->container) {
84+
$this->container->add(AuthenticationService::class, $service);
7285
}
7386

7487
try {

tests/TestCase/Middleware/AuthenticationMiddlewareTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Authentication\IdentityInterface;
2525
use Authentication\Middleware\AuthenticationMiddleware;
2626
use Authentication\Test\TestCase\AuthenticationTestCase as TestCase;
27+
use Cake\Core\Container;
2728
use Cake\Core\TestSuite\ContainerStubTrait;
2829
use Cake\Http\Response;
2930
use Cake\Http\ServerRequestFactory;
@@ -667,4 +668,25 @@ public function testMiddlewareInjectsServiceIntoDIC(): void
667668
$container = $this->application->getContainer();
668669
$this->assertInstanceOf(AuthenticationService::class, $container->get(AuthenticationService::class));
669670
}
671+
672+
public function testMiddlewareInjectsServiceIntoDICCustomContainerInstance(): void
673+
{
674+
$request = ServerRequestFactory::fromGlobals(
675+
['REQUEST_URI' => '/testpath'],
676+
[],
677+
['username' => 'mariano', 'password' => 'password']
678+
);
679+
$handler = new TestRequestHandler();
680+
681+
$provider = $this->createMock(AuthenticationServiceProviderInterface::class);
682+
$provider
683+
->method('getAuthenticationService')
684+
->willReturn($this->service);
685+
$container = new Container();
686+
687+
$middleware = new AuthenticationMiddleware($provider, $container);
688+
$middleware->process($request, $handler);
689+
690+
$this->assertInstanceOf(AuthenticationService::class, $container->get(AuthenticationService::class));
691+
}
670692
}

0 commit comments

Comments
 (0)