snortlin/config-bundle
Composer 安装命令:
composer require snortlin/config-bundle
包简介
Symfony bundle for application configuration values
README 文档
README
Installation
Step 1: Download the Bundle
The preferred method of installation is via Composer:
composer require snortlin/config-bundle
Step 2: Enable the Bundle
Then, enable the bundle by adding it to the list of registered bundles
in the config/bundles.php file of your project:
// config/bundles.php return [ // ... Snortlin\Bundle\ConfigBundle\ConfigBundle::class => ['all' => true], ];
Usage
# config/packages/config.yaml # Caching is optional framework: cache: pools: config.cache.config: adapter: 'cache.adapter.filesystem' config: entity_class: App\Entity\Config cache: enabled: true # optional, default is true service: 'config.cache.config' # optional, null = Array Cache Adapter lifetime: 180 # optional, null = cache pool default
Storing in database
Create entity:
// App\Entity\Config use Doctrine\ORM\Mapping as ORM; use Snortlin\Bundle\ConfigBundle\Entity\AbstractConfig; #[ORM\Entity] #[ORM\Table( name: 'configs', )] class Config extends AbstractConfig { }
Doctrine migrations:
// DoctrineMigrations public function up(Schema $schema): void { $this->addSql(<<<'SQL' CREATE TABLE configs ( key VARCHAR(128) NOT NULL, value JSON DEFAULT NULL, description TEXT DEFAULT NULL, PRIMARY KEY(key)) SQL ); $this->addSql('ALTER TABLE configs ADD CONSTRAINT uc_configs_key UNIQUE(key)'); }
Configuration model
Use the #[AsSystemConfig] attribute to define a configuration model and SerializerFactory::SERIALIZER_GROUP to define values for serialization.
// App\Config use Snortlin\Bundle\ConfigBundle\Attribute\AsSystemConfig; use Snortlin\Bundle\ConfigBundle\Serializer\SerializerFactory; use Symfony\Component\Serializer\Annotation as Serializer; #[AsSystemConfig('my_config_key')] #[Serializer\Groups(SerializerFactory::SERIALIZER_GROUP)] class MyConfig { public function __construct(public string $value1, public ?string $value2 = null) { } // ... }
Configuration defaults
Use ConfigDefaultsInterface to implement the default instance.
use Snortlin\Bundle\ConfigBundle\Config\ConfigDefaultsInterface; class HealthcheckRequestMatcherConfig implements ConfigDefaultsInterface { public static function getConfigDefaults(): self { return new self('My value1'); } }
Configuration defaults
Use ConfigSerializerContextInterface to modify serializer context for this model.
use Snortlin\Bundle\ConfigBundle\Config\ConfigSerializerContextInterface; use Symfony\Component\Serializer\Context\Normalizer\AbstractObjectNormalizerContextBuilder; class HealthcheckRequestMatcherConfig implements ConfigSerializerContextInterface { public static function withConfigNormalizationContextBuilder(AbstractObjectNormalizerContextBuilder $contextBuilder): AbstractObjectNormalizerContextBuilder { return $contextBuilder ->withGroups(['group1', 'group2']); } }
Avoiding cache key collisions when using a shared cache
# config/packages/config.yaml config: cache: service: 'cache.app' # shared cache key_prefix: 'configs_' # cache key prefix
Shared cache
# config/packages/config.yaml config: config_paths: - '%kernel.project_dir%/src/Config' # this is default
Custom service with namespace prefix
# config/packages/config.yaml services: config.cache.config: parent: 'cache.adapter.redis_tag_aware' tags: - { name: 'cache.pool', namespace: '%env(APP_PREFIX_SEED)%_config' }
snortlin/config-bundle 适用场景与选型建议
snortlin/config-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 297 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 03 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 snortlin/config-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 snortlin/config-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 297
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-03-26