intentsolutions / symfony-lombok-bumbu
dev-main
2024-08-01 09:28 UTC
This package is not auto-updated.
Last update: 2024-09-27 08:29:36 UTC
README
欢迎使用Bumbu!这个库旨在通过提供强大的注解来简化您的Symfony开发,自动化重复性任务。在这里,您可以找到有关如何安装、配置和使用Bumbu在您的Symfony项目中详细的文档。
目录
简介
Bumbu是一个受Lombok启发的Symfony库,旨在减少项目中的样板代码。使用Bumbu,您可以使用自定义属性来简化常见的任务,如请求处理、验证和getter/setter生成。
安装
要安装Bumbu,请使用Composer
composer require intentsolutions/symfony-lombok-bumbu:dev-main
配置
安装Bumbu后,您需要在您的Symfony项目中启用它。
- 将Bumbu添加到bundles.php
return [ // Other bundles IS\Bumbu\BumbuBundle::class => ['all' => true], ];
- 在config/packages/doctrine.yaml中更新Doctrine配置
doctrine: orm: class_metadata_factory_name: IS\Bumbu\Doctrine\CustomClassMetadataFactory
属性
Bumbu提供了几个有用的属性,使您的Symfony开发更加顺畅
RequestBody
RequestBody属性将请求体直接映射到控制器操作中的参数。
用法
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
Valid属性在给定的参数上触发验证。它通常与RequestBody属性一起使用,以在数据从请求体映射时自动验证数据。
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
Getter属性为指定的属性生成getter方法。
use IS\Bumbu\Attribute\Getter; class MyEntity { #[Getter] private $property; // Bumbu will create a getProperty() method }
Setter
Setter属性为指定的属性生成setter方法。
use IS\Bumbu\Attribute\Setter; class MyEntity { #[Setter] private $property; // Bumbu will create a setProperty($value) method }
使用示例
示例1:使用RequestBody和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 } }
示例2:使用Get和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 }
注意:为了使用具有生成的getter和setter方法的User类,您应该通过依赖注入(DI)在适用的情况下创建其实例。
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); } }
贡献
我欢迎对Bumbu的贡献!如果您想帮忙,请按照以下步骤操作
- 分支仓库。
- 为您的功能或错误修复创建一个新分支。
- 实现您的更改并添加测试。
- 提交一个带有您所做更改的清晰描述的拉取请求。
许可
Bumbu在MIT许可下发布。有关更多详细信息,请参阅LICENSE文件。
感谢您使用Bumbu!希望它使您的Symfony开发更加容易。如果您有任何问题或反馈,请随时在GitHub仓库上打开一个问题。