Skip to content

Initial commit of RFC alpha implementation.#1

Draft
tyrsson wants to merge 1 commit into
php-db:0.1.xfrom
tyrsson:initial-commit
Draft

Initial commit of RFC alpha implementation.#1
tyrsson wants to merge 1 commit into
php-db:0.1.xfrom
tyrsson:initial-commit

Conversation

@tyrsson
Copy link
Copy Markdown
Member

@tyrsson tyrsson commented May 26, 2026

Signed-off-by: Joey Smith jsmith@webinertia.net

Q A
Documentation yes
Bugfix no
BC Break no
New Feature yes
RFC yes
QA no
House Keeping no

Description

This PR is exploratory. This is a base working implementation based around phpdb-mysql for discussion on how to best abstract for all drivers.

Signed-off-by: Joey Smith <jsmith@webinertia.net>
@tyrsson tyrsson self-assigned this May 26, 2026
@tyrsson tyrsson marked this pull request as draft May 26, 2026 06:52
@tyrsson tyrsson requested review from settermjd and simon-mundy May 26, 2026 06:52
namespace PhpDb\Session\Container;

use PhpDb\Adapter\AdapterInterface;
use PhpDb\Session\DbSessionHandler;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is PhpDb taking responsibility for sessions? Would that not be more in the realm of an adapter?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Laminas is removing "save handlers" for laminas db from the session packages. Mezzio still technically owns Sessions, mezzio-session exposes the contracts that phpdb-mezzio-session is built upon. This is the exact use case described in the mezzio-session docs.


return PhpDbSessionPersistence::fromConfigArray(
$container->get(AdapterInterface::class),
isset($config['session']) && is_array($config['session']) ? $config['session'] : []
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit late, here, so bear with me, but would a null coalescing operator simplify this logic?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, not completely, no.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably :) This is literally the first iteration of the code.

Comment thread src/Sql/Insert.php
Comment on lines +26 to +30
'INSERT INTO %1$s (%2$s) VALUES (%3$s)'
. ' ON DUPLICATE KEY UPDATE'
. ' payload = VALUES(payload)'
. ', expires_at = VALUES(expires_at)'
. ', modified_at = VALUES(modified_at)',
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about using a HEREDOC string instead for slightly greater readability? Also, if you're using MySQL-specific syntax, would this break in any of the other supported databases?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I'm aware that it will break. That is the primary purpose of this RFC. So, we can decide how best to abstract it for all drivers.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add a PR to allow Upsert as a new PhpDb\Sql class and then add PR to each driver to decorate it as required. This will allow a more generic call here.

Copy link
Copy Markdown

@settermjd settermjd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my first run through, it seems to be fine. I've left a few comments for your consideration. I hope they're of some help.

@tyrsson
Copy link
Copy Markdown
Member Author

tyrsson commented May 27, 2026

Oh, they'll help for sure 😁

Comment thread src/ConfigProvider.php
// PhpSessionPersistenceDecorator is available for sync-only environments
// but is NOT aliased here. Swap the alias below to use it instead.
SessionPersistenceInterface::class => PhpDbSessionPersistence::class,
],
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any plans for short 'key' aliases here? 'db' for example?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The sync-only stuff is actually just a left-over comment. It's not relevant anymore as it has been dropped.

But, generally speaking. No. I despise string keys because they provide today's tooling no further information. IE, no deprecation notices etc.

Comment thread src/DbSessionHandler.php

public function __construct(AdapterInterface $adapter)
{
$this->sql = new Sql($adapter, 'session');
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is an alpha but I'd like to see a config passed through with 'session' as the default table name string.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, I'll get around to it in the next revision or two. This is literally just what I built for personal use in a current project so...

Comment thread src/DbSessionHandler.php
public function read(string $id): string|false
{
$select = $this->sql->select()
->columns(['payload'])
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here - the sane 'payload' column as default but also configurable.

Comment thread src/DbSessionHandler.php
{
$select = $this->sql->select()
->columns(['payload'])
->where(['id' => $id]);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And the PK could also be session_id etc...

Comment thread src/DbSessionHandler.php
$now = date('Y-m-d H:i:s');

$this->sql->prepareStatementForSqlObject(
(new Insert('session'))
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new Upsert :)

}

// No ID means a new session was created but never written to.
if ($id === '') {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reckon an $id should be NULL instead of an empty string if it's not yet available

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants