intentsolutions/symfony-lombok-bumbu 问题修复 & 功能扩展

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

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

intentsolutions/symfony-lombok-bumbu

Composer 安装命令:

composer require intentsolutions/symfony-lombok-bumbu

包简介

README 文档

README

Welcome to Bumbu! This library aims to simplify your Symfony development by providing powerful annotations that automate repetitive tasks. Here you will find detailed documentation on how to install, configure, and use Bumbu in your Symfony projects.

Table of Contents

  1. Introduction
  2. Installation
  3. Configuration
  4. Attributes
  5. Usage Examples
  6. Contributing
  7. License

Introduction

Bumbu is a Symfony library inspired by Lombok, designed to reduce boilerplate code in your projects. With Bumbu, you can use custom attributes to streamline common tasks such as request handling, validation, and getter/setter generation.

Installation

To install Bumbu, use Composer:

composer require intentsolutions/symfony-lombok-bumbu:dev-main

Configuration

After installing Bumbu, you need to enable it in your Symfony project.

  1. Add Bumbu to bundles.php:
return [
   // Other bundles
   IS\Bumbu\BumbuBundle::class => ['all' => true],
];
  1. Update Doctrine Configuration in config/packages/doctrine.yaml:
doctrine:
    orm:
        class_metadata_factory_name: IS\Bumbu\Doctrine\CustomClassMetadataFactory

Attributes

Bumbu provides several useful attributes to make your Symfony development smoother:

RequestBody

The RequestBody attribute maps the request body directly to a parameter in your controller action.

Usage:

use IS\Bumbu\Attribute\RequestBody;

class MyController extends AbstractController
{
    #[Route('/endpoint', name: 'endpoint', methods: ['POST'])]
    public function myAction(#[RequestBody] MyRequestDto $dto)
    {
        // $dto will be automatically filled with data from the request body
    }
}

Valid

The Valid attribute triggers validation on the given parameter. It is often used together with the RequestBody attribute to validate data automatically when it is mapped from the request body.

use IS\Bumbu\Attribute\RequestBody;
use IS\Bumbu\Attribute\Valid;

class MyController extends AbstractController
{
    #[Route('/endpoint', name: 'endpoint', methods: ['POST'])]
    public function myAction(#[RequestBody, Valid] MyRequestDto $dto)
    {
        // The $dto parameter will be populated from the request body and validated automatically
    }
}

Getter

The Getter attribute generates a getter method for a specified property.

use IS\Bumbu\Attribute\Getter;

class MyEntity
{
    #[Getter]
    private $property;

    // Bumbu will create a getProperty() method
}

Setter

The Setter attribute generates a setter method for a specified property.

use IS\Bumbu\Attribute\Setter;

class MyEntity
{
    #[Setter]
    private $property;

    // Bumbu will create a setProperty($value) method
}

Usage Examples

Example 1: Using RequestBody and Valid

use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Type;

class MyRequestDto
{
    #[NotBlank]
    #[Length(min: 3)]
    private string $name;

    #[NotBlank]
    #[Type(type: 'integer')]
    private int $age;

    #[NotBlank]
    #[Email]
    private string $email;
}
use IS\Bumbu\Attribute\RequestBody;
use IS\Bumbu\Attribute\Valid;

class MyController extends AbstractController
{
    #[Route('/submit', name: 'submit', methods: ['POST'])]
    public function submit(#[RequestBody, Valid] MyRequestDto $dto)
    {
        // Handle the validated request DTO
    }
}

Example 2: Using Get and Set

use IS\Bumbu\Attribute\Get;
use IS\Bumbu\Attribute\Set;

class User
{
    #[Getter]
    #[Setter]
    private $username;

    #[Getter]
    private $email;

    #[Setter]
    private $password;

    // Bumbu will generate getUsername(), setUsername($value), getEmail(), and setPassword($value) methods
}

Note: To use the User class with the generated getter and setter methods, you should create an instance of it through dependency injection (DI) where applicable.

use App\Entity\User;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;

class SomeController extends AbstractController
{
    public function someAction(User $user): Response
    {
        $username = $user->getUsername();
        // Use $username and other methods as needed

        return new Response('User username is ' . $username);
    }
}

Contributing

I welcome contributions to Bumbu! If you want to help out, please follow these steps:

  1. Fork the repository.
  2. Create a new branch for your feature or bugfix.
  3. Implement your changes and add tests.
  4. Submit a pull request with a clear description of what you’ve done.

License

Bumbu is licensed under the MIT License. See the LICENSE file for more details.

Thanks for using Bumbu! I hope it makes your Symfony development easier. If you have any questions or feedback, feel free to open an issue on the GitHub repository.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-08-01