marktic/settings
Composer 安装命令:
composer require marktic/settings
包简介
Multi-tenant settings management package
README 文档
README
A multi-tenant settings management package for PHP 8.2+ applications. Supports typed values (string, JSON, integer, float, boolean, date, datetime, email, url), grouped settings, tenant-scoped configuration, and multiple storage adapters (database, cache file).
Features
- Typed settings: PHP property types (
string,bool,int,float,array) automatically determine the cast, with explicit per-property overrides fordate,datetime,email, andurl - Class-based settings: Define settings as plain PHP classes extending
AbstractSettings; properties with default values are automatically used as fallbacks - Grouped settings: Each settings class declares its group via
group() - Multi-tenant support: Scope settings to any tenant by passing a tenant record to
SettingsManager::get() - Instance caching:
SettingsManager::get()always returns the same object for the same class+tenant combination - Multiple storages: Persist settings to a relational database (
DatabaseStorage) or a JSON cache file (FileStorage) - Mapper:
SettingMapperhandles low-level conversion betweenSettingDtoand database/array representations - Trait-based integration: Use
HasSettingsRecordTraitto add per-model setting capabilities - Timestamps: Every stored setting entry tracks
created_atandupdated_at
Installation
composer require marktic/settings
Register the service provider (if not using auto-discovery):
// config/app.php (Laravel) 'providers' => [ Marktic\Settings\MktSettingsServiceProvider::class, ],
Run migrations:
php artisan migrate
Usage
Defining a Settings Class
use Marktic\Settings\AbstractSettings; class GeneralSettings extends AbstractSettings { // PHP property types determine the storage cast automatically: // string → SettingType::String // bool → SettingType::Boolean // int → SettingType::Integer // float → SettingType::Float // array → SettingType::Json (stored as JSON) public string $site_name = 'My App'; public bool $site_active = true; public int $max_items = 20; public float $tax_rate = 0.2; public array $supported_locales = ['en']; public string $launch_date = '2026-01-01'; public string $maintenance_at = '2026-01-01 10:00:00'; public string $support_email = 'support@example.com'; public string $homepage_url = 'https://example.com'; public static function settingTypes(): array { return [ 'launch_date' => 'date', 'maintenance_at' => 'datetime', 'support_email' => 'email', 'homepage_url' => 'url', ]; } // Determines the group used in storage public static function group(): string { return 'general'; } // Returns the named storage to use, or null for the package default public static function repository(): ?string { return null; } }
Setting up the SettingsManager
use Marktic\Settings\Hydrator\SettingsHydrator;use Marktic\Settings\Mapper\SettingMapper;use Marktic\Settings\MktSettingsManager;use Marktic\Settings\Storages\FileStorage;use Marktic\Settings\Utility\MktSettingsModels; // Database storage (requires bytic/orm set up) $storage = MktSettingsModels::createDatabaseStorage(); // File storage (no database required) $storage = new FileStorage('/path/to/settings.json', new SettingMapper()); $manager = new MktSettingsManager($storage, new SettingsHydrator()); // Register optional named storages referenced by repository() $manager->addStorage('file', new FileStorage('/path/to/file.json', new SettingMapper()));
Retrieving and Saving Settings
// Get hydrated settings (default values are used if nothing is stored yet) $settings = $manager->get(GeneralSettings::class); echo $settings->site_name; // "My App" // Repeated calls return the SAME object instance $settings1 = $manager->get(GeneralSettings::class); $settings2 = $manager->get(GeneralSettings::class); var_dump($settings1 === $settings2); // true // Modify and save $settings->site_name = 'New Name'; $settings->max_items = 50; $manager->save($settings);
Tenant-Scoped Settings
use Marktic\Settings\SettingsTenantInterface; // Your model can implement SettingsTenantInterface class Organization implements SettingsTenantInterface { public function getSettingTenantType(): string { return static::class; } public function getSettingTenantId(): string|int|null { return $this->id; } } $org = Organization::find(42); // Each tenant gets its own isolated instance and storage scope $tenantSettings = $manager->get(GeneralSettings::class, $org); $tenantSettings->site_name = 'Org Site'; $manager->save($tenantSettings); // Another tenant is isolated $otherOrg = Organization::find(99); $otherSettings = $manager->get(GeneralSettings::class, $otherOrg); echo $otherSettings->site_name; // "My App" (its own defaults)
Low-level: Using Storages Directly
use Marktic\Settings\Mapper\SettingMapper;use Marktic\Settings\Settings\Dto\SettingDto;use Marktic\Settings\Settings\Enums\SettingType;use Marktic\Settings\Storages\FileStorage; $storage = new FileStorage('/path/to/settings.json', new SettingMapper()); $dto = new SettingDto(); $dto->name = 'site.title'; $dto->group = 'general'; $dto->type = SettingType::String; $dto->setValue('My Application'); $storage->save($dto); $found = $storage->find('site.title', 'general'); echo $found->getCastValue(); // "My Application"
Database Schema
Table: mkt_settings
| Column | Type | Description |
|---|---|---|
id |
BIGINT UNSIGNED |
Primary key |
name |
VARCHAR(191) |
Setting name/key |
group |
VARCHAR(100) |
Logical group, default: "default" |
value |
TEXT |
Raw stored value |
type |
VARCHAR(20) |
Value type: string, json, integer, float, boolean, date, datetime, email, url |
tenant_type |
VARCHAR(191) |
Tenant class/type (nullable) |
tenant_id |
BIGINT UNSIGNED |
Tenant identifier (nullable) |
created_at |
DATETIME |
Creation timestamp |
updated_at |
DATETIME |
Last update timestamp |
A unique index on (name, group, tenant_type, tenant_id) ensures no duplicate settings per scope.
Architecture
src/
├── AbstractSettings.php # Base class for user-defined settings (extend this)
├── SettingsManager.php # Manager: get(class, tenant?) with caching + save()
├── SettingsTenantInterface.php # Interface for tenant objects
├── Settings/
│ ├── Models/ # ORM Record (Setting) + RecordManager (Settings)
│ ├── Dto/ # SettingDto — low-level value object
│ ├── Enums/ # SettingType — typed enum with cast/encode
│ ├── Mapper/ # SettingMapper — DTO ↔ DB / array conversion
│ ├── Hydrator/ # SettingsHydrator — reflection-based property mapping
│ └── Storages/ # SettingStorageInterface, DatabaseStorage, FileStorage
├── AbstractBase/ # Base Record and Repository classes
├── ModelsRelated/ # Cross-cutting HasSettings traits
├── Utility/ # SettingsModels (ModelFinder), PackageConfig
└── SettingsServiceProvider.php
Testing
composer test
Changelog
See CHANGELOG.md for recent changes.
License
The MIT License (MIT). See LICENSE.
Inspiration
This package was inspired by and takes ideas from the following open-source projects:
- spatie/laravel-settings — DTO-based settings with casts, migrations support, and per-repository design.
- akaunting/laravel-setting — Simple key-value settings with driver-based persistence.
- phemellc/yii2-settings — Group-based settings with ActiveRecord integration for Yii2.
- jbtronics/settings-bundle — Attribute-driven settings with storage adapters and Symfony DI integration.
- ScriptingBeating/laravel-global-settings — Global application settings with a simple API.
- appstract/laravel-options — Fluent options/settings stored in the database.
marktic/settings 适用场景与选型建议
marktic/settings 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 50 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 02 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「configuration」 「Settings」 「multitenant」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 marktic/settings 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 marktic/settings 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 marktic/settings 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Persistent settings package for Laravel framework.
Configuration component
A Zend Framework module to quickly and easily set PHP settings.
Settings Card for Laravel Nova.
The shared database used by all tenants
Utils to load, parse and work with configuration on Mezzio projects
统计信息
- 总下载量: 50
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-02-23