承接 mhujer/consistence-bundle 相关项目开发

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

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

mhujer/consistence-bundle

Composer 安装命令:

composer require mhujer/consistence-bundle

包简介

Add translator and form elements for enums from consistence/consistence

README 文档

README

Build Status Latest Stable Version Total Downloads License Coverage Status

This Bundle provides translator service, translator twig filter and form type for consistence/consistence enums.

Installation

Applications that use Symfony Flex

Open a command console, enter your project directory and execute:

$ composer require mhujer/consistence-bundle

Applications that don't use Symfony Flex

Step 1: Download the Bundle

Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:

$ composer require mhujer/consistence-bundle

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

Step 2: Enable the Bundle

Then, enable the bundle by adding it to the list of registered bundles in the config/bundles.php file of your project:

// config/bundles.php

return [
    // ...
    Mhujer\ConsistenceBundle\MhujerConsistenceBundle::class => ['all' => true],
];

Usage

Examples consider having the following enum:

<?php declare(strict_types = 1);

namespace App\Card;

final class CardColor extends \Consistence\Enum\Enum
{

    public const BLACK = 'black';
    public const RED = 'red';

}

Translation file

The translator automatically converts an instance of enum to a translation key consisting of FQCN of the enum, colon and its value, e.g.:

App\Card\CardColor:red

Best approach is to create a translation file using the PHP format (enums.en.php) which allows you to use the class name:

<?php declare(strict_types = 1);

use App\Card\CardColor;

return [
    CardColor::class . ':' . CardColor::RED => 'red',
    CardColor::class . ':' . CardColor::BLACK => 'black',
];

As you might have noticed, the translation domain is set to enums.

Twig

In Twig templates you can use transEnum filter to convert an enum to a translated string:

{{ variableContainingEnum | transEnum }}

Translation domain

Sometimes it is useful to have different translations for the same enum (e.g. when the enum is used in admin and frontend UI). This is achieved by an translationDomain parameter which can be passed to transEnum:

{{ variableContainingEnum | transEnum('enums-frontend') }}

It loads translations transparently from another domain using Symfony translator:

// enums-frontend.en.php
<?php declare(strict_types = 1);

use App\Card\CardColor;

return [
    CardColor::class . ':' . CardColor::RED => 'Red',
    CardColor::class . ':' . CardColor::BLACK => 'Black',
];

Forms

In forms, you can use EnumType as a field type. You need to set an option enum_class to an enum class:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('cardColor', EnumType::class, [
            'enum_class' => CardColor::class,
            'label' => 'Card Color',
        ])
    //...

Property in your request object should look like this (it contains an instance of CardColor):

/**
 * @Assert\NotBlank()
 * @var \App\Card\CardColor
 */
public $cardColor;

Requirements

Works with PHP 7.4 or higher and Symfony 5.4 or higher.

Submitting bugs and feature requests

Bugs and feature request are tracked on GitHub

Author

Martin Hujer

Changelog

2.0.1 (2023-12-09)

  • add Symfony 7 and PHP 8.2 support (dfridrich)

2.0.0 (2022-09-20)

  • require PHP 8.1
  • add support for native enums to make migration easier

1.4.0 (2021-12-04)

  • require Symfony 5.4+
  • allow PHP 8.1
  • allow Symfony 6.0

1.3.0 (2021-06-10)

  • BC break: optional parameter in transEnum is treated as translation domain

1.2.0 (2021-06-03)

  • add optional parameter $enumNamespace to transEnum method

1.1.0 (2021-02-28)

  • allow PHP 8.0
  • require PHP 7.4+

1.0.2 (2020-01-13)

  • Fixed #3: Undefined "translator" dependency in services.yaml

1.0.1 (2019-11-24)

  • Symfony 5 and Twig 3 compatibility

1.0.0 (2019-11-06)

  • initial release

mhujer/consistence-bundle 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-11-05