2lenet / config-bundle
配置包
Requires
- 2lenet/crudit-bundle: *
- symfony/framework-bundle: ^6.0|^7.0
- symfony/orm-pack: *
- twig/twig: ^3.4
Requires (Dev)
- ergebnis/phpstan-rules: ^2.0
- phpstan/phpstan: ^1.9
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-doctrine: ^1.3
- phpstan/phpstan-mockery: ^1.1
- phpstan/phpstan-phpunit: ^1.3
- phpstan/phpstan-strict-rules: ^1.5
- phpstan/phpstan-symfony: ^1.2
- phpunit/phpunit: ^10.0
- slevomat/coding-standard: ^8.0
- squizlabs/php_codesniffer: ^3.5
This package is auto-updated.
Last update: 2024-09-23 06:51:46 UTC
README
Symfony 插件,为您的应用程序提供易于配置的功能。非常适合与著名的 CRUD 插件 Crudit 一起使用
安装
该插件尚未在 Packagist 上,请确保将以下内容添加到您的 composer.json
文件中
{ "url": "https://github.com/2lenet/ConfigBundle", "type": "git" }
使用 Composer 安装
composer require 2lenet/config-bundle
该插件非常灵活,旨在满足您的项目需求,它只包含用于您自己的配置实体的 trait。
您还将获得一个准备就绪的 Symfony 存储库。
在您的实体目录中创建名为 Config
的类,它必须实现 ConfigInterface。如果不需要自定义,您可以使用
<?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; }
您的存储库文件必须扩展来自插件的 ConfigRepository
<?php namespace App\Repository; use App\Entity\Config; use Doctrine\Persistence\ManagerRegistry; use Lle\ConfigBundle\Repository\AbstractConfigRepository; /** * @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) { parent::__construct($registry, Config::class); } ... }
在您的项目中,将以下内容添加到配置文件中: /config/packages/doctrine.yaml
doctrine: orm: resolve_target_entities: Lle\ConfigBundle\Contracts\ConfigInterface: App\Entity\Config
在 /config/routes.yaml
中添加
lle_config: resource: "@LleConfigBundle/Resources/config/routes.yaml"
然后您可以创建一个迁移
bin/console make:migration
检查创建的迁移文件,并让 doctrine 执行迁移
bin/console doctrine:migrations:migrate
您就可以开始使用了!
自定义
如果您需要更多选项或实体字段,您可以在实体类中添加它们
<?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; } }
您可能还需要比存储库文件中更多的选项,在这种情况下,在您的项目中创建一个新的存储库类。别忘了更新实体中使用的命名空间(参见前面的示例)。
使用
概述
要使用该插件,请在您的服务中注入配置存储库,并使用以下 可用方法 之一。该插件将检查配置是否存在,如果不存在,则创建新的配置。
支持的配置
该插件支持以下格式的配置
- 布尔值
- 字符串
- 文本
- 整数
可用方法
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 集成
{{ get_config_value('type', 'group', 'label', 'default') }}
初始化新的配置(预热)
一个命令允许您初始化新的配置。我们建议在应用程序启动时执行它。
bin/console lle:config:warmup
要配置默认值,创建一个实现 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); } }
不要使用 set*(),因为它将覆盖已定义的配置。