temant/settings-manager 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

temant/settings-manager

Composer 安装命令:

composer require temant/settings-manager

包简介

A flexible, type-safe settings management library for PHP powered by Doctrine ORM.

README 文档

README

Build Status Coverage Status License PHPStan PHP

A modern, type-safe settings management library for PHP applications powered by Doctrine ORM. Persist key-value settings to any database with automatic type detection, in-memory caching, group organisation, bulk operations, and import/export.

Features

  • 8 data typesstring, integer, boolean, float, json, array, datetime, and auto (auto-detect)
  • In-memory cache — subsequent reads for the same key skip the database
  • Groups & descriptions — organise settings logically and document them inline
  • Bulk operationssetMany(), removeMany(), clear()
  • Search & filter — substring search and group-based filtering
  • Import / Export — JSON and array formats, round-trip safe
  • Fluent interface — chain calls: $manager->set(...)->set(...)->remove(...)
  • Countablecount($manager) returns the total number of settings
  • Doctrine ORM 3 — works with MySQL, PostgreSQL, and SQLite
  • PHPStan max level — fully statically analysed
  • 65 tests, 141 assertions — comprehensive test suite

Installation

composer require temant/settings-manager

Requires PHP 8.5+ and Doctrine ORM 3.

Quick Start

use Temant\SettingsManager\SettingsManager;
use Temant\SettingsManager\Enum\SettingType;

// Create a manager with your Doctrine EntityManager
$manager = new SettingsManager($entityManager);

// Set values — type is auto-detected
$manager->set('site.name', 'Acme Corp');
$manager->set('cache.ttl', 3600);
$manager->set('debug', false);

// Get typed values back
$manager->getValue('site.name');   // 'Acme Corp' (string)
$manager->getValue('cache.ttl');   // 3600 (int)
$manager->getValue('debug');       // false (bool)

// Safe defaults for missing keys
$manager->getOrDefault('ui.theme', 'dark'); // 'dark'

Usage Guide

Setting Values

use Temant\SettingsManager\Enum\SettingType;

// Auto-detect type (default)
$manager->set('key', 'value');
$manager->set('count', 42);
$manager->set('enabled', true);
$manager->set('rate', 0.75);
$manager->set('config', '{"nested": true}');    // detected as JSON
$manager->set('tags', ['php', 'doctrine']);      // stored as ARRAY
$manager->set('launch', new DateTimeImmutable()); // stored as DATETIME

// Explicit type
$manager->set('port', '8080', SettingType::STRING); // force string, not int

// With metadata
$manager->set(
    name: 'smtp.host',
    value: 'mail.example.com',
    description: 'SMTP server hostname',
    group: 'email',
);

// Prevent accidental overwrites
$manager->set('api.key', 'secret', allowUpdate: false);
// throws SettingAlreadyExistsException if 'api.key' exists

Retrieving Values

// Full entity (with metadata, timestamps, etc.)
$entity = $manager->get('site.name');
$entity->getValue();       // typed value
$entity->getRawValue();    // raw string from DB
$entity->getType();        // SettingType::STRING
$entity->getDescription(); // ?string
$entity->getGroup();       // ?string
$entity->getCreatedAt();   // DateTimeImmutable
$entity->getUpdatedAt();   // ?DateTimeImmutable

// Shorthand — typed value directly
$manager->getValue('site.name');              // 'Acme Corp'

// With fallback
$manager->getOrDefault('missing.key', 'default'); // 'default'

// Existence check
$manager->exists('site.name'); // true
$manager->has('site.name');    // alias

Updating Values

use Temant\SettingsManager\Enum\UpdateType;

// Update value and auto-detect new type
$manager->update('cache.ttl', 7200);

// Update value but keep the original type (validates compatibility)
$manager->update('cache.ttl', 'not-an-int', UpdateType::KEEP_CURRENT);
// throws SettingTypeMismatchException

Removing Values

$manager->remove('old.setting');

// Remove multiple
$manager->removeMany(['key1', 'key2', 'key3']);

// Remove everything
$manager->clear();

Bulk Operations

$manager->setMany([
    'site.name'  => ['value' => 'Acme', 'type' => SettingType::STRING, 'group' => 'site'],
    'site.url'   => ['value' => 'https://acme.dev', 'group' => 'site'],
    'cache.ttl'  => ['value' => 3600, 'description' => 'Cache lifetime in seconds'],
    'debug.mode' => ['value' => false],
]);

Search & Filter

// Substring search (case-insensitive)
$results = $manager->search('site');    // all settings with 'site' in the name

// Group filtering
$emailSettings = $manager->findByGroup('email');

Counting

$total = count($manager); // implements Countable

Import & Export

// Export all settings to array
$data = $manager->export();

// Export to JSON
$json = $manager->exportJson();

// Import from array
$manager->import($data);

// Import from JSON
$manager->importJson($json);

// Static utility classes also available
use Temant\SettingsManager\Utils\SettingsExporter;
use Temant\SettingsManager\Utils\SettingsImporter;

$json = SettingsExporter::toJson($manager);
SettingsImporter::fromJson($manager, $json);

Default Settings

Seed settings on first run — existing values are never overwritten:

$manager = new SettingsManager($entityManager, 'settings', [
    'site.name' => [
        'value' => 'My App',
        'type' => SettingType::STRING,
        'description' => 'Application name',
        'group' => 'site',
    ],
    'cache.ttl' => [
        'value' => 3600,
        // type auto-detected as INTEGER
    ],
    'debug' => [
        'value' => false,
    ],
]);

Custom Table Name

$manager = new SettingsManager($entityManager, 'app_config');

Cache Management

The manager caches entities in memory. If you modify the settings table externally:

$manager->clearCache();

Data Types

SettingType PHP Type Storage Format Example
STRING string As-is 'hello'
INTEGER int String cast 42
BOOLEAN bool 'true' / 'false' true
FLOAT float String cast 3.14
JSON array (assoc) JSON string '{"key":"val"}'
ARRAY array JSON encoded ['a', 'b']
DATETIME DateTimeImmutable ISO 8601 new DateTimeImmutable
AUTO (detected) (varies) (any of the above)

Exceptions

Exception When
SettingAlreadyExistsException set() with allowUpdate: false on existing key
SettingNotFoundException update() / remove() on missing key
SettingTypeMismatchException Value incompatible with expected type
SettingsImportExportException Import/export failure (bad JSON, missing keys)
SettingsTableInitializationException Database table creation failure

Running Tests

composer test

Static Analysis

composer phpstan

Run Both

composer check-all

Contributing

Contributions are welcome! Feel free to submit issues or pull requests.

License

MIT

temant/settings-manager 适用场景与选型建议

temant/settings-manager 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 67 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 09 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「orm」 「php」 「configuration」 「doctrine」 「Settings」 「typed-settings」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 temant/settings-manager 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 temant/settings-manager 我们能提供哪些服务?
定制开发 / 二次开发

基于 temant/settings-manager 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 67
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 29
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-09-10