Files
php-qml/framework/qml/src/BackendConnection.h

124 lines
3.7 KiB
C
Raw Normal View History

#pragma once
Phase 2 sub-commit 3: full Update Semantics + ReactiveListModel + AppShell BackendConnection's ConnectionState enum is now Connecting / Online / Reconnecting / Offline (PLAN.md §5). The probe loop tracks the first failure since the last Online and transitions to Reconnecting on any failed probe, then to Offline once the configurable threshold (30 s default) is exceeded. The Error state is gone; Reconnecting + the exposed `error` string subsume its UI role. ReactiveListModel is the headline QML type: - QAbstractListModel that GETs `baseUrl + source` for an initial JSON array and then keeps in sync via an internal MercureClient subscribed to `topic`. - Role names are derived dynamically from the first row's keys plus an internal `pending` boolean role used by optimistic mutations. - Diff application: upsert (insert-or-update), delete, replace; gap detection via the envelope `version` field with auto re-fetch. - `invoke(method, path, body, optimistic)` is the optimistic command primitive. Generates an Idempotency-Key, applies the local diff, POST/PATCH/DELETEs with that key, and resolves on the matching Mercure echo (correlation-key matched in ModelPublisher's envelope). Rolls back and emits commandFailed on 4xx/5xx, commandTimedOut after 10 s without an echo. Phase 4 packaging will surface configuration for the timeout. AppShell.qml is the optional convenience root: - Reads BackendConnection.connectionState. - Reconnecting → top banner. - Offline → modal overlay with the error string and a Retry button (calls BackendConnection.restart()). - Wraps user content via `default property alias content`. Apps that want full chrome control can skip AppShell entirely; the skeleton's Main.qml keeps its own status display for demonstration and is unaffected. ReactiveObject (single-entity twin of ReactiveListModel) is intentionally deferred — same envelope handling, smaller surface; will land in Phase 2 follow-up or Phase 3 alongside the todo example. Cursor pagination is similarly deferred (the Phase 2 done criterion uses small lists). Smoke tested: /healthz + /api/ping round-trip cleanly, zero Mercure 401s, clean shutdown. composer quality stays green (16 tests, 45 assertions). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 02:40:12 +02:00
#include <QElapsedTimer>
#include <QObject>
Phase 4a sub-commit 1: bundled-mode startup in BackendConnection Auto-detected on construction: - BRIDGE_URL env set → dev mode (today's behaviour, unchanged). - BRIDGE_URL unset → bundled mode: BackendConnection now 1. Resolves the user app data dir (QStandardPaths::AppDataLocation, ~/.local/share/<org>/<app> on Linux) and ensures var/, var/log/, var/cache/ exist there. 2. Generates a per-session 32-byte URL-safe token and a 48-byte Mercure JWT secret. 3. Runs `frankenphp php-cli bin/console doctrine:migrations:migrate -n` against the user's DATABASE_URL with a 60s timeout. 4. Spawns FrankenPHP via QProcess with BRIDGE_TOKEN/MERCURE_*/PORT in the env, prctl(PR_SET_PDEATHSIG, SIGTERM) on the child, and a supervisor that re-spawns up to 5 times on unexpected exit. Each restart fires tokenRotated(newToken). Path resolution defaults to applicationDirPath() + bin/frankenphp, applicationDirPath() + symfony, applicationDirPath() + Caddyfile, with both `/../share/<app>/...` and `/../usr/share/<app>/...` fallbacks for AppImage-style layouts. All three are overridable via BRIDGE_FRANKENPHP_BIN / BRIDGE_SYMFONY_DIR / BRIDGE_CADDYFILE env vars. Caddyfiles in skeleton + example now use {$VAR:default} substitution for PORT and the Mercure JWT keys, so the same Caddyfile works in both modes. Dev defaults match symfony/.env. restart() in bundled mode re-spawns the child (resets the supervisor counter); in dev mode it stays a probe-only no-op. Smoke-tested locally with `BRIDGE_FRANKENPHP_BIN=… BRIDGE_SYMFONY_DIR=… BRIDGE_CADDYFILE=… ./build/qml/todo` (no BRIDGE_URL): bundled mode created ~/.local/share/php-qml/todo/var/data.sqlite, ran the migration, spawned FrankenPHP, served /healthz, accepted a POST /api/todos with the per-session bearer. Dev mode (`make dev`) still works unchanged. Includes a `phpqml.bridge.bundled` Q_LOGGING_CATEGORY so failures surface to the user; enable with QT_LOGGING_RULES='phpqml.bridge.bundled.*=true'. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 17:00:13 +02:00
#include <QProcess>
#include <QString>
#include <QtQmlIntegration>
class QNetworkAccessManager;
class QNetworkReply;
class QTimer;
class QQmlEngine;
class QJSEngine;
namespace PhpQml::Bridge {
/// Owns the backend lifecycle and exposes its health to QML.
///
Phase 4a sub-commit 1: bundled-mode startup in BackendConnection Auto-detected on construction: - BRIDGE_URL env set → dev mode (today's behaviour, unchanged). - BRIDGE_URL unset → bundled mode: BackendConnection now 1. Resolves the user app data dir (QStandardPaths::AppDataLocation, ~/.local/share/<org>/<app> on Linux) and ensures var/, var/log/, var/cache/ exist there. 2. Generates a per-session 32-byte URL-safe token and a 48-byte Mercure JWT secret. 3. Runs `frankenphp php-cli bin/console doctrine:migrations:migrate -n` against the user's DATABASE_URL with a 60s timeout. 4. Spawns FrankenPHP via QProcess with BRIDGE_TOKEN/MERCURE_*/PORT in the env, prctl(PR_SET_PDEATHSIG, SIGTERM) on the child, and a supervisor that re-spawns up to 5 times on unexpected exit. Each restart fires tokenRotated(newToken). Path resolution defaults to applicationDirPath() + bin/frankenphp, applicationDirPath() + symfony, applicationDirPath() + Caddyfile, with both `/../share/<app>/...` and `/../usr/share/<app>/...` fallbacks for AppImage-style layouts. All three are overridable via BRIDGE_FRANKENPHP_BIN / BRIDGE_SYMFONY_DIR / BRIDGE_CADDYFILE env vars. Caddyfiles in skeleton + example now use {$VAR:default} substitution for PORT and the Mercure JWT keys, so the same Caddyfile works in both modes. Dev defaults match symfony/.env. restart() in bundled mode re-spawns the child (resets the supervisor counter); in dev mode it stays a probe-only no-op. Smoke-tested locally with `BRIDGE_FRANKENPHP_BIN=… BRIDGE_SYMFONY_DIR=… BRIDGE_CADDYFILE=… ./build/qml/todo` (no BRIDGE_URL): bundled mode created ~/.local/share/php-qml/todo/var/data.sqlite, ran the migration, spawned FrankenPHP, served /healthz, accepted a POST /api/todos with the per-session bearer. Dev mode (`make dev`) still works unchanged. Includes a `phpqml.bridge.bundled` Q_LOGGING_CATEGORY so failures surface to the user; enable with QT_LOGGING_RULES='phpqml.bridge.bundled.*=true'. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 17:00:13 +02:00
/// Mode is auto-detected on construction:
/// - `BRIDGE_URL` env set → **dev mode**: connect to a developer-managed
/// backend at that URL.
/// - `BRIDGE_URL` unset → **bundled mode** (Phase 4a): spawn the embedded
/// `frankenphp` next to the host binary, generate a per-session bearer
/// token, run first-launch migrations, and supervise the child.
///
/// See PLAN.md §3 (Run modes), §7 (BackendConnection), §13 Phase 4a.
class BackendConnection : public QObject
{
Q_OBJECT
QML_ELEMENT
QML_SINGLETON
Q_PROPERTY(Mode mode READ mode CONSTANT)
Phase 4a sub-commit 1: bundled-mode startup in BackendConnection Auto-detected on construction: - BRIDGE_URL env set → dev mode (today's behaviour, unchanged). - BRIDGE_URL unset → bundled mode: BackendConnection now 1. Resolves the user app data dir (QStandardPaths::AppDataLocation, ~/.local/share/<org>/<app> on Linux) and ensures var/, var/log/, var/cache/ exist there. 2. Generates a per-session 32-byte URL-safe token and a 48-byte Mercure JWT secret. 3. Runs `frankenphp php-cli bin/console doctrine:migrations:migrate -n` against the user's DATABASE_URL with a 60s timeout. 4. Spawns FrankenPHP via QProcess with BRIDGE_TOKEN/MERCURE_*/PORT in the env, prctl(PR_SET_PDEATHSIG, SIGTERM) on the child, and a supervisor that re-spawns up to 5 times on unexpected exit. Each restart fires tokenRotated(newToken). Path resolution defaults to applicationDirPath() + bin/frankenphp, applicationDirPath() + symfony, applicationDirPath() + Caddyfile, with both `/../share/<app>/...` and `/../usr/share/<app>/...` fallbacks for AppImage-style layouts. All three are overridable via BRIDGE_FRANKENPHP_BIN / BRIDGE_SYMFONY_DIR / BRIDGE_CADDYFILE env vars. Caddyfiles in skeleton + example now use {$VAR:default} substitution for PORT and the Mercure JWT keys, so the same Caddyfile works in both modes. Dev defaults match symfony/.env. restart() in bundled mode re-spawns the child (resets the supervisor counter); in dev mode it stays a probe-only no-op. Smoke-tested locally with `BRIDGE_FRANKENPHP_BIN=… BRIDGE_SYMFONY_DIR=… BRIDGE_CADDYFILE=… ./build/qml/todo` (no BRIDGE_URL): bundled mode created ~/.local/share/php-qml/todo/var/data.sqlite, ran the migration, spawned FrankenPHP, served /healthz, accepted a POST /api/todos with the per-session bearer. Dev mode (`make dev`) still works unchanged. Includes a `phpqml.bridge.bundled` Q_LOGGING_CATEGORY so failures surface to the user; enable with QT_LOGGING_RULES='phpqml.bridge.bundled.*=true'. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 17:00:13 +02:00
Q_PROPERTY(QString url READ url NOTIFY urlChanged)
Q_PROPERTY(QString token READ token NOTIFY tokenChanged)
Q_PROPERTY(ConnectionState connectionState READ connectionState NOTIFY connectionStateChanged)
Q_PROPERTY(QString error READ error NOTIFY errorChanged)
public:
enum class Mode {
Dev,
Phase 4a sub-commit 1: bundled-mode startup in BackendConnection Auto-detected on construction: - BRIDGE_URL env set → dev mode (today's behaviour, unchanged). - BRIDGE_URL unset → bundled mode: BackendConnection now 1. Resolves the user app data dir (QStandardPaths::AppDataLocation, ~/.local/share/<org>/<app> on Linux) and ensures var/, var/log/, var/cache/ exist there. 2. Generates a per-session 32-byte URL-safe token and a 48-byte Mercure JWT secret. 3. Runs `frankenphp php-cli bin/console doctrine:migrations:migrate -n` against the user's DATABASE_URL with a 60s timeout. 4. Spawns FrankenPHP via QProcess with BRIDGE_TOKEN/MERCURE_*/PORT in the env, prctl(PR_SET_PDEATHSIG, SIGTERM) on the child, and a supervisor that re-spawns up to 5 times on unexpected exit. Each restart fires tokenRotated(newToken). Path resolution defaults to applicationDirPath() + bin/frankenphp, applicationDirPath() + symfony, applicationDirPath() + Caddyfile, with both `/../share/<app>/...` and `/../usr/share/<app>/...` fallbacks for AppImage-style layouts. All three are overridable via BRIDGE_FRANKENPHP_BIN / BRIDGE_SYMFONY_DIR / BRIDGE_CADDYFILE env vars. Caddyfiles in skeleton + example now use {$VAR:default} substitution for PORT and the Mercure JWT keys, so the same Caddyfile works in both modes. Dev defaults match symfony/.env. restart() in bundled mode re-spawns the child (resets the supervisor counter); in dev mode it stays a probe-only no-op. Smoke-tested locally with `BRIDGE_FRANKENPHP_BIN=… BRIDGE_SYMFONY_DIR=… BRIDGE_CADDYFILE=… ./build/qml/todo` (no BRIDGE_URL): bundled mode created ~/.local/share/php-qml/todo/var/data.sqlite, ran the migration, spawned FrankenPHP, served /healthz, accepted a POST /api/todos with the per-session bearer. Dev mode (`make dev`) still works unchanged. Includes a `phpqml.bridge.bundled` Q_LOGGING_CATEGORY so failures surface to the user; enable with QT_LOGGING_RULES='phpqml.bridge.bundled.*=true'. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 17:00:13 +02:00
Bundled,
};
Q_ENUM(Mode)
Phase 2 sub-commit 3: full Update Semantics + ReactiveListModel + AppShell BackendConnection's ConnectionState enum is now Connecting / Online / Reconnecting / Offline (PLAN.md §5). The probe loop tracks the first failure since the last Online and transitions to Reconnecting on any failed probe, then to Offline once the configurable threshold (30 s default) is exceeded. The Error state is gone; Reconnecting + the exposed `error` string subsume its UI role. ReactiveListModel is the headline QML type: - QAbstractListModel that GETs `baseUrl + source` for an initial JSON array and then keeps in sync via an internal MercureClient subscribed to `topic`. - Role names are derived dynamically from the first row's keys plus an internal `pending` boolean role used by optimistic mutations. - Diff application: upsert (insert-or-update), delete, replace; gap detection via the envelope `version` field with auto re-fetch. - `invoke(method, path, body, optimistic)` is the optimistic command primitive. Generates an Idempotency-Key, applies the local diff, POST/PATCH/DELETEs with that key, and resolves on the matching Mercure echo (correlation-key matched in ModelPublisher's envelope). Rolls back and emits commandFailed on 4xx/5xx, commandTimedOut after 10 s without an echo. Phase 4 packaging will surface configuration for the timeout. AppShell.qml is the optional convenience root: - Reads BackendConnection.connectionState. - Reconnecting → top banner. - Offline → modal overlay with the error string and a Retry button (calls BackendConnection.restart()). - Wraps user content via `default property alias content`. Apps that want full chrome control can skip AppShell entirely; the skeleton's Main.qml keeps its own status display for demonstration and is unaffected. ReactiveObject (single-entity twin of ReactiveListModel) is intentionally deferred — same envelope handling, smaller surface; will land in Phase 2 follow-up or Phase 3 alongside the todo example. Cursor pagination is similarly deferred (the Phase 2 done criterion uses small lists). Smoke tested: /healthz + /api/ping round-trip cleanly, zero Mercure 401s, clean shutdown. composer quality stays green (16 tests, 45 assertions). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 02:40:12 +02:00
/// Full Update Semantics enum (PLAN.md §5).
enum class ConnectionState {
Connecting,
Online,
Phase 2 sub-commit 3: full Update Semantics + ReactiveListModel + AppShell BackendConnection's ConnectionState enum is now Connecting / Online / Reconnecting / Offline (PLAN.md §5). The probe loop tracks the first failure since the last Online and transitions to Reconnecting on any failed probe, then to Offline once the configurable threshold (30 s default) is exceeded. The Error state is gone; Reconnecting + the exposed `error` string subsume its UI role. ReactiveListModel is the headline QML type: - QAbstractListModel that GETs `baseUrl + source` for an initial JSON array and then keeps in sync via an internal MercureClient subscribed to `topic`. - Role names are derived dynamically from the first row's keys plus an internal `pending` boolean role used by optimistic mutations. - Diff application: upsert (insert-or-update), delete, replace; gap detection via the envelope `version` field with auto re-fetch. - `invoke(method, path, body, optimistic)` is the optimistic command primitive. Generates an Idempotency-Key, applies the local diff, POST/PATCH/DELETEs with that key, and resolves on the matching Mercure echo (correlation-key matched in ModelPublisher's envelope). Rolls back and emits commandFailed on 4xx/5xx, commandTimedOut after 10 s without an echo. Phase 4 packaging will surface configuration for the timeout. AppShell.qml is the optional convenience root: - Reads BackendConnection.connectionState. - Reconnecting → top banner. - Offline → modal overlay with the error string and a Retry button (calls BackendConnection.restart()). - Wraps user content via `default property alias content`. Apps that want full chrome control can skip AppShell entirely; the skeleton's Main.qml keeps its own status display for demonstration and is unaffected. ReactiveObject (single-entity twin of ReactiveListModel) is intentionally deferred — same envelope handling, smaller surface; will land in Phase 2 follow-up or Phase 3 alongside the todo example. Cursor pagination is similarly deferred (the Phase 2 done criterion uses small lists). Smoke tested: /healthz + /api/ping round-trip cleanly, zero Mercure 401s, clean shutdown. composer quality stays green (16 tests, 45 assertions). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 02:40:12 +02:00
Reconnecting,
Offline,
};
Q_ENUM(ConnectionState)
explicit BackendConnection(QObject* parent = nullptr);
~BackendConnection() override;
static BackendConnection* create(QQmlEngine* engine, QJSEngine*);
Mode mode() const noexcept { return m_mode; }
QString url() const { return m_url; }
QString token() const { return m_token; }
ConnectionState connectionState() const noexcept { return m_state; }
QString error() const { return m_error; }
Phase 4a sub-commit 1: bundled-mode startup in BackendConnection Auto-detected on construction: - BRIDGE_URL env set → dev mode (today's behaviour, unchanged). - BRIDGE_URL unset → bundled mode: BackendConnection now 1. Resolves the user app data dir (QStandardPaths::AppDataLocation, ~/.local/share/<org>/<app> on Linux) and ensures var/, var/log/, var/cache/ exist there. 2. Generates a per-session 32-byte URL-safe token and a 48-byte Mercure JWT secret. 3. Runs `frankenphp php-cli bin/console doctrine:migrations:migrate -n` against the user's DATABASE_URL with a 60s timeout. 4. Spawns FrankenPHP via QProcess with BRIDGE_TOKEN/MERCURE_*/PORT in the env, prctl(PR_SET_PDEATHSIG, SIGTERM) on the child, and a supervisor that re-spawns up to 5 times on unexpected exit. Each restart fires tokenRotated(newToken). Path resolution defaults to applicationDirPath() + bin/frankenphp, applicationDirPath() + symfony, applicationDirPath() + Caddyfile, with both `/../share/<app>/...` and `/../usr/share/<app>/...` fallbacks for AppImage-style layouts. All three are overridable via BRIDGE_FRANKENPHP_BIN / BRIDGE_SYMFONY_DIR / BRIDGE_CADDYFILE env vars. Caddyfiles in skeleton + example now use {$VAR:default} substitution for PORT and the Mercure JWT keys, so the same Caddyfile works in both modes. Dev defaults match symfony/.env. restart() in bundled mode re-spawns the child (resets the supervisor counter); in dev mode it stays a probe-only no-op. Smoke-tested locally with `BRIDGE_FRANKENPHP_BIN=… BRIDGE_SYMFONY_DIR=… BRIDGE_CADDYFILE=… ./build/qml/todo` (no BRIDGE_URL): bundled mode created ~/.local/share/php-qml/todo/var/data.sqlite, ran the migration, spawned FrankenPHP, served /healthz, accepted a POST /api/todos with the per-session bearer. Dev mode (`make dev`) still works unchanged. Includes a `phpqml.bridge.bundled` Q_LOGGING_CATEGORY so failures surface to the user; enable with QT_LOGGING_RULES='phpqml.bridge.bundled.*=true'. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 17:00:13 +02:00
/// Bundled mode: re-spawn the FrankenPHP child (e.g. after the user
/// hits Retry on the Offline overlay). Dev mode: re-probe.
Q_INVOKABLE void restart();
signals:
Phase 4a sub-commit 1: bundled-mode startup in BackendConnection Auto-detected on construction: - BRIDGE_URL env set → dev mode (today's behaviour, unchanged). - BRIDGE_URL unset → bundled mode: BackendConnection now 1. Resolves the user app data dir (QStandardPaths::AppDataLocation, ~/.local/share/<org>/<app> on Linux) and ensures var/, var/log/, var/cache/ exist there. 2. Generates a per-session 32-byte URL-safe token and a 48-byte Mercure JWT secret. 3. Runs `frankenphp php-cli bin/console doctrine:migrations:migrate -n` against the user's DATABASE_URL with a 60s timeout. 4. Spawns FrankenPHP via QProcess with BRIDGE_TOKEN/MERCURE_*/PORT in the env, prctl(PR_SET_PDEATHSIG, SIGTERM) on the child, and a supervisor that re-spawns up to 5 times on unexpected exit. Each restart fires tokenRotated(newToken). Path resolution defaults to applicationDirPath() + bin/frankenphp, applicationDirPath() + symfony, applicationDirPath() + Caddyfile, with both `/../share/<app>/...` and `/../usr/share/<app>/...` fallbacks for AppImage-style layouts. All three are overridable via BRIDGE_FRANKENPHP_BIN / BRIDGE_SYMFONY_DIR / BRIDGE_CADDYFILE env vars. Caddyfiles in skeleton + example now use {$VAR:default} substitution for PORT and the Mercure JWT keys, so the same Caddyfile works in both modes. Dev defaults match symfony/.env. restart() in bundled mode re-spawns the child (resets the supervisor counter); in dev mode it stays a probe-only no-op. Smoke-tested locally with `BRIDGE_FRANKENPHP_BIN=… BRIDGE_SYMFONY_DIR=… BRIDGE_CADDYFILE=… ./build/qml/todo` (no BRIDGE_URL): bundled mode created ~/.local/share/php-qml/todo/var/data.sqlite, ran the migration, spawned FrankenPHP, served /healthz, accepted a POST /api/todos with the per-session bearer. Dev mode (`make dev`) still works unchanged. Includes a `phpqml.bridge.bundled` Q_LOGGING_CATEGORY so failures surface to the user; enable with QT_LOGGING_RULES='phpqml.bridge.bundled.*=true'. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 17:00:13 +02:00
void urlChanged();
void tokenChanged();
void connectionStateChanged();
void errorChanged();
Phase 4a sub-commit 1: bundled-mode startup in BackendConnection Auto-detected on construction: - BRIDGE_URL env set → dev mode (today's behaviour, unchanged). - BRIDGE_URL unset → bundled mode: BackendConnection now 1. Resolves the user app data dir (QStandardPaths::AppDataLocation, ~/.local/share/<org>/<app> on Linux) and ensures var/, var/log/, var/cache/ exist there. 2. Generates a per-session 32-byte URL-safe token and a 48-byte Mercure JWT secret. 3. Runs `frankenphp php-cli bin/console doctrine:migrations:migrate -n` against the user's DATABASE_URL with a 60s timeout. 4. Spawns FrankenPHP via QProcess with BRIDGE_TOKEN/MERCURE_*/PORT in the env, prctl(PR_SET_PDEATHSIG, SIGTERM) on the child, and a supervisor that re-spawns up to 5 times on unexpected exit. Each restart fires tokenRotated(newToken). Path resolution defaults to applicationDirPath() + bin/frankenphp, applicationDirPath() + symfony, applicationDirPath() + Caddyfile, with both `/../share/<app>/...` and `/../usr/share/<app>/...` fallbacks for AppImage-style layouts. All three are overridable via BRIDGE_FRANKENPHP_BIN / BRIDGE_SYMFONY_DIR / BRIDGE_CADDYFILE env vars. Caddyfiles in skeleton + example now use {$VAR:default} substitution for PORT and the Mercure JWT keys, so the same Caddyfile works in both modes. Dev defaults match symfony/.env. restart() in bundled mode re-spawns the child (resets the supervisor counter); in dev mode it stays a probe-only no-op. Smoke-tested locally with `BRIDGE_FRANKENPHP_BIN=… BRIDGE_SYMFONY_DIR=… BRIDGE_CADDYFILE=… ./build/qml/todo` (no BRIDGE_URL): bundled mode created ~/.local/share/php-qml/todo/var/data.sqlite, ran the migration, spawned FrankenPHP, served /healthz, accepted a POST /api/todos with the per-session bearer. Dev mode (`make dev`) still works unchanged. Includes a `phpqml.bridge.bundled` Q_LOGGING_CATEGORY so failures surface to the user; enable with QT_LOGGING_RULES='phpqml.bridge.bundled.*=true'. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 17:00:13 +02:00
/// Emitted in bundled mode when the supervisor restarts the FrankenPHP
/// child and a fresh per-session secret is generated. RestClient and
/// MercureClient pick the new value up on next request (§3 *Edge cases*).
void tokenRotated(const QString& newToken);
private slots:
void probe();
void onProbeFinished();
Phase 4a sub-commit 1: bundled-mode startup in BackendConnection Auto-detected on construction: - BRIDGE_URL env set → dev mode (today's behaviour, unchanged). - BRIDGE_URL unset → bundled mode: BackendConnection now 1. Resolves the user app data dir (QStandardPaths::AppDataLocation, ~/.local/share/<org>/<app> on Linux) and ensures var/, var/log/, var/cache/ exist there. 2. Generates a per-session 32-byte URL-safe token and a 48-byte Mercure JWT secret. 3. Runs `frankenphp php-cli bin/console doctrine:migrations:migrate -n` against the user's DATABASE_URL with a 60s timeout. 4. Spawns FrankenPHP via QProcess with BRIDGE_TOKEN/MERCURE_*/PORT in the env, prctl(PR_SET_PDEATHSIG, SIGTERM) on the child, and a supervisor that re-spawns up to 5 times on unexpected exit. Each restart fires tokenRotated(newToken). Path resolution defaults to applicationDirPath() + bin/frankenphp, applicationDirPath() + symfony, applicationDirPath() + Caddyfile, with both `/../share/<app>/...` and `/../usr/share/<app>/...` fallbacks for AppImage-style layouts. All three are overridable via BRIDGE_FRANKENPHP_BIN / BRIDGE_SYMFONY_DIR / BRIDGE_CADDYFILE env vars. Caddyfiles in skeleton + example now use {$VAR:default} substitution for PORT and the Mercure JWT keys, so the same Caddyfile works in both modes. Dev defaults match symfony/.env. restart() in bundled mode re-spawns the child (resets the supervisor counter); in dev mode it stays a probe-only no-op. Smoke-tested locally with `BRIDGE_FRANKENPHP_BIN=… BRIDGE_SYMFONY_DIR=… BRIDGE_CADDYFILE=… ./build/qml/todo` (no BRIDGE_URL): bundled mode created ~/.local/share/php-qml/todo/var/data.sqlite, ran the migration, spawned FrankenPHP, served /healthz, accepted a POST /api/todos with the per-session bearer. Dev mode (`make dev`) still works unchanged. Includes a `phpqml.bridge.bundled` Q_LOGGING_CATEGORY so failures surface to the user; enable with QT_LOGGING_RULES='phpqml.bridge.bundled.*=true'. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 17:00:13 +02:00
void onChildFinished(int exitCode, QProcess::ExitStatus status);
private:
Phase 4a sub-commit 1: bundled-mode startup in BackendConnection Auto-detected on construction: - BRIDGE_URL env set → dev mode (today's behaviour, unchanged). - BRIDGE_URL unset → bundled mode: BackendConnection now 1. Resolves the user app data dir (QStandardPaths::AppDataLocation, ~/.local/share/<org>/<app> on Linux) and ensures var/, var/log/, var/cache/ exist there. 2. Generates a per-session 32-byte URL-safe token and a 48-byte Mercure JWT secret. 3. Runs `frankenphp php-cli bin/console doctrine:migrations:migrate -n` against the user's DATABASE_URL with a 60s timeout. 4. Spawns FrankenPHP via QProcess with BRIDGE_TOKEN/MERCURE_*/PORT in the env, prctl(PR_SET_PDEATHSIG, SIGTERM) on the child, and a supervisor that re-spawns up to 5 times on unexpected exit. Each restart fires tokenRotated(newToken). Path resolution defaults to applicationDirPath() + bin/frankenphp, applicationDirPath() + symfony, applicationDirPath() + Caddyfile, with both `/../share/<app>/...` and `/../usr/share/<app>/...` fallbacks for AppImage-style layouts. All three are overridable via BRIDGE_FRANKENPHP_BIN / BRIDGE_SYMFONY_DIR / BRIDGE_CADDYFILE env vars. Caddyfiles in skeleton + example now use {$VAR:default} substitution for PORT and the Mercure JWT keys, so the same Caddyfile works in both modes. Dev defaults match symfony/.env. restart() in bundled mode re-spawns the child (resets the supervisor counter); in dev mode it stays a probe-only no-op. Smoke-tested locally with `BRIDGE_FRANKENPHP_BIN=… BRIDGE_SYMFONY_DIR=… BRIDGE_CADDYFILE=… ./build/qml/todo` (no BRIDGE_URL): bundled mode created ~/.local/share/php-qml/todo/var/data.sqlite, ran the migration, spawned FrankenPHP, served /healthz, accepted a POST /api/todos with the per-session bearer. Dev mode (`make dev`) still works unchanged. Includes a `phpqml.bridge.bundled` Q_LOGGING_CATEGORY so failures surface to the user; enable with QT_LOGGING_RULES='phpqml.bridge.bundled.*=true'. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 17:00:13 +02:00
void initDevMode();
void initBundledMode();
bool runMigrations();
bool spawnChild(QString* errorOut = nullptr);
void teardownChild();
QString resolveFrankenphpBin() const;
QString resolveSymfonyDir() const;
QString resolveCaddyfilePath() const;
QString userDataDir() const;
QString databaseUrl() const;
void setState(ConnectionState s);
void setError(const QString& msg);
Phase 4a sub-commit 1: bundled-mode startup in BackendConnection Auto-detected on construction: - BRIDGE_URL env set → dev mode (today's behaviour, unchanged). - BRIDGE_URL unset → bundled mode: BackendConnection now 1. Resolves the user app data dir (QStandardPaths::AppDataLocation, ~/.local/share/<org>/<app> on Linux) and ensures var/, var/log/, var/cache/ exist there. 2. Generates a per-session 32-byte URL-safe token and a 48-byte Mercure JWT secret. 3. Runs `frankenphp php-cli bin/console doctrine:migrations:migrate -n` against the user's DATABASE_URL with a 60s timeout. 4. Spawns FrankenPHP via QProcess with BRIDGE_TOKEN/MERCURE_*/PORT in the env, prctl(PR_SET_PDEATHSIG, SIGTERM) on the child, and a supervisor that re-spawns up to 5 times on unexpected exit. Each restart fires tokenRotated(newToken). Path resolution defaults to applicationDirPath() + bin/frankenphp, applicationDirPath() + symfony, applicationDirPath() + Caddyfile, with both `/../share/<app>/...` and `/../usr/share/<app>/...` fallbacks for AppImage-style layouts. All three are overridable via BRIDGE_FRANKENPHP_BIN / BRIDGE_SYMFONY_DIR / BRIDGE_CADDYFILE env vars. Caddyfiles in skeleton + example now use {$VAR:default} substitution for PORT and the Mercure JWT keys, so the same Caddyfile works in both modes. Dev defaults match symfony/.env. restart() in bundled mode re-spawns the child (resets the supervisor counter); in dev mode it stays a probe-only no-op. Smoke-tested locally with `BRIDGE_FRANKENPHP_BIN=… BRIDGE_SYMFONY_DIR=… BRIDGE_CADDYFILE=… ./build/qml/todo` (no BRIDGE_URL): bundled mode created ~/.local/share/php-qml/todo/var/data.sqlite, ran the migration, spawned FrankenPHP, served /healthz, accepted a POST /api/todos with the per-session bearer. Dev mode (`make dev`) still works unchanged. Includes a `phpqml.bridge.bundled` Q_LOGGING_CATEGORY so failures surface to the user; enable with QT_LOGGING_RULES='phpqml.bridge.bundled.*=true'. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 17:00:13 +02:00
void setUrl(const QString& url);
void setToken(const QString& token);
static QString randomSecret(int bytes);
Mode m_mode = Mode::Dev;
QString m_url;
QString m_token;
Phase 4a sub-commit 1: bundled-mode startup in BackendConnection Auto-detected on construction: - BRIDGE_URL env set → dev mode (today's behaviour, unchanged). - BRIDGE_URL unset → bundled mode: BackendConnection now 1. Resolves the user app data dir (QStandardPaths::AppDataLocation, ~/.local/share/<org>/<app> on Linux) and ensures var/, var/log/, var/cache/ exist there. 2. Generates a per-session 32-byte URL-safe token and a 48-byte Mercure JWT secret. 3. Runs `frankenphp php-cli bin/console doctrine:migrations:migrate -n` against the user's DATABASE_URL with a 60s timeout. 4. Spawns FrankenPHP via QProcess with BRIDGE_TOKEN/MERCURE_*/PORT in the env, prctl(PR_SET_PDEATHSIG, SIGTERM) on the child, and a supervisor that re-spawns up to 5 times on unexpected exit. Each restart fires tokenRotated(newToken). Path resolution defaults to applicationDirPath() + bin/frankenphp, applicationDirPath() + symfony, applicationDirPath() + Caddyfile, with both `/../share/<app>/...` and `/../usr/share/<app>/...` fallbacks for AppImage-style layouts. All three are overridable via BRIDGE_FRANKENPHP_BIN / BRIDGE_SYMFONY_DIR / BRIDGE_CADDYFILE env vars. Caddyfiles in skeleton + example now use {$VAR:default} substitution for PORT and the Mercure JWT keys, so the same Caddyfile works in both modes. Dev defaults match symfony/.env. restart() in bundled mode re-spawns the child (resets the supervisor counter); in dev mode it stays a probe-only no-op. Smoke-tested locally with `BRIDGE_FRANKENPHP_BIN=… BRIDGE_SYMFONY_DIR=… BRIDGE_CADDYFILE=… ./build/qml/todo` (no BRIDGE_URL): bundled mode created ~/.local/share/php-qml/todo/var/data.sqlite, ran the migration, spawned FrankenPHP, served /healthz, accepted a POST /api/todos with the per-session bearer. Dev mode (`make dev`) still works unchanged. Includes a `phpqml.bridge.bundled` Q_LOGGING_CATEGORY so failures surface to the user; enable with QT_LOGGING_RULES='phpqml.bridge.bundled.*=true'. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 17:00:13 +02:00
QString m_jwtSecret;
ConnectionState m_state = ConnectionState::Connecting;
QString m_error;
Phase 4a sub-commit 1: bundled-mode startup in BackendConnection Auto-detected on construction: - BRIDGE_URL env set → dev mode (today's behaviour, unchanged). - BRIDGE_URL unset → bundled mode: BackendConnection now 1. Resolves the user app data dir (QStandardPaths::AppDataLocation, ~/.local/share/<org>/<app> on Linux) and ensures var/, var/log/, var/cache/ exist there. 2. Generates a per-session 32-byte URL-safe token and a 48-byte Mercure JWT secret. 3. Runs `frankenphp php-cli bin/console doctrine:migrations:migrate -n` against the user's DATABASE_URL with a 60s timeout. 4. Spawns FrankenPHP via QProcess with BRIDGE_TOKEN/MERCURE_*/PORT in the env, prctl(PR_SET_PDEATHSIG, SIGTERM) on the child, and a supervisor that re-spawns up to 5 times on unexpected exit. Each restart fires tokenRotated(newToken). Path resolution defaults to applicationDirPath() + bin/frankenphp, applicationDirPath() + symfony, applicationDirPath() + Caddyfile, with both `/../share/<app>/...` and `/../usr/share/<app>/...` fallbacks for AppImage-style layouts. All three are overridable via BRIDGE_FRANKENPHP_BIN / BRIDGE_SYMFONY_DIR / BRIDGE_CADDYFILE env vars. Caddyfiles in skeleton + example now use {$VAR:default} substitution for PORT and the Mercure JWT keys, so the same Caddyfile works in both modes. Dev defaults match symfony/.env. restart() in bundled mode re-spawns the child (resets the supervisor counter); in dev mode it stays a probe-only no-op. Smoke-tested locally with `BRIDGE_FRANKENPHP_BIN=… BRIDGE_SYMFONY_DIR=… BRIDGE_CADDYFILE=… ./build/qml/todo` (no BRIDGE_URL): bundled mode created ~/.local/share/php-qml/todo/var/data.sqlite, ran the migration, spawned FrankenPHP, served /healthz, accepted a POST /api/todos with the per-session bearer. Dev mode (`make dev`) still works unchanged. Includes a `phpqml.bridge.bundled` Q_LOGGING_CATEGORY so failures surface to the user; enable with QT_LOGGING_RULES='phpqml.bridge.bundled.*=true'. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 17:00:13 +02:00
QString m_appName;
QString m_dataDir;
quint16 m_port = 8765;
QNetworkAccessManager* m_nam = nullptr;
QNetworkReply* m_pendingReply = nullptr;
QTimer* m_retryTimer = nullptr;
Phase 4a sub-commit 1: bundled-mode startup in BackendConnection Auto-detected on construction: - BRIDGE_URL env set → dev mode (today's behaviour, unchanged). - BRIDGE_URL unset → bundled mode: BackendConnection now 1. Resolves the user app data dir (QStandardPaths::AppDataLocation, ~/.local/share/<org>/<app> on Linux) and ensures var/, var/log/, var/cache/ exist there. 2. Generates a per-session 32-byte URL-safe token and a 48-byte Mercure JWT secret. 3. Runs `frankenphp php-cli bin/console doctrine:migrations:migrate -n` against the user's DATABASE_URL with a 60s timeout. 4. Spawns FrankenPHP via QProcess with BRIDGE_TOKEN/MERCURE_*/PORT in the env, prctl(PR_SET_PDEATHSIG, SIGTERM) on the child, and a supervisor that re-spawns up to 5 times on unexpected exit. Each restart fires tokenRotated(newToken). Path resolution defaults to applicationDirPath() + bin/frankenphp, applicationDirPath() + symfony, applicationDirPath() + Caddyfile, with both `/../share/<app>/...` and `/../usr/share/<app>/...` fallbacks for AppImage-style layouts. All three are overridable via BRIDGE_FRANKENPHP_BIN / BRIDGE_SYMFONY_DIR / BRIDGE_CADDYFILE env vars. Caddyfiles in skeleton + example now use {$VAR:default} substitution for PORT and the Mercure JWT keys, so the same Caddyfile works in both modes. Dev defaults match symfony/.env. restart() in bundled mode re-spawns the child (resets the supervisor counter); in dev mode it stays a probe-only no-op. Smoke-tested locally with `BRIDGE_FRANKENPHP_BIN=… BRIDGE_SYMFONY_DIR=… BRIDGE_CADDYFILE=… ./build/qml/todo` (no BRIDGE_URL): bundled mode created ~/.local/share/php-qml/todo/var/data.sqlite, ran the migration, spawned FrankenPHP, served /healthz, accepted a POST /api/todos with the per-session bearer. Dev mode (`make dev`) still works unchanged. Includes a `phpqml.bridge.bundled` Q_LOGGING_CATEGORY so failures surface to the user; enable with QT_LOGGING_RULES='phpqml.bridge.bundled.*=true'. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 17:00:13 +02:00
QElapsedTimer m_firstFailureSinceOnline;
Phase 2 sub-commit 3: full Update Semantics + ReactiveListModel + AppShell BackendConnection's ConnectionState enum is now Connecting / Online / Reconnecting / Offline (PLAN.md §5). The probe loop tracks the first failure since the last Online and transitions to Reconnecting on any failed probe, then to Offline once the configurable threshold (30 s default) is exceeded. The Error state is gone; Reconnecting + the exposed `error` string subsume its UI role. ReactiveListModel is the headline QML type: - QAbstractListModel that GETs `baseUrl + source` for an initial JSON array and then keeps in sync via an internal MercureClient subscribed to `topic`. - Role names are derived dynamically from the first row's keys plus an internal `pending` boolean role used by optimistic mutations. - Diff application: upsert (insert-or-update), delete, replace; gap detection via the envelope `version` field with auto re-fetch. - `invoke(method, path, body, optimistic)` is the optimistic command primitive. Generates an Idempotency-Key, applies the local diff, POST/PATCH/DELETEs with that key, and resolves on the matching Mercure echo (correlation-key matched in ModelPublisher's envelope). Rolls back and emits commandFailed on 4xx/5xx, commandTimedOut after 10 s without an echo. Phase 4 packaging will surface configuration for the timeout. AppShell.qml is the optional convenience root: - Reads BackendConnection.connectionState. - Reconnecting → top banner. - Offline → modal overlay with the error string and a Retry button (calls BackendConnection.restart()). - Wraps user content via `default property alias content`. Apps that want full chrome control can skip AppShell entirely; the skeleton's Main.qml keeps its own status display for demonstration and is unaffected. ReactiveObject (single-entity twin of ReactiveListModel) is intentionally deferred — same envelope handling, smaller surface; will land in Phase 2 follow-up or Phase 3 alongside the todo example. Cursor pagination is similarly deferred (the Phase 2 done criterion uses small lists). Smoke tested: /healthz + /api/ping round-trip cleanly, zero Mercure 401s, clean shutdown. composer quality stays green (16 tests, 45 assertions). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 02:40:12 +02:00
int m_offlineThresholdMs = 30000;
Phase 4a sub-commit 1: bundled-mode startup in BackendConnection Auto-detected on construction: - BRIDGE_URL env set → dev mode (today's behaviour, unchanged). - BRIDGE_URL unset → bundled mode: BackendConnection now 1. Resolves the user app data dir (QStandardPaths::AppDataLocation, ~/.local/share/<org>/<app> on Linux) and ensures var/, var/log/, var/cache/ exist there. 2. Generates a per-session 32-byte URL-safe token and a 48-byte Mercure JWT secret. 3. Runs `frankenphp php-cli bin/console doctrine:migrations:migrate -n` against the user's DATABASE_URL with a 60s timeout. 4. Spawns FrankenPHP via QProcess with BRIDGE_TOKEN/MERCURE_*/PORT in the env, prctl(PR_SET_PDEATHSIG, SIGTERM) on the child, and a supervisor that re-spawns up to 5 times on unexpected exit. Each restart fires tokenRotated(newToken). Path resolution defaults to applicationDirPath() + bin/frankenphp, applicationDirPath() + symfony, applicationDirPath() + Caddyfile, with both `/../share/<app>/...` and `/../usr/share/<app>/...` fallbacks for AppImage-style layouts. All three are overridable via BRIDGE_FRANKENPHP_BIN / BRIDGE_SYMFONY_DIR / BRIDGE_CADDYFILE env vars. Caddyfiles in skeleton + example now use {$VAR:default} substitution for PORT and the Mercure JWT keys, so the same Caddyfile works in both modes. Dev defaults match symfony/.env. restart() in bundled mode re-spawns the child (resets the supervisor counter); in dev mode it stays a probe-only no-op. Smoke-tested locally with `BRIDGE_FRANKENPHP_BIN=… BRIDGE_SYMFONY_DIR=… BRIDGE_CADDYFILE=… ./build/qml/todo` (no BRIDGE_URL): bundled mode created ~/.local/share/php-qml/todo/var/data.sqlite, ran the migration, spawned FrankenPHP, served /healthz, accepted a POST /api/todos with the per-session bearer. Dev mode (`make dev`) still works unchanged. Includes a `phpqml.bridge.bundled` Q_LOGGING_CATEGORY so failures surface to the user; enable with QT_LOGGING_RULES='phpqml.bridge.bundled.*=true'. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 17:00:13 +02:00
QProcess* m_child = nullptr;
int m_supervisorRetries = 0;
static constexpr int kMaxSupervisorRetries = 5;
};
} // namespace PhpQml::Bridge