fervo/enum-bundle 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

fervo/enum-bundle

Composer 安装命令:

composer require fervo/enum-bundle

包简介

A library to integrate enums into the Symfony framework

README 文档

README

Provides a MyCLabs\Enum integration with Doctrine for your Symfony projects.

Installation

Step 1: Download the Bundle

$ composer require fervo/enum-bundle "^2.0"

Step 2: Enable the Bundle

<?php

// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...

            new Fervo\EnumBundle\FervoEnumBundle(),
        );

        // ...
    }

    // ...
}

Step 3: Configure your enum

fervo_enum:
    fqcn_choice_label_prefix: true  # For Backward compatibily, should be explicitely set to true
    enums:
        AppBundle\Enum\Gender:
            doctrine_type: gender # Type name used in doctrine annotations
            form_type: gender # Used in translation keys

Step 4: Create your enum

<?php

namespace AppBundle\Enum\Gender;

use MyCLabs\Enum\Enum;

class Gender extends Enum
{
    const MALE = 'male';
    const FEMALE = 'female';
}

Step 5: Use the enum in a doctrine entity

<?php

namespace AppBundle\Entity;

use AppBundle\Enum\Gender;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity()
 */
class Person
{
    // ...

    /**
     * @ORM\Column(type="gender")
     */
    protected $gender;

    // ...

    public function getGender()
    {
        return $this->gender;
    }

    public function setGender(Gender $gender)
    {
        $this->gender = $gender;
    }

    // ...
}

Step 6: Use the enum in Symfony forms

The bundle auto-generates a corresponding form type for each configured enum. The FQCN for the form type is on the format FervoEnumBundle\Generated\Form\{{enum class name}}Type. So with the enum class in the example above, it could be used in a form type in the following way.

<?php

namespace AppBundle\Form\Type;

use FervoEnumBundle\Generated\Form\GenderType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class EmployeeType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
        	// ...
            ->add('gender', GenderType::class)
            // ...
        ;
    }
}

If the underlying object of the form type is a doctrine mapped entity, the type can also be guessed by the framework. But it is a good practice to always specify the FQCN in form types.

Or you can use EnumType with configured options:

<?php

namespace AppBundle\Form\Type;

use AppBundle\Enum\Gender;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class EmployeeType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
        	// ...
            ->add('gender', EnumType::class, [
                'class' => Gender::class,
                'choice_label_prefix' => 'gender', // optional
            ])
            // ...
        ;
    }
}

Step 7: Specify translations for the enum values

The form type looks by default for the translation of the enum values in the enums translation domain. The translation keys are on the format {{configured form_type name}}.{{enum constant value}}. So going with the example the translation keys would be gender.male and gender.female.

Additional functionality

Use the enum with Symfony @ParamConverter

<?php

namespace AppBundle\Controller;

use AppBundle\Enum\Gender;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class EmployeeController extends Controller
{
    /**
     * @ParamConverter("gender")
     */
    public function indexAction(Gender $gender)
    {
        // ...
    }
}

Use the enum with JMS\Serializer

<?php

namespace AppBundle\Entity;

use AppBundle\Enum\Gender;
use JMS\Serializer\Annotation as JMS;

class Person
{
    // ...

    /**
     * @JMS\Type("gender")
     */
    protected $gender;

    // ...

    public function getGender()
    {
        return $this->gender;
    }

    public function setGender(Gender $gender)
    {
        $this->gender = $gender;
    }

    // ...
}

fervo/enum-bundle 适用场景与选型建议

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 115.64k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 11
  • 点击次数: 5
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 11
  • Watchers: 3
  • Forks: 10
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-02-10