定制 modera/config-bundle 二次开发

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

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

modera/config-bundle

Composer 安装命令:

composer require modera/config-bundle

包简介

Bundles provides tools that allow to you to dynamically store and fetch your configuration properties in a flexible way.

README 文档

README

Bundles provides tools that allow to you to dynamically store and fetch your configuration properties in a flexible way. You can store any type of configuration property - your configuration property can store both simple values (like string, integers, arrays) or complex ones - like objects or references to entities, this is achieved by using so called "Handlers" (implementations of \Modera\ConfigBundle\Config\HandlerInterface).

Installation

Step 1: Download the Bundle

composer require modera/config-bundle:5.x-dev

This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.

Step 2: Enable the Bundle

This bundle should be automatically enabled by Flex. In case you don't use Flex, you'll need to manually enable the bundle by adding the following line in the config/bundles.php file of your project:

<?php
// config/bundles.php

return [
    // ...
    Modera\ConfigBundle\ModeraConfigBundle::class => ['all' => true],
];

Publishing configuration properties

Before you can use your configuration properties you need to publish them. Publishing process consists of several steps:

  1. Create a provider class.
  2. Register your provides class in service container with "modera_config.config_entries_provider" tag.
  3. Use modera:config:install-config-entries command to publish exposed configuration entries.

This is how a simple provider class could look like:

<?php

namespace MyCompany\SiteBundle\Contributions;

use Modera\ConfigBundle\Config\ConfigurationEntryDefinition as CED;
use Modera\ExpanderBundle\Ext\ContributorInterface;

class ConfigEntriesProvider implements ContributorInterface
{
    private $em;

    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }

    public function getItems(): array
    {
        $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)
        );
    }
}

Once you have a class you need to register it in a service container:

<services>
    <service id="my_company_site.contributions.config_entries_provider"
             class="MyCompany\SiteBundle\Contributions\ConfigEntriesProvider">

        <tag name="modera_config.config_entries_provider" />
    </service>
</services>

Now we can use modera:config:install-config-entries to publish our configuration property.

Fetching configuration properties

In order to fetch a configuration property in your application code you need to use modera_config.configuration_entries_manager service.

<?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 integration

The bundle also provides integration with Twig that allow you to fetch configuration properties' values from your template. For this you will want to use modera_config_value function:

{{ modera_config_value("my_property_name") }}

This will print value for "my_property_name" configuration property. By default if no given configuration property is found then exception is thrown but you can change this behaviour by passing FALSE as second argument to the function and in this case NULL be returned instead of throwing an exception.

As you will read later in this document the bundle also has support for associating configuration entries with users. To fetch a user specific configuration property from a template use modera_config_owner_value, for example:

{{ modera_config_value("my_property_name", app.user) }}

Handlers

By default the bundle is capable of storing these types of values:

  • string
  • text
  • float
  • array
  • boolean
  • references to entities

If you need to store some more complex values then you need to implement \Modera\ConfigBundle\Config\HandlerInterface interface. Please see already shipped implementations (\Modera\ConfigBundle\Config\EntityRepositoryHandler, for example) to see how you can create your own handlers.

Creating user related configuration entries

Sometimes you may want to store configuration entries which are not related to the system as a whole but instead to one single user, for example - user's preferred admin panel language. To achieve this you need to use modera_config/owner_entity semantic configuration key to specify a fully qualified name of user entity. For example:

modera_config:
    owner_entity: "Modera\SecurityBundle\Entity\User"

Once owner_entity is configured don't forget to update your database schema by running doctrine:schema:update --force.

Now that we have proper configuration in place and database schema has been updated when creating new configuration entries you can specify "owner", for example:

<?php

$bob = new \Modera\SecurityBundle\Entity\User();
// ... configure and persist $bob

$ce = new ConfigurationEntry();
$ce->setOwner($myUser);

$manager->save($ce);

Hints

In your application code when working with components from ModeraConfigBundle you should rely on interfaces instead of implementations, that is, when you use modera_config.configuration_entries_manager rely on \Modera\ConfigBundle\Manager\ConfigurationEntriesManagerInterface and when working with configuration entries rely on \Modera\ConfigBundle\Manager\ConfigurationEntryInterface this way you will make your code portable. By default the bundle uses Doctrine ORM to store values for configuration entries but later some more storage mechanism may be added and if you rely on interfaces then you won't need to update your code to leverage new possible storage engines.

Licensing

This bundle is under the MIT license. See the complete license in the bundle: Resources/meta/LICENSE

modera/config-bundle 适用场景与选型建议

modera/config-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.64k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2015 年 01 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 modera/config-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 modera/config-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 modera/config-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-01-12