Skip to content

Commit 96deaab

Browse files
authored
Merge pull request #278 from cakephp/2.next-more-dic
2.next: more DIC
2 parents bc0e956 + 183e7f9 commit 96deaab

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/Middleware/AuthorizationMiddleware.php

+17-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Authorization\IdentityInterface;
2828
use Authorization\Middleware\UnauthorizedHandler\UnauthorizedHandlerTrait;
2929
use Cake\Core\ContainerApplicationInterface;
30+
use Cake\Core\ContainerInterface;
3031
use Cake\Core\InstanceConfigTrait;
3132
use InvalidArgumentException;
3233
use Psr\Http\Message\ResponseInterface;
@@ -73,15 +74,26 @@ class AuthorizationMiddleware implements MiddlewareInterface
7374
*/
7475
protected $subject;
7576

77+
/**
78+
* The container instance from the application
79+
*
80+
* @var \Cake\Core\ContainerInterface|null
81+
*/
82+
protected $container;
83+
7684
/**
7785
* Constructor.
7886
*
7987
* @param \Authorization\AuthorizationServiceInterface|\Authorization\AuthorizationServiceProviderInterface $subject Authorization service or provider instance.
8088
* @param array $config Config array.
89+
* @param \Cake\Core\ContainerInterface|null $container The container instance from the application
8190
* @throws \InvalidArgumentException
8291
*/
83-
public function __construct($subject, array $config = [])
84-
{
92+
public function __construct(
93+
$subject,
94+
array $config = [],
95+
?ContainerInterface $container = null
96+
) {
8597
/** @psalm-suppress DocblockTypeContradiction */
8698
if (
8799
!$subject instanceof AuthorizationServiceInterface &&
@@ -104,6 +116,7 @@ public function __construct($subject, array $config = [])
104116
}
105117

106118
$this->subject = $subject;
119+
$this->container = $container;
107120
$this->setConfig($config);
108121
}
109122

@@ -122,6 +135,8 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
122135
if ($this->subject instanceof ContainerApplicationInterface) {
123136
$container = $this->subject->getContainer();
124137
$container->add(AuthorizationService::class, $service);
138+
} elseif ($this->container) {
139+
$this->container->add(AuthorizationService::class, $service);
125140
}
126141

127142
$attribute = $this->getConfig('identityAttribute');

tests/TestCase/Middleware/AuthorizationMiddlewareTest.php

+26
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Authorization\IdentityDecorator;
2525
use Authorization\IdentityInterface;
2626
use Authorization\Middleware\AuthorizationMiddleware;
27+
use Cake\Core\Container;
2728
use Cake\Http\Response;
2829
use Cake\Http\ServerRequest;
2930
use Cake\Http\ServerRequestFactory;
@@ -302,4 +303,29 @@ public function testMiddlewareInjectsServiceIntoDIC()
302303
$container = $application->getContainer();
303304
$this->assertInstanceOf(AuthorizationService::class, $container->get(AuthorizationService::class));
304305
}
306+
307+
public function testMiddlewareInjectsServiceIntoDICViaCustomContainerInstance()
308+
{
309+
$request = ServerRequestFactory::fromGlobals(
310+
['REQUEST_URI' => '/testpath'],
311+
[],
312+
['username' => 'mariano', 'password' => 'password']
313+
);
314+
$handler = new TestRequestHandler();
315+
316+
$service = $this->createMock(AuthorizationServiceInterface::class);
317+
$provider = $this->createMock(AuthorizationServiceProviderInterface::class);
318+
$provider
319+
->expects($this->once())
320+
->method('getAuthorizationService')
321+
->with($this->isInstanceOf(ServerRequestInterface::class))
322+
->willReturn($service);
323+
324+
$container = new Container();
325+
326+
$middleware = new AuthorizationMiddleware($provider, ['requireAuthorizationCheck' => false], $container);
327+
$middleware->process($request, $handler);
328+
329+
$this->assertEquals($service, $container->get(AuthorizationService::class));
330+
}
305331
}

0 commit comments

Comments
 (0)