Skip to content

Commit 329f9de

Browse files
authored
Merge pull request #662 from cakephp/2.next-more-dic
2.next: add ability to set custom container instance
2 parents a5dc587 + a258721 commit 329f9de

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

src/Middleware/AuthenticationMiddleware.php

Lines changed: 17 additions & 2 deletions
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 Cake\Core\InstanceConfigTrait;
2728
use InvalidArgumentException;
2829
use Laminas\Diactoros\Response;
@@ -63,15 +64,26 @@ class AuthenticationMiddleware implements MiddlewareInterface
6364
*/
6465
protected $subject;
6566

67+
/**
68+
* The container instance from the application
69+
*
70+
* @var \Cake\Core\ContainerInterface|null
71+
*/
72+
protected $container;
73+
6674
/**
6775
* Constructor
6876
*
6977
* @param \Authentication\AuthenticationServiceInterface|\Authentication\AuthenticationServiceProviderInterface $subject Authentication service or application instance.
7078
* @param array $config Array of configuration settings.
79+
* @param \Cake\Core\ContainerInterface|null $container The container instance from the application
7180
* @throws \InvalidArgumentException When invalid subject has been passed.
7281
*/
73-
public function __construct($subject, $config = [])
74-
{
82+
public function __construct(
83+
$subject,
84+
$config = [],
85+
?ContainerInterface $container = null
86+
) {
7587
$this->setConfig($config);
7688

7789
if (
@@ -89,6 +101,7 @@ public function __construct($subject, $config = [])
89101
}
90102

91103
$this->subject = $subject;
104+
$this->container = $container;
92105
}
93106

94107
/**
@@ -105,6 +118,8 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
105118
if ($this->subject instanceof ContainerApplicationInterface) {
106119
$container = $this->subject->getContainer();
107120
$container->add(AuthenticationService::class, $service);
121+
} elseif ($this->container) {
122+
$this->container->add(AuthenticationService::class, $service);
108123
}
109124

110125
try {

tests/TestCase/Middleware/AuthenticationMiddlewareTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Authentication\IdentityInterface;
2323
use Authentication\Middleware\AuthenticationMiddleware;
2424
use Authentication\Test\TestCase\AuthenticationTestCase as TestCase;
25+
use Cake\Core\Container;
2526
use Cake\Core\TestSuite\ContainerStubTrait;
2627
use Cake\Http\Response;
2728
use Cake\Http\ServerRequestFactory;
@@ -772,4 +773,25 @@ public function testMiddlewareInjectsServiceIntoDIC(): void
772773
$container = $this->application->getContainer();
773774
$this->assertInstanceOf(AuthenticationService::class, $container->get(AuthenticationService::class));
774775
}
776+
777+
public function testMiddlewareInjectsServiceIntoDICCustomContainerInstance(): void
778+
{
779+
$request = ServerRequestFactory::fromGlobals(
780+
['REQUEST_URI' => '/testpath'],
781+
[],
782+
['username' => 'mariano', 'password' => 'password']
783+
);
784+
$handler = new TestRequestHandler();
785+
786+
$provider = $this->createMock(AuthenticationServiceProviderInterface::class);
787+
$provider
788+
->method('getAuthenticationService')
789+
->willReturn($this->service);
790+
$container = new Container();
791+
792+
$middleware = new AuthenticationMiddleware($provider, [], $container);
793+
$middleware->process($request, $handler);
794+
795+
$this->assertInstanceOf(AuthenticationService::class, $container->get(AuthenticationService::class));
796+
}
775797
}

0 commit comments

Comments
 (0)