kent013/artisan-command-pseudo-ca 问题修复 & 功能扩展

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

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

kent013/artisan-command-pseudo-ca

最新稳定版本:v0.0.4

Composer 安装命令:

composer require kent013/artisan-command-pseudo-ca

包简介

An artisan command to generate usecase / request / resource classes to implement pseudo clean architecture.

README 文档

README

Artisan make command to generate usecase / request / resource classes to implement pseudo clean architecture.

This command is based on the article about pseudo clean architecture.

Pseudo / Relaxed Clean Architecture

pseudo clean architecture for laravel is a relaxed domain modelling architecture which consists with Request, Resource and Usecase. Again this architecture is proposed by mpyw. And I'm using this architecture in my several projects. so that I just need a class generator.

According to the article written by mpyw, Request is like

class StoreRequest extends FormRequest
{
    public function authorize(Gate $gate): bool
    {
        $community = $this->route()->parameter('community');
	    return $gate->authorize('store', [Post::class, $community]);
    }

    public function rules(): array
    {
        return [
            'title' => 'required|string|max:30',
            'body' => 'required|string|max:10000',
        ];
    }

    public function makePost(): Post
    {
        return new Post($this->validated());
    }
}

Resource is like

class PostResource extends JsonResource
{
    public function toArray($request): array
    {
        return [
            'title' => $this->resource->title,
            'body' => $this->resource->body,
            'created_at' => $this->resource->created_at,
            'updated_at' => $this->resource->updated_at, 
        ];
    }
}

Usecase is like

class StoreAction
{
    public function storeAfterDomainValidation(User $user, Community $community): self
    {
        $userPostsCountToday = $user
            ->posts()
            ->where('community_id', $community->id)
            ->where('created_at', '>=', Carbon::midnight())
            ->count();
        if ($userPostsCountToday >= 200) {
            throw new PostLimitExceededException('Exceeded');
        }
        
        $post->save();
        return $post;
    }
}

And controller with Request, Resource and Usecase goes like

class PostContoller
{
    public function store(StoreRequest $request, StoreAction $action): PostResource
    {
        $post = $request->makePost();

        try {
            return new PostResource($action($user, $community, $post));
        } catch (PostLimitExceededException $e) {
            throw new TooManyRequestsHttpException(null, $e->getMessage(), $e);
        }
    }
}

Installation

composer require --dev kent013/artisan-command-pseudo-ca

Generate config file

php artisan vendor:publish --tag="pseudoca"

Configuration

PSEUDOCA_USECASE_NS="\UseCases"
PSEUDOCA_REQUEST_NS="\Http\Requests"
PSEUDOCA_RESOURCE_NS="\Http\Resources"

If you want to change namespaces, please add above lines in your .env file and change values.

Generate Usecase

php artisan make:pseudoca:usecase LoginAction

will generates LoginAction class under root namespace suffixed with PSEUDOCA_USECASE_NS. Default namespace is \UseCase.

Generate Request

php artisan make:pseudoca:request LoginRequest

will generates LoginRequest class under root namespace suffixed with PSEUDOCA_REQUEST_NS. Default namespace is \Http\Requests.

Generate Resource

php artisan make:pseudoca:resource LoginResource

will generates LoginResource class under root namespace suffixed with PSEUDOCA_RESOURCE_NS. Default namespace is \Http\Resources.

Generate PseudoCA classes at once

php artisan make:pseudoca Login

will generates LoginResource, LoginRequest and LoginUsecase.

统计信息

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

GitHub 信息

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

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固