-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: PSR-17 support for
PhpSession
authentication
Signed-off-by: Maximilian Bösing <2189546+boesing@users.noreply.github.com>
- Loading branch information
Showing
7 changed files
with
253 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Mezzio\Authentication\Session; | ||
|
||
use Mezzio\Authentication\Session\Response\CallableResponseFactoryDecorator; | ||
use Mezzio\Container\ResponseFactoryFactory; | ||
use Psr\Container\ContainerInterface; | ||
use Psr\Http\Message\ResponseFactoryInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Webmozart\Assert\Assert; | ||
|
||
/** | ||
* @internal | ||
* @deprecated Will be removed with v2.0.0 | ||
*/ | ||
trait Psr17ResponseFactoryTrait | ||
{ | ||
private function detectResponseFactory(ContainerInterface $container): ResponseFactoryInterface | ||
{ | ||
$psr17FactoryAvailable = $container->has(ResponseFactoryInterface::class); | ||
|
||
if (! $psr17FactoryAvailable) { | ||
return $this->createResponseFactoryFromDeprecatedCallable($container); | ||
} | ||
|
||
if ($this->doesConfigurationProvidesDedicatedResponseFactory($container)) { | ||
return $this->createResponseFactoryFromDeprecatedCallable($container); | ||
} | ||
|
||
$responseFactory = $container->get(ResponseFactoryInterface::class); | ||
Assert::isInstanceOf($responseFactory, ResponseFactoryInterface::class); | ||
return $responseFactory; | ||
} | ||
|
||
private function createResponseFactoryFromDeprecatedCallable( | ||
ContainerInterface $container | ||
): ResponseFactoryInterface { | ||
/** @var callable():ResponseInterface $responseFactory */ | ||
$responseFactory = $container->get(ResponseInterface::class); | ||
|
||
return new CallableResponseFactoryDecorator($responseFactory); | ||
} | ||
|
||
private function doesConfigurationProvidesDedicatedResponseFactory(ContainerInterface $container): bool | ||
{ | ||
if (! $container->has('config')) { | ||
return false; | ||
} | ||
|
||
$config = $container->get('config'); | ||
Assert::isArrayAccessible($config); | ||
$dependencies = $config['dependencies'] ?? []; | ||
Assert::isMap($dependencies); | ||
|
||
$delegators = $dependencies['delegators'] ?? []; | ||
$aliases = $dependencies['aliases'] ?? []; | ||
Assert::isArrayAccessible($delegators); | ||
Assert::isArrayAccessible($aliases); | ||
|
||
if (isset($delegators[ResponseInterface::class]) || isset($aliases[ResponseInterface::class])) { | ||
// Even tho, aliases could point to a different service, we assume that there is a dedicated factory | ||
// available. The alias resolving is not worth it. | ||
return true; | ||
} | ||
|
||
/** @psalm-suppress MixedAssignment */ | ||
$deprecatedResponseFactory = $dependencies['factories'][ResponseInterface::class] ?? null; | ||
|
||
return $deprecatedResponseFactory !== null && $deprecatedResponseFactory !== ResponseFactoryFactory::class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Mezzio\Authentication\Session\Response; | ||
|
||
use Psr\Http\Message\ResponseFactoryInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
/** | ||
* @internal | ||
* @deprecated Will be removed with v2.0.0 | ||
*/ | ||
final class CallableResponseFactoryDecorator implements ResponseFactoryInterface | ||
{ | ||
/** @var callable():ResponseInterface */ | ||
private $responseFactory; | ||
|
||
/** | ||
* @param callable():ResponseInterface $responseFactory | ||
*/ | ||
public function __construct(callable $responseFactory) | ||
{ | ||
$this->responseFactory = $responseFactory; | ||
} | ||
|
||
public function createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface | ||
{ | ||
return $this->getResponseFromCallable()->withStatus($code, $reasonPhrase); | ||
} | ||
|
||
public function getResponseFromCallable(): ResponseInterface | ||
{ | ||
return ($this->responseFactory)(); | ||
} | ||
} |
Oops, something went wrong.