theozebua/laravel-repository

一个简单的 artisan 命令,用于创建您的仓库模式文件。

v1.3.3 2024-05-25 02:29 UTC

This package is auto-updated.

Last update: 2024-09-25 03:09:37 UTC


README

一个简单的 artisan 命令,轻松生成您的仓库模式文件。

安装

要安装此包,您只需使用 composer 安装即可。

composer require theozebua/laravel-repository --dev

用法

要使用此包,您只需运行

php artisan repository:generate

然后它会询问您一些问题。以下是一些示例。

生成仓库接口

运行

php artisan repository:generate

然后选择 Interface,您可以输入名称或索引。

  What do you want to generate?
  Interface .............................................. 0
  Repository ............................................. 1
❯ Interface

然后输入您的接口名称。

  What is the name of your interface? [RepositoryInterface]
❯ RepositoryInterface

就这样。

文件将放置在 app/Repositories/Interfaces/RepositoryInterface.php

注意:这些步骤是在您第一次生成接口且之前没有创建接口时使用的。如果您之后尝试生成更多接口,它将要求您回答附加问题。以下是一个示例。

生成一个继承其他接口的仓库接口

运行

php artisan repository:generate

然后选择 Interface,您可以输入名称或索引。

  What do you want to generate?
  Interface .............................................. 0
  Repository ............................................. 1
❯ Interface

然后输入您的接口名称。

  What is the name of your interface? [RepositoryInterface]
❯ RepositoryInterface

它将询问您是否要扩展其他接口。如果您选择 no,则接口将创建而不扩展其他接口。但在这个例子中,我们选择了 yes

  Do you want to extends another interfaces? (yes/no) [no]
❯ yes

然后它会询问您选择哪个接口要扩展。

然后选择要扩展的接口,您可以直接输入接口名称或索引。我将输入索引,因为接口名称太长了,难以输入。

  Please choose interface(s) that you want to extends separated by comma:
  App\Repositories\Interfaces\AnotherInterface ..................... 0  
  App\Repositories\Interfaces\OtherInterface ....................... 1  
❯ 0,1

然后完成。

生成仓库类

运行

php artisan repository:generate

然后选择 Repository,您可以输入名称或索引。

  What do you want to generate?
  Interface .............................................. 0
  Repository ............................................. 1
❯ Repository

然后输入您的仓库名称。

  What is the name of your repository? [Repository]
❯ Repository

就这样。

文件将放置在 app/Repositories/Implementations/Repository.php

注意:与接口生成器类似,这些步骤是在您第一次生成仓库且之前没有创建接口时使用的。如果您之后尝试生成更多仓库,它将要求您回答附加问题。以下是一个示例。

生成一个实现一些接口的仓库类

运行

php artisan repository:generate

然后选择 Repository,您可以输入名称或索引。

  What do you want to generate?
  Interface .............................................. 0
  Repository ............................................. 1
❯ Repository

然后输入您的仓库名称。

  What is the name of your repository? [Repository]
❯ Repository

它将询问您是否要实现一些接口。如果您选择 no,则仓库将创建而不实现任何接口。但在这个例子中,我们选择了 yes

  Do you want to implements some interfaces? (yes/no) [no]
❯ yes

然后它会询问您选择哪个接口要实现。

  Please choose interface(s) that you want to implements separated by comma:
  App\Repositories\Interfaces\AnotherInterface ..................... 0  
  App\Repositories\Interfaces\OtherInterface ....................... 1  
❯ 0,1

然后完成。

注意:如果您的接口使用了 php 标准类或常量,请确保在它前面添加反斜杠 \。例如 \PHP_EOL\Iterator 等。或者,您可以在文件顶部使用 "use 语句"。例如 use const PHP_EOLuse Iterator 等,然后您可以直接使用它而不需要反斜杠。

配置

通常情况下,您不需要进行任何配置。但万一您想配置路径,您可以使用以下简单命令发布配置文件。

php artisan vendor:publish --tag=laravel-repository-config