modera / config-bundle
包提供工具,允许您以灵活的方式动态存储和检索配置属性。
Requires
- php: >=5.6
- doctrine/doctrine-bundle: ^1.6|^2.0
- doctrine/orm: ^2.5
- modera/foundation-bundle: ~3.1|~4.0
- sergeil/expander-bundle: ^1.0
- symfony/framework-bundle: ^3.4|^4.0
- twig/twig: ~1.34|~2.4
Requires (Dev)
- phake/phake: ^2.0
- phpunit/phpunit: ^5.5
- dev-master / 4.x-dev
- 3.x-dev
- v3.1.0
- v3.0.0
- 2.x-dev
- v2.57.0
- v2.56.0
- v2.55.0
- v2.54.0
- v2.53.0
- v2.52.2
- v2.52.1
- v2.52.0
- v2.51.1
- v2.51.0
- v2.0.50
- dev-sf3
- dev-MPFE-1005
- dev-MPFE-1007
- dev-MPFE-1014
- dev-MPFE-1014-2
- dev-MPFE-1017
- dev-MPFE-1029
- dev-MPFE-963
- dev-MPFE-975
- dev-MPFE-977
- dev-crub_pagination_fetch_fix
- dev-direct-bundle-csrf
This package is auto-updated.
Last update: 2024-08-29 03:47:18 UTC
README
包提供工具,允许您以灵活的方式动态存储和检索配置属性。您可以存储任何类型的配置属性 - 您的配置属性可以存储简单的值(如字符串、整数、数组)或复杂的值 - 如对象或实体引用,这是通过使用所谓的“处理器”(\Modera\ConfigBundle\Config\HandlerInterface
的实现)来实现的。
安装
步骤 1: 下载包
composer require modera/config-bundle:4.x-dev
此命令要求您全局安装了Composer,如Composer文档中的安装章节中所述。
步骤 2: 启用包
此包应通过Flex自动启用。如果您不使用Flex,您需要手动通过在项目config/bundles.php
文件中添加以下行来启用包
<?php // config/bundles.php return [ // ... Modera\ConfigBundle\ModeraConfigBundle::class => ['all' => true], ];
发布配置属性
在您可以使用配置属性之前,您需要发布它们。发布过程包括几个步骤
- 创建一个提供者类。
- 使用“modera_config.config_entries_provider”标签在服务容器中注册您的提供者类。
- 使用
modera:config:install-config-entries
命令发布暴露的配置条目。
以下是一个简单的提供者类的示例
<?php namespace MyCompany\SiteBundle\Contributions; use Modera\ConfigBundle\Config\ConfigurationEntryDefinition as CED; use Sli\ExpanderBundle\Ext\ContributorInterface; class ConfigEntriesProvider implements ContributorInterface { private $em; public function __construct(EntityManager $em) { $this->em = $em; } /** * {@inheritdoc} */ public function getItems() { $serverConfig = array( 'id' => 'modera_config.entity_repository_handler' ); $admin = $this->em->find('MyCompany\SecurityBundle\Entity\User', 1); return array( new CED('admin_user', 'Site administrator', $admin) ); } }
一旦您有一个类,您需要将其注册到服务容器中
<services> <service id="my_company_site.contributions.config_entries_provider" class="MyCompany\SiteBundle\Contributions\ConfigEntriesProvider"> <tag name="modera_config.config_entries_provider" /> </service> </services>
现在我们可以使用modera:config:install-config-entries
来发布我们的配置属性。
检索配置属性
为了在您的应用程序代码中检索配置属性,您需要使用modera_config.configuration_entries_manager
服务。
<?php /* @var \Modera\ConfigBundle\Manager\ConfigurationEntriesManagerInterface $service */ $service = $container->get('modera_config.configuration_entries_manager'); /* @var \Modera\ConfigBundle\Config\ConfigurationEntryInterface $entry */ $entry = $service->findOneByNameOrDie('admin_user'); // will yield "MyCompany\SecurityBundle\Entity\User" echo get_class($property->getValue());
Twig集成
该包还提供了与Twig的集成,允许您从模板中检索配置属性的值。为此,您需要使用modera_config_value
函数
{{ modera_config_value("my_property_name") }}
这将打印“my_property_name”配置属性的值。默认情况下,如果未找到配置属性,则会抛出异常,但您可以通过将函数的第二个参数传递为FALSE来更改此行为,在这种情况下,将返回NULL而不是抛出异常。
正如您在本文件稍后所读到的,该包还支持将配置条目与用户关联。要从模板中检索特定于用户的配置属性,请使用modera_config_owner_value
,例如
{{ modera_config_value("my_property_name", app.user) }}
处理器
默认情况下,该包能够存储以下类型的值
- 字符串
- 文本
- 浮点数
- 数组
- 布尔值
- 实体引用
如果您需要存储更复杂的值,则需要实现\Modera\ConfigBundle\Config\HandlerInterface
接口。请参阅已提供的实现(例如\Modera\ConfigBundle\Config\EntityRepositoryHandler
),了解如何创建自己的处理器。
创建与用户相关的配置条目
有时您可能想存储与整个系统无关的配置条目,而是与单个用户相关,例如 - 用户的首选管理面板语言。为了实现这一点,您需要使用modera_config/owner_entity
语义配置密钥来指定用户实体的完全限定名称。例如
modera_config: owner_entity: "Modera\SecurityBundle\Entity\User"
一旦配置了owner_entity
,不要忘记通过运行doctrine:schema:update --force
来更新您的数据库模式。
现在我们已经设置了适当的配置,并且在创建新的配置条目时更新了数据库模式,您可以为“所有者”指定,例如
<?php $bob = new \Modera\SecurityBundle\Entity\User(); // ... configure and persist $bob $ce = new ConfigurationEntry(); $ce->setOwner($myUser); $manager->save($ce);
提示
在您的应用程序代码中,当使用来自ModeraConfigBundle的组件时,您应该依赖接口而不是实现,也就是说,当您使用modera_config.configuration_entries_manager
时,依赖于\Modera\ConfigBundle\Manager\ConfigurationEntriesManagerInterface,当处理配置条目时,依赖于\Modera\ConfigBundle\Manager\ConfigurationEntryInterface。这样,您将使您的代码具有可移植性。默认情况下,该捆绑包使用Doctrine ORM来存储配置条目的值,但以后可能还会添加更多存储机制,如果您依赖接口,那么您不需要更新您的代码以利用新的可能存储引擎。
许可
此捆绑包受MIT许可证的约束。请参阅捆绑包中的完整许可证:Resources/meta/LICENSE