承接 2lenet/config-bundle 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

2lenet/config-bundle

最新稳定版本:1.4.5

Composer 安装命令:

composer require 2lenet/config-bundle

包简介

Config Bundle

README 文档

README

Validate .github/workflows/test.yml SymfonyInsight

Symfony bundle that gives you an easy configuration for your app. Perfect to use with the famous CRUD Crudit

Installation

The bundle is not yet on packagist make sure to add the following to your composer.json file:

{ "url": "https://github.com/2lenet/ConfigBundle", "type": "git" }

Install with composer:

composer require 2lenet/config-bundle

The bundle is flexible and built to suit your project it is shiped only with trait to use in your own config entity.

You will also get a Symfony Repository ready to use.

Create in your entity directory the class Config it has to implements the ConfigInterface if no customization is needed you can use:

<?php namespace App\Entity; use Doctrine\ORM\Mapping as ORM; use Lle\ConfigBundle\Traits\ConfigTrait; use App\Repository\ConfigRepository; use Lle\ConfigBundle\Contracts\ConfigInterface; /**  * @ORM\Entity(repositoryClass=ConfigRepository::class)  */ class Config implements ConfigInterface { use ConfigTrait; }

Your repository file has to extend the ConfigRepository from the bundle:

<?php namespace App\Repository; use App\Entity\Config; use Doctrine\Persistence\ManagerRegistry; use Lle\ConfigBundle\Repository\AbstractConfigRepository; use Lle\ConfigBundle\Service\CacheManager; /**  * @method Config|null find($id, $lockMode = null, $lockVersion = null)  * @method Config|null findOneBy(array $criteria, array $orderBy = null)  * @method Config[] findAll()  * @method Config[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)  */ class ConfigRepository extends AbstractConfigRepository { public function __construct(ManagerRegistry $registry, CacheManager $cacheManager) { parent::__construct($cacheManager, $registry, Config::class); } ... }

In your project add the folowing to the config file: /config/packages/doctrine.yaml

doctrine: orm: resolve_target_entities: Lle\ConfigBundle\Contracts\ConfigInterface: App\Entity\Config

In /config/routes.yaml add:

lle_config: resource: "@LleConfigBundle/Resources/config/routes.yaml"

You can then create a migration

bin/console make:migration

Check the migration file created and ask doctrine to execute the migration :

bin/console doctrine:migrations:migrate

You are ready to go!

Customization

If you need more options or entity fields you can add them in your entity class:

<?php namespace App\Entity; use App\Repository\ConfigRepository; use Doctrine\ORM\Mapping as ORM; use Lle\ConfigBundle\Contracts\ConfigInterface; use Lle\ConfigBundle\Traits\ConfigTrait; /**  * @ORM\Entity(repositoryClass=ConfigRepository::class)  */ class Config implements ConfigInterface { use ConfigTrait; /**  * @ORM\ManyToOne(targetEntity=Establishment::class, inversedBy="configs")  * @ORM\JoinColumn(nullable=false)  */ private ?Establishment $establishment; public function getEstablishment(): ?Establishment { return $this->establishment; } public function setEstablishment(?Establishment $establishment): self { $this->establishment = $establishment; return $this; } }

You may also need more options than the ones in the Repository file, in that case create a new repository class in your project. Don't forget to update the namspace used in your entity (see previous exemple).

Usage

General overview

To use the bundle, inject in your services the config repository and use one of the available methods. The bundle will check if the configuration exist otherwise a new configuration will be created.

Supported configurations

The bundle offers support for configuration in the following formats :

  • boolean
  • string
  • text
  • integer

Available methods

 public function getBool($group, $label, bool $default): bool public function setBool(string $group, string $label, bool $value): void public function getString($group, $label, string $default): string public function setString($group, $label, string $value): void public function getText($group, $label, string $default): string public function setText($group, $label, string $value): void public function getInt($group, $label, string $default): int

Twig integration

{{ get_config_value('type', 'group', 'label', 'default') }}

Initialise new configurations (Warm-up)

A command allows you to initialise new configurations. We suggest to execute it everytime your app starts.

bin/console lle:config:warmup 

To configure the default values, create a class that implements WarmupInterface.

<?php namespace App\Warmup; use Lle\ConfigBundle\Contracts\WarmupInterface; use Lle\ConfigBundle\Repository\AbstractConfigRepository; class ConfigWarmup implements WarmupInterface { public function warmup(AbstractConfigRepository $configRepository): void { $configRepository->initBool('CONFIG', 'active', true); } }

Do not use set*(), because it will overwrite defined configurations.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04