malTeX/laravel-service-generators

提供命令行生成服务层文件。

dev-master 2016-12-15 10:15 UTC

This package is not auto-updated.

Last update: 2024-09-23 13:07:56 UTC


README

一个用于生成应用服务层文件的命令行工具。灵感来自Jeffrey Way的L5生成器包。

简介

本包提供的生成器旨在减少在使用laravel服务层时编写样板代码所需的时间。虽然它也允许不使用它,但主要目的是与仓库一起使用。

设置

1. 安装

使用composer安装此包

composer require maltex/laravel-service-generators

2. 添加服务提供者

通过添加服务提供者使包可用于您的应用程序。这对于生产代码不是必需的,因此通过将以下内容添加到您的app/Providers/AppServiceProvider.php来注册它

public function register()
{
    if ($this->app->environment() == 'local') {
        $this->app->register('Maltex\Generators\GeneratorsServiceProvider');
    }
}

示例

假设您正在处理一个Client模型。您已经创建了前端屏幕、路由和控制器端点。现在我们需要处理业务逻辑。

php artisan make:service AddClientService --repo=Client --func=addClient

您将看到应用程序中现在有了一个名为AddClientService.php的Services文件夹。

use Maltex\Generators\Contracts\ServiceContract;
use App\Repositories\CLientRepository;

class AddClientService implements ServiceContract
{
  /**
   * @var App\Repositories\ClientRepository
   */
   protected $repository;

   /**
    * Inject the repository
    *
    * @return void
    */
    public function __construct(App\Repositories\ClientRepository $repository)
    {
       $this->repository = repository;
    }

    /**
     * @inheritdoc
     */
    public function execute($data)
    {
        // TODO Add business logic

        $this->repository->addClient($data)
    }
}

然后应该将此服务注入到您的控制器函数中。

// ClientController.php
public function create(
    Request $request,
    AddClientService $service
)
{
    if($service->execute($request)) {
        // success response
    }
}

待办事项

  • 添加仓库目录和服务的配置细节
  • 添加用于仓库服务的生成器(与Dingo或Lara5 Repo绑定)
  • 提供为服务生成测试文件的选择