Skip to content

Commit

Permalink
Corrected code to actually work with psr-15 middleware pattern.
Browse files Browse the repository at this point in the history
  • Loading branch information
Danack committed Jun 14, 2023
1 parent 67ec814 commit 9479fed
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
33 changes: 22 additions & 11 deletions lib/Asm/Bridge/SlimSessionMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,34 @@ public function __construct(

public function process(Request $request, RequestHandlerInterface $handler): ResponseInterface
{
$session = $this->sessionManager->createSession($request);
$this->requestSessionStorage->store($session);
// $response = $next($request, $response);
$session = $this->sessionManager->openSessionFromCookie($request);

if ($session) {
$this->requestSessionStorage->store($session);
}

$response = $handler->handle($request);

$session->save();
$headers = $session->getHeaders(
\Asm\SessionManager::CACHE_PRIVATE,
'/'
// Session could have been opened inside request
$session = $this->requestSessionStorage->get();

if ($session) {
$session->save();
$headersArrays = $session->getHeaders(
\Asm\SessionManager::CACHE_PRIVATE,
'/'
// $domain = false,
// $secure = false,
// $httpOnly = true
);
);

foreach ($headersArrays as $nameAndValue) {
$name = $nameAndValue[0];
$value = $nameAndValue[1];

foreach ($headers as $key => $value) {
/** @var ResponseInterface $response */
$response = $response->withAddedHeader($key, $value);
/** @var ResponseInterface $response */
$response = $response->withAddedHeader($name, $value);
}
}

return $response;
Expand Down
5 changes: 4 additions & 1 deletion lib/Asm/Predis/PredisDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,10 @@ public function save(
$sessionLifeTime
);

if ($written !== true) {
/** @var $written \Predis\Response\Status */

if ($written->getPayload() !== 'OK') {
// TODO - handle this more gracefully...
throw new AsmException("Failed to save data", AsmException::IO_ERROR);
}
}
Expand Down
3 changes: 3 additions & 0 deletions lib/Asm/RequestSessionStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

use Asm\Session;


/**
* Allows an active session to be stored back from the Asm middleware
* so it can be accessed by your application.
*/
interface RequestSessionStorage
{
public function store(Session $session);

public function get(): Session|null;
}

0 comments on commit 9479fed

Please sign in to comment.