定制 snortlin/config-bundle 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

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' }

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-03-26