lordfm / raw
基于注解模型的存储库模式模板
Requires
- doctrine/annotations: ^1.4
- icanboogie/inflector: 1.4.4
- symfony/class-loader: 2.8
Requires (Dev)
- orchestra/testbench: ^3.4
- phpunit/phpunit: ^5.7
README
Raw 是为 laravel 量身定制的快速存储库模式脚手架包。它将 Doctrine Annotations 与 Eloquent ORM 结合使用,从模型结构来处理您的项目。
你有想法吗?你应该能够从底部开始构建,而不会受到影响。
安装
composer require lordfm/raw
将提供者添加到你的 app.php
'providers' = [
/*
* Lots of providers
*
*/
LoRDFM\Raw\RawServiceProvider::class
]
发布 raw.php
配置文件
php artisan vendor:publish --provider="LoRDFM\Raw\RawServiceProvider"
注解你的模型
声明使用 LoRDFM\Raw\Annotations\Rawable
并在模型顶部添加注解。请确保在注解和类之间不要留空行。
MyModel.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use LoRDFM\Raw\Annotations\Rawable;
/**
*
* @Rawable
*/
class MyModel extends Model
{
}
现在,只需运行
php artisan raw:repository
发生了什么?我们刚刚构建了我们典型的 --resource Controller
,包括一个用于声明所有 CRUD 相关方法的 Contract
接口和一个实现这些方法的 Repository
类。
别忘了在你的 Providers\AppServiceProvider.php
中绑定你的存储库
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
// Other bindings
$this->app->bind('App\Repositories\Contracts\MyModelContract', 'App\Repositories\MyModelRepository');
}
默认情况下,它们将存储在 app\Http\Controllers
、app\Repositories
和 app\Repositories\Contracts
中。
正如你所期望的,你可以创建具有关系的复杂模型。我们支持以下关系
注解
注解非常直观,每个注解都处理模型与其他模型之间的关系以及声明包含存储库模式的文件。
@Rawable
每个你想由该包处理的模型都应该有这个注解。
@HasMany
一个模型数组,它们与当前模型具有 One to Many
关系
@HasMany(models = {"Articles", "Books", ...})
@HasOne
一个模型数组,它们与当前模型具有 One to One
关系
@HasOne(models = {"Profile", ...})
@BelongsTo
一个模型数组,它们与当前模型具有 Many to One
关系
@BelongsTo(models = {"Author", "Publisher", ...})
@RawableContract
处理创建包含你的存储库和控制器应实现的方法的 interface
类(即 'contract')。这是自定义存储库的核心,@RawableController
和 @RawableRepository
需要 namespace
作为参数。支持 path
和 namespace
以存储和声明接口
@RawableContract(path = "app\Repository\Contracts", namespace = "App\Repository\Contracts")
@RawableController
处理创建控制器。支持 path
、namespace
和 contract
以存储和声明用于你的模型的 --resource
风格控制器。
* @RawableController(path = "app\Http\Controllers\Api", namespace = "App\Http\Controllers\Api", contract = "App\Repository\Contracts")
@RawableRepository
处理创建存储库类。支持 path
、namespace
和 contract
以存储和声明类
@RawableRepository(path = "app\Repository", namespace = "App\Repository", contract = "App\Repository\Contracts")
现在怎么办?
简而言之,我们将使用一个众所周知的关联实体示例来演示插件的使用。经典的 作者 -> 文章 示例
作者模型
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use LoRDFM\Raw\Annotations\Rawable;
use LoRDFM\Raw\Annotations\HasMany;
use LoRDFM\Raw\Annotations\HasOne;
/**
*
* @Rawable
* @HasMany(models = {"Article"})
* @HasOne(models = {"Profile"})
*/
class Author extends Model
{
public function articles()
{
return $this->hasMany('App\Models\Article');
}
public function profile()
{
return $this->hasOne('App\Models\Profile');
}
}
作者与 Profile 模型具有 One to One
关系,与文章模型具有 One to Many
关系
文章模型
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use LoRDFM\Raw\Annotations\Rawable;
use LoRDFM\Raw\Annotations\BelongsTo;
/**
*
* @Rawable
* @BelongsTo(models = {"Author"})
*/
class Article extends Model
{
public function author()
{
return $this->belongsTo('App\Models\Author');
}
}
文章与作者具有 Many to One
关系
配置文件模型
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Profile extends Model
{
public function author()
{
return $this->belongsTo('App\Models\Author');
}
}
配置文件模型根本不需要 @Rawable 实现,目前不需要。
让我们开始吧!
现在,只需运行
php artisan raw:repository
它将告诉你正在处理哪个模型以及正在创建其存储库
...
Making repository for App\Models\Article
Making repository for App\Models\Author
...
如果模型的存储库已存在,你将收到警告
Making repository for App\Models\Author
This Contract already exists. If you want to overwrite it use '--force'
This Repository already exists. If you want to overwrite it use '--force'
This Controller already exists. If you want to overwrite it use '--force'
This RouteGroup already exists. If you want to overwrite it use '--force'
你可以使用 --force
覆盖它们
php artisan raw:repository --force
小心,相应的文件将被完全覆盖。你可以选择仅针对某些模型运行命令
php artisan raw:repository Author
php artisan raw:repository Author Article
php artisan raw:repository Author Article Publisher ...
自定义结构
您可以为您的模型自定义合约、控制器和仓库。只需导入注解,并使用您希望的路径和命名空间来定义它们。请记住,RawableController
和 RawableRepository
需要与其相应合约的命名空间。
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use LoRDFM\Raw\Annotations\Rawable;
use LoRDFM\Raw\Annotations\RawableController;
use LoRDFM\Raw\Annotations\RawableContract;
use LoRDFM\Raw\Annotations\RawableRepository;
use LoRDFM\Raw\Annotations\HasMany;
use LoRDFM\Raw\Annotations\HasOne;
/**
*
* @Rawable
* @HasMany(models = {"Article"})
* @HasOne(models = {"Profile"})
*
* @RawableContract(path = "app\Repository\Contracts", namespace = "App\Repository\Contracts")
* @RawableController(path = "app\Http\Controllers", namespace = "App\Http\Controllers", contract = "App\Repository\Contracts")
* @RawableRepository(path = "app\Repository", namespace = "App\Repository", contract = "App\Repository\Contracts")
*/
class Author extends Model
{
public function articles()
{
return $this->hasMany('App\Models\Article');
}
public function profile()
{
return $this->hasOne('App\Models\Profile');
}
}
多个端点
您可能希望有多个具有相似实现的端点,例如为未认证用户和认证用户创建 Guest
和 Api
实现。只需定义多个 RawableContract
、RawableController
和 RawableRepository
组。以下是一个示例...
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use LoRDFM\Raw\Annotations\Rawable;
use LoRDFM\Raw\Annotations\RawableController;
use LoRDFM\Raw\Annotations\RawableContract;
use LoRDFM\Raw\Annotations\RawableRepository;
use LoRDFM\Raw\Annotations\BelongsTo;
/**
*
* @Rawable
* @BelongsTo(models = {"Author"})
*
* @RawableContract(path = "app\Repository\Api\Contracts", namespace = "App\Repository\Api\Contracts")
* @RawableController(path = "app\Http\Controllers\Api", namespace = "App\Http\Controllers", contract = "App\Repository\Api\Contracts")
* @RawableRepository(path = "app\Repository\Api", namespace = "App\Repository\Api", contract = "App\Repository\Api\Contracts")
*
* @RawableContract(path = "app\Repository\Guest\Contracts", namespace = "App\Repository\Guest\Contracts")
* @RawableController(path = "app\Http\Controllers\Guest", namespace = "App\Http\Controllers", contract = "App\Repository\Guest\Contracts")
* @RawableRepository(path = "app\Repository\Guest", namespace = "App\Repository\Guest", contract = "App\Repository\Guest\Contracts")
*
*/
class Article extends Model
{
public function author()
{
return $this->belongsTo('App\Models\Author');
}
}
测试模板
您可以为您的仓库生成一些单元测试模板。
php artisan raw:repository --test
注意事项
- 在类声明前不要添加空格来编写注释。
- 在
@RawableController
和@RawableController
中,contract
参数是相应合约的namespace
,而不是最终的屈折类名。 - 请记住,在您的
Providers\AppServiceProvider.php
中绑定您的仓库。