inz / repository
laravel项目的仓库模式文件生成器
Requires
- php: >=7.0.2
Requires (Dev)
- orchestra/testbench: ^4.3
This package is auto-updated.
Last update: 2024-09-30 02:05:16 UTC
README
仓库模式生成器
此包帮助您快速开始使用 仓库模式 在您的下一个或当前laravel项目中,因为看了那些关于设计模式和SOLID原则的laracon视频后(顺便说一句,这些视频非常多:p),您变得非常兴奋,想要尝试并应用它们,至少我是这样:D。
内容
简介
使用此包将帮助您通过处理位于逻辑和数据库之间的层来生成和实现,从而让您有更多时间专注于应用程序的逻辑。
-
仓库合约:这是一个空的接口,可以用于向仓库实现类添加自定义方法,以便扩展功能,它也在绑定过程中使用。
-
实现类:该类中存放仓库逻辑,它实现了生成的合约。
注意:目前,此包测试的最小laravel版本是 5.5
安装
composer require inz/repository
如果您使用 laravel 5.4及以下版本,您需要在 config/app.php 中添加包的service provider
'providers' => [ ... Inz\RepositoryServiceProvider::class, ... ],
然后,您需要发布配置文件,以便包能够正确地执行其工作
php artisan vendor:publish --tag=inz-repository
这将复制包的配置文件到应用程序的config文件夹中,更多关于此的信息在 配置 部分
配置
包的配置文件包含一组可以修改以满足您需求的设置,它们有希望清晰的注释来描述其目的。
/* |-------------------------------------------------------------------------- | Base Values |-------------------------------------------------------------------------- | this array give you the ability to define base path & namespace to be used by the package, | path is used to create the base directory in which all generated files are stored, on | the other hand, namespace is used to define the base namespace for those files. | | Note that (in the comments) the anti-slash for namespace required when defining it's value. | */ 'base' => [ 'path' => app_path(), // 'app folder' which is the best choice 'namespace' => app()->getNamespace(), // 'App\' which is the best choice 'providers' => [ 'path' => app_path(), 'namespace' => app()->getNamespace(), ], ],
- 路径 用于创建文件的根目录。
- 命名空间 用于为文件设置命名空间。
- 提供者 数组,其中您设置应用程序中提供者的基本路径和命名空间,以便包可以确定在哪里以及如何定义仓库服务提供者。
/* |-------------------------------------------------------------------------- | Namespaces |-------------------------------------------------------------------------- | this array define the namespaces related to each category of the generated classes | by the package, you can re-define these values to control under what namespace | the these classes should reside. | | Note that anti-slash in the end is not set so make sure you don't add it. */ 'namespaces' => [ 'contracts' => 'Repositories\Contracts', 'implementations' => 'Repositories\Implementations', ],
- 命名空间 数组确定不同文件的命名空间,注意
App\不存在,因为它将在指定时添加。
/* |-------------------------------------------------------------------------- | Paths |-------------------------------------------------------------------------- | this array defines the path for each category of the generated classes by the package, | you can re-define these values to control where the files should be stored, by | default they are stored in the application directory. | | Note that anti-slash in the end is not set so make sure you don't add it. */ 'paths' => [ 'contracts' => 'Repositories/Contracts', 'implementations' => 'Repositories/Implementations', ],
- 路径 数组确定不同文件的路径,
Repositories代表它们的根目录以及斜线之后是这些文件的目录,注意app/不存在,因为它将在指定时添加。
使用
生成合约 & 实现
要生成完整的脚手架,请运行
// the pattern php artisan make:repository Model // for a model in app directory php artisan make:repository Post // for a model in Models folder php artisan make:repository Models/Post // for a model in Models folder in a subdirectory php artisan make:repository Models/Blog/Post
注意
-
如果模型不存在,您将被询问是否要创建它,如果是这样,它将被创建。
-
如果其他文件之一已存在,您将被要求覆盖它,如果是这样,它将,因此 请注意 这种情况以避免丢失编写的代码。
-
此命令还将绑定类。
-
请记住,在生成后注册
RepositoryServiceProvider到app.php
将合约绑定到实现
要绑定某个模型实现的合约类,请使用以下命令
php artisan bind:repository Model
- 使用模型名称,包可以确定相应的合约和实现类
目前,包不会检查类(模型、合约或实现)是否存在,这将在即将推出的版本中修复。
- 如果服务提供者不存在,它将创建一个
- 如果仓库已经绑定,则不会完成此过程。
功能
仓库抽象类
在 Inz\Base\Abstractions\Repository 中,您可以找到描述用于访问数据库的方法的 Inz\Base\Interfaces\RepositoryInterface 的实现。
方法
以下是仓库类可用的方法列表
注意:仓库类的实现方式应按照您的需求进行,因此请使用接口来定义其蓝图,并在具体类中实现额外的方法或重写现有方法。
软删除记录
要处理表中的回收站记录(如果模型中使用了软删除特性),在仓库实现类中使用特性 Inz\Base\Traits\TrashedOperations,它包含以下一组方法
贡献
欢迎提交任何Pull Request,任何贡献都受欢迎。如果您遇到问题,请使用问题区域。