承接 biig/dictionary-bundle 相关项目开发

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

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

biig/dictionary-bundle

Composer 安装命令:

composer require biig/dictionary-bundle

包简介

Are you often tired to repeat static choices like gender or civility in your apps ?

关键字:

README 文档

README

Build Status Scrutinizer Code Quality

Are you often tired to repeat static choices like gender or civility in your apps ?

Notice: This bundle is a fork from KnpLabs/DictionaryBundle. We are working on releasing a 3.0 version but for now the released version is 2.x. You should refere to the right branch to have a good documentation.

Requirements

  • Symfony >= 3.4
  • PHP >= 7.1

Installation

Add the DictionaryBundle to your composer.json:

{
    "require": {
        "biig/dictionary-bundle": "^3.0"
    }
}

Or install it directly with composer command

composer require biig/dictionary-bundle

Register the bundle in app/AppKernel.php

$bundles = array(
    // ...
    new Knp\DictionaryBundle\KnpDictionaryBundle(),
);

Basic usage

Define dictionaries in your config.yaml file:

knp_dictionary:
    dictionaries:
        my_dictionary:      # your dictionary name
            - Foo           # your dictionary content
            - Bar
            - Baz

You will be able to retrieve it through the dictionary registry service:

$container->get('knp_dictionary.registry')->get('my_dictionary');

Dictionary form type

Now, use them in your forms:

use Knp\DictionaryBundle\Form\Type\DictionaryType;

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('civility', DictionaryType::class, [
            'name' => 'my_dictionary'
        ])
    ;
}

The dictionary form type extends the symfony's choice type and its options.

Validation constraint

You can also use the constraint for validation. The value have to be set.

use Knp\DictionaryBundle\Validator\Constraints\Dictionary;

class User
{
    /**
     * @ORM\Column
     * @Dictionary(name="my_dictionary", multiple=false)
     */
    private $civility;
}

Advanced usage

You can specify the indexation mode of each dictionary

knp_dictionary:
    dictionaries:
        my_dictionary:                  # your dictionary name
            type: 'key_value'           # your dictionary type
            content:                    # your dictionary content
                "foo": "foo_value"
                "bar": "bar_value"
                "baz": "baz_value"

Available types

  • value (default) : Natural indexation
  • value_as_key: Keys are defined from their value
  • key_value: Define your own keys
  • callable: Build a dictionary from a callable

Callable dictionary

You can create a callable dictionary:

knp_dictionary:
    dictionaries:
        my_callable_dictionary:         # your dictionary name
            type: 'callable'            # your dictionary type
            service: 'app.service.id'   # a valid service from your application
            method: 'getSomething'      # the method name to execute

Callable dictionaries are loaded with a lazy strategy. It means that the callable will not be called if you do not use the dictionary.

Categories in dictionaries

If you want to retrieve some groups of dictionaries from the registry, then you can specify a category to your dictionary.

knp_dictionary:
    dictionaries:
        my_dictionary:                  # your dictionary name
            type: 'key_value'           # your dictionary type
            category: 'product.toy'     # your dictionary category
            content:                    # your dictionary content
                foo: foo_value
                bar: bar_value
                baz: baz_value

Get a registry for this category later:

// Retrieve all dictionaries of a special category
$dictionaries = $registry->filterByCategory('product.toy')->all();

Transformers

For now, this bundle is only able to resolve your class constants:

my_dictionary:
    - MyClass::MY_CONSTANT
    - Foo
    - Bar

You want to add other kinds of transformations for your dictionary values ? Feel free to create your own transformer !

Add your own transformers

Create your class that implements TransformerInterface. Load your transformer and tag it as knp_dictionary.value_transformer.

services:
    my_bundle.my_namespace.my_transformer:
    	class: %my_transformer_class%
    	tags:
        	- { name: knp_dictionary.value_transformer }

Use your dictionary in twig

You can also use your dictionary in your Twig templates via calling dictionary function (or filter)

{% for example in dictionary('examples') %}
    {{ example }}
{% endfor %}

But you can also access directly to a value by using the same function (or filter)

{{ 'my_key'|dictionary('dictionary_name') }}

Faker provider

The KnpDictionaryBundle comes with a faker provider that can be used to provide a random entry from a dictionary.

Alice

To register the provider in nelmio/alice, you can follow the official documentation

or ...

if you use the awesome knplabs/rad-fixtures-load library, the dictionary provider will be automaticaly loaded for you :)

App\Entity\User:
    john_doe:
        firstname: John
        latnale: Doe
        city: <dictionary('cities')>

Use dictionary command

You can use the following command to show your app dictionaries easily:

php bin/console knp:dictionary:dump

If you want to display only one dictionary, you can set it name in argument

php bin/console knp:dictionary:dump my_dictionary

Create your own dictionary implementation

Dictionary

Your dictionary implementation must implements the interface Dictionary. In Symfony >= 3.3, your class will be automatically register as dictionary service.

Dictionary Factory

You must create a dictionary factory that will be responsible to instanciate your dictionary.

services:
    # Syntax Symfony >= 3.3
    App\Dictionary\Factory\MyCustomFactory:
        tags:
            { name: 'knp_dictionary.factory' }

biig/dictionary-bundle 适用场景与选型建议

biig/dictionary-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11.17k 次下载、GitHub Stars 达 8, 最近一次更新时间为 2018 年 07 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「dictionary」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 8
  • Watchers: 7
  • Forks: 24
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-07-06