Skip to content

Commit

Permalink
Add auto_regenerate option to CacheSessionPersistence
Browse files Browse the repository at this point in the history
Signed-off-by: Michal Izewski <[email protected]>
  • Loading branch information
Michal Izewski committed May 7, 2024
1 parent 3346c3a commit a09db68
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
8 changes: 6 additions & 2 deletions docs/book/v1/manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ The following details the constructor of the `Mezzio\Session\Cache\CacheSessionP
* @param string $cookieSameSite The same-site rule to apply to the persisted
* cookie. Options include "Lax", "Strict", and "None".
* Available since 1.4.0
*
* @param bool $autoRegenerate Whether or not the session ID should be
* regenerated on session data changes
* Available since 1.13.0
*
* @todo reorder these arguments so they make more sense and are in an
* order of importance
*/
Expand All @@ -51,7 +54,8 @@ public function __construct(
string $cookieDomain = null,
bool $cookieSecure = false,
bool $cookieHttpOnly = false,
string $cookieSameSite = 'Lax'
string $cookieSameSite = 'Lax',
bool $autoRegenerate = true,
) {
```

Expand Down
12 changes: 9 additions & 3 deletions src/CacheSessionPersistence.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class CacheSessionPersistence implements SessionPersistenceInterface
private CacheItemPoolInterface $cache;

private bool $persistent;
private bool $autoRegenerate;

/**
* Prepare session cache and default HTTP caching headers.
Expand Down Expand Up @@ -67,6 +68,8 @@ class CacheSessionPersistence implements SessionPersistenceInterface
* be accessed by client-side apis.
* @param string $cookieSameSite The same-site rule to apply to the persisted
* cookie. Options include "Lax", "Strict", and "None".
* @param bool $autoRegenerate Whether or not the session ID should be
* regenerated on session data changes
* @todo reorder the constructor arguments
*/
public function __construct(
Expand All @@ -80,7 +83,8 @@ public function __construct(
?string $cookieDomain = null,
bool $cookieSecure = false,
bool $cookieHttpOnly = false,
string $cookieSameSite = 'Lax'
string $cookieSameSite = 'Lax',
bool $autoRegenerate = true
) {
$this->cache = $cache;

Expand Down Expand Up @@ -112,6 +116,8 @@ public function __construct(
: $this->getLastModified();

$this->persistent = $persistent;

$this->autoRegenerate = $autoRegenerate;
}

public function initializeSessionFromRequest(ServerRequestInterface $request): SessionInterface
Expand Down Expand Up @@ -139,8 +145,8 @@ public function persistSession(SessionInterface $session, ResponseInterface $res
// Regenerate the session if:
// - we have no session identifier
// - the session is marked as regenerated
// - the session has changed (data is different)
if ('' === $id || $session->isRegenerated() || $session->hasChanged()) {
// - the session has changed (data is different) and autoRegenerate is turned off in the configuration
if ('' === $id || $session->isRegenerated() || ($this->autoRegenerate && $session->hasChanged())) {
$id = $this->regenerateSession($id);
}

Expand Down
4 changes: 3 additions & 1 deletion src/CacheSessionPersistenceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function __invoke(ContainerInterface $container)
$cacheExpire = $config['cache_expire'] ?? 10800;
$lastModified = $config['last_modified'] ?? null;
$persistent = $config['persistent'] ?? false;
$autoRegenerate = $config['auto_regenerate'] ?? true;

Check failure on line 37 in src/CacheSessionPersistenceFactory.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (Psalm [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1, ...

MixedAssignment

src/CacheSessionPersistenceFactory.php:37:9: MixedAssignment: Unable to determine the type that $autoRegenerate is being assigned to (see https://psalm.dev/032)

Check failure on line 37 in src/CacheSessionPersistenceFactory.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (Psalm [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1, ...

MixedArrayAccess

src/CacheSessionPersistenceFactory.php:37:27: MixedArrayAccess: Cannot access array value on mixed variable $config (see https://psalm.dev/051)

return new CacheSessionPersistence(
$container->get($cacheService),
Expand All @@ -46,7 +47,8 @@ public function __invoke(ContainerInterface $container)
$cookieDomain,
$cookieSecure,
$cookieHttpOnly,
$cookieSameSite
$cookieSameSite,
$autoRegenerate

Check failure on line 51 in src/CacheSessionPersistenceFactory.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (Psalm [8.1, locked], ubuntu-latest, laminas/laminas-continuous-integration-action@v1, ...

MixedArgument

src/CacheSessionPersistenceFactory.php:51:13: MixedArgument: Argument 12 of Mezzio\Session\Cache\CacheSessionPersistence::__construct cannot be mixed|true, expecting bool (see https://psalm.dev/030)
);
}
}

0 comments on commit a09db68

Please sign in to comment.