pixelcone/fraction

此包的最新版本(v0.1.1)没有可用的许可信息。

一组架构抽象,帮助您构建结构化且易于维护的PHP应用程序

v0.1.1 2024-04-25 17:43 UTC

This package is auto-updated.

Last update: 2024-09-25 18:51:21 UTC


README

Fraction 是一套抽象,为PHP开发者提供了一个方便地将业务逻辑存储在应用程序中的方法。

这个包的想法是在重构一个用Laravel编写的项目的过程中产生的。这个包受到了许多不同作者(包、文章)作品的启发。以下是一些例子

先决条件

在开始之前,请确保您已经满足以下要求

  • PHP ^8.0

实际上没有其他要求。该包对Laravel友好,所以如果您将其安装到Laravel项目中,您将能够在Actions/Features的handle()方法中使用依赖注入(更多内容请见下文)。

安装Fraction

要安装Fraction,请运行composer require命令

composer require pixelcone/fraction:^0.1.1

使用Fraction

此包引入了两个抽象层:Actions和Features。

Actions

Actions是整个应用程序的基本“生命单元”。简而言之,Actions是常规PHP类,专注于执行一项特定的任务(例如,将用户保存到数据库或发送电子邮件)。Actions可以通过构造函数接受不同的参数,并可以从代码中的任何地方调用(最好是从应用程序或同一领域,尽管如此)。强烈建议每个动作类只公开一个方法,以帮助开发者避免将其与额外逻辑污染。动作的名称应该反映其实际执行的操作,通过回答“它应该做什么?”这个问题。Actions是领域的组成部分,位于自己的Actions文件夹中。Actions也可以用作Laravel作业。

动作类及其使用的示例

<?php

namespace App\Actions;

use App\Models\User;
use Pixelcone\Fraction\AsAction;

class CreateUserAction {
    use AsAction;

    public function __construct(
        protected string $fullName;
        protected string $email;
        protected string $password;
    )
    {}
    
    public function handle ()
    {
        // User storing logic here.
        // This method can return anything
        // or can return nothing.
        
        [$firstName, $lastName] = $this->getFullNameParts();
        
        $user = new User();
        
        $user->create([
            'first_name' => $firstName,
            'last_name'  => $lastName,
            'email'      => $this->email,
            'password'   => bcrypt($this->password);
        ]);
    }
    
    protected function getFullNameParts (): array
    {
        // this method does some internal job
        // and is accessible within
        // this particular Action only
        
        return explode(' ', $this->fullName);
    }
}

多亏了Pixelcone\Fraction\AsAction特性,这个动作类现在可以这样调用

CreateUserAction::run('Alex Doe', 'alex@gmail.com', 'pass');

我们刚刚实例化了动作类,并使用一个单表达式调用了handle()方法!

Features

Features与Actions有很多共同之处:它们也是常规PHP类,它们通过构造函数接受不同的参数,并且它们也可以有一个自定义的公开handle方法。不过,它们有不同的用途:它们作为控制器/命令和动作之间的中间层。当控制器或命令方法随着时间的推移变得越来越大时,这是一个相当常见的情况,这使得阅读它们变得更加困难。Features旨在解决这个问题,作为控制器/命令动作处理器:处理输入、调用动作类并形成响应。不过,Features不应该负责运行实际的业务逻辑。简单来说,特定控制器或命令方法的全部内容都放在Feature中。由于现在它在那里,它可以被分成更小的方法。也就是说,Features只能从控制器/命令调用。与Action类似,Feature的名称应该回答“它应该做什么?”这个问题。

典型的Feature任务包括(但不限于)

  1. 接收用户输入并将其传递到调用链中
  2. 调用不同的Actions
  3. 形成并返回响应

Feature类及其使用的示例

<?php

namespace App\Http\Features;

use App\Models\User;

class ShowProfilePageFeature {    
    public function handle ($request)
    {
        // Feature logic here.
        // This method may return any kind of
        // response if the outer code requires it.
        
        $user = User::getCurrent();
        $userMessages = $this->getUserMessages($user);
        
        return render('profile', compact('user', 'userMessages'));
    }
    
    protected function profileIsValid (User $user): array
    {
        // Some complex logic here.
        // This method does some internal job
        // and is accessible within
        // this particular Feature only.
        
        return $user->messages();
    }
}

借助Pixelcone\Fraction\RunsFeatures特性,这个类现在可以方便地在控制器的方法中调用

<?php

namespace App\Http\Controllers;

use App\Http\Features\ShowProfilePageFeature;
use Pixelcone\Fraction\RunsFeatures;

class ProfileController {
    use RunsFeatures;

    // ...
    
    public function index ()
    {
        return $this->run(ShowProfilePageFeature::class);
    }
    
    // ...
}

为Fraction做出贡献

为贡献给Fraction,请按照以下步骤操作

  1. 将该仓库进行Fork。
  2. 创建一个分支: git checkout -b <分支名>
  3. 进行修改并提交: git commit -m '<提交信息>'
  4. 将更改推送到原始分支: git push origin pixelcone/fraction
  5. 创建Pull Request。

或者,查看GitHub关于创建Pull Request的文档。

联系方式

如果您想联系我,可以通过info@delho.ee与我联系。

许可证

本项目使用以下许可证: MIT