changhorizon/scoped-storage-strategy

A flexible and pluggable storage strategy for managing scoped, temporary key-value data using sessions, tokens, Redis, and more.

Maintainers

Package info

github.com/changhorizon/scoped-storage-strategy

pkg:composer/changhorizon/scoped-storage-strategy

Statistics

Installs: 25

Dependents: 1

Suggesters: 0

Stars: 0

Open Issues: 0

v2.1.2 2026-05-12 00:16 UTC

This package is auto-updated.

Last update: 2026-05-12 00:16:56 UTC


README

A pluggable and namespace-aware storage abstraction for temporarily persisting key-value data during scoped user interactions.

License Latest Version PHP Version Static Analysis Tests codecov CI

Supports session-based (cookie and token) and Redis-based implementations, designed to decouple application logic from underlying storage mechanisms. Ideal for tracking transient states — such as validation progress, multistep workflows, or temporary metadata.

✨ 特性

  • 🍪 Cookie-based PHP session — traditional web applications
  • 🆔 Token-based PHP session — stateless API support
  • 🚀 Redis storage — shared, scalable scenarios
  • 🔌 PSR-style interface for easy integration and extension
  • ✅ Unified interface with put, get, exists, remove, clear

📦 安装

composer require changhorizon/scoped-storage-strategy

📂 目录结构

src/
├── ScopedStorageStrategyInterface.php
├── SessionInitializerInterface.php
├── Session/
│   ├── SessionStorageStrategy.php
│   ├── SessionInitializerWithCookie.php
│   └── SessionInitializerWithToken.php
└── Redis/
    └── RedisStorageStrategy.php

🚀 用法示例

SessionStorageStrategy with Cookie

use ChangHorizon\ScopedStorageStrategy\SessionStorageStrategy;
use ChangHorizon\ScopedStorageStrategy\SessionInitializerWithCookie;

$initializer = new SessionInitializerWithCookie();
$strategy = new SessionStorageStrategy('scope-123', $initializer);

$strategy->put('demo-file-123', '/path/to/demo-file-123');
$value = $strategy->get('demo-file-123');

SessionStorageStrategy with Token

use ChangHorizon\ScopedStorageStrategy\SessionStorageStrategy;
use ChangHorizon\ScopedStorageStrategy\SessionInitializerWithToken;

$token = $_GET['token'] ?? '';
$initializer = new SessionInitializerWithToken($token);
$strategy = new SessionStorageStrategy('scope-456', $initializer);

$strategy->put('demo-file-456', '/path/to/demo-file-456');
$value = $strategy->get('demo-file-456');

RedisStorageStrategy

use ChangHorizon\ScopedStorageStrategy\RedisStorageStrategy;

$redis = new \Redis();
$redis->connect('127.0.0.1', 6379);

$strategy = new RedisStorageStrategy('scope-789', $redis);

$strategy->put('demo-file-789', '/path/to/demo-file-789');
$value = $strategy->get('demo-file-789');

📐 接口说明

All strategies implement:

namespace ChangHorizon\ScopedStorageStrategy;

interface ScopedStorageStrategyInterface
{
    public function put(string $key, string $value): void;
    public function get(string $key): ?string;
    public function exists(string $key): bool;
    public function remove(string $key): void;
    public function all(): ?array;
    public function empty(): bool;
    public function clear(): void;
}

Session-based strategies require a session initializer:

namespace ChangHorizon\ScopedStorageStrategy;

interface SessionInitializerInterface
{
    public function initialize(): void;
}

🔍 静态分析

composer stan

🎯 代码风格

composer cs:chk    # check
composer cs:fix    # auto-fix

✅ 单元测试

composer test
composer test:coverage

🤝 贡献指南

欢迎 Issue 与 PR,建议遵循以下流程:

  1. Fork 仓库
  2. 创建新分支进行开发
  3. 提交 PR 前请确保测试通过、风格一致
  4. 提交详细描述

📜 License

MIT License. See the LICENSE file for details.