dingkara / repobuilder
从控制台创建迁移、模型和仓库
Requires
- php: ~5.6|~7.0
- illuminate/support: ~5.1
- sofa/eloquence: ^5.5
Requires (Dev)
- phpunit/phpunit: >=5.4.3
- squizlabs/php_codesniffer: ^2.3
README
Repobuilder 是 PHP Laravel 框架的插件,实现了 Repository 模式,并为我们的项目架构中的数据库通信增加了额外的一层。您不应在控制器类或模型中访问模型和编写查询,而应将它们放在这里。这样,您将为数据库创建一个单点访问,并具有受控的功能集合。库还包括必要的 Laravel 命令,以便轻松创建完整的类和文件集(迁移、模型和仓库)。
主要优势包括:* 代码可重用性 * 代码透明性 * 全局使用 * 可扩展性
要求
- Laravel ~5.5 或更高版本
Composer 安装
composer require dinkara/repobuilder
发布服务
在 config/app.php
中添加服务提供者
Dinkara\RepoBuilder\RepositoryBuilderServiceProvider::class,
要发布新服务,您需要执行以下行
php artisan vendor:publish --provider="Dinkara\RepoBuilder\RepositoryBuilderServiceProvider"
现在您已准备好使用新功能。要检查一切是否正常,请执行以下命令
php artisan list
如果您可以看到 make:repo 选项,则一切准备就绪。
如何使用命令
示例
php artisan make:repo User
为了将您的仓库用作标准的 Laravel 服务,您需要在 app/Providers/AppServiceProvider.php 中添加以下行。这将指向正确的类,这样 Laravel 就知道初始化该接口时要加载哪个类。
public function register() { /* $repos represents array of all you created with this library */ $repos = array( 'User', ); foreach ($repos as $idx => $repo) { $this->app->bind("App\Repositories\\{$repo}\I{$repo}Repo", "App\Repositories\\{$repo}\\Eloquent{$repo}"); } }
此命令将在 App\Repositories 下创建新的 User 文件夹,并在其中创建两个新文件。接口 IUserRepo 和类 EloquentUser。
php artisan make:repo User --migation
此命令将创建与第一个命令相同的命令,但它还将创建具有给定名称的新迁移。
php artisan make:repo User --model
此命令将创建与第一个命令相同的命令,但它将在 App\Models 中创建具有给定名称的新模型。
php artisan make:repo User --all
此命令将创建迁移、模型和必要的仓库类。
如何使用仓库
使用此库创建的仓库可以在控制器、服务、种子或 Laravel 应用程序中的任何其他类中使用。
基本示例
<?php namespace App\Http\Controllers; use App\Repositories\User\IUserRepo; class UserController extends Controller { protected $userRepo; public function __construct(IUserRepo $userRepo) { $this->userRepo = $userRepo; } public function show($id) { if($item = $this->repo->find($id)){ return response()->json($item->getModel()); } return response('Not Found', 404); } public function update(Request $request, $id) { $data = $request->all(); if($this->userRepo->find($id)->update($data)){ return response("Ok", 200); } return response("Failed", 500); } /* ... Your code ... */ }
包含所有可用函数的基本接口
<?php namespace Dinkara\RepoBuilder\Repositories; interface IRepo { function model(); function getModel(); function firstOrNew($where); function firstOrCreate($where); function fill($fields); function find($id); function all(); function paginateAll($perPage = 20); function create($fields); function findAndUpdate($id, $fields); function update($fields); function save(); function delete(); function __call($name, $arguments); }
函数 "__call" 是 PHP 的一个魔术函数。它可以用来通过属性名称查询数据库。
示例
$this->userRepo->findByEmail($email);
这只能用于 "findBy" 函数,并且建议仅与唯一属性结合使用,因为它只返回第一条记录。
向仓库添加自定义函数
如果您想向仓库添加自定义函数或覆盖现有函数,可以通过更改特定仓库的接口和类轻松完成。
让我们看看我们的用户示例
您首先应在 App\Repositories\User\IUserRepo.php 接口中添加新函数,如下所示。
<?php namespace App\Repositories\User; use App\Repositories\IRepo; /** * Interface UserRepository * @package App\Repositories\User */ interface IUserRepo extends IRepo { /** * Function that creates new user * @param type $fields */ function register($fields); /* ... Your custom functions ... */ }
之后,您应在 App\Repositories\User\EloquentUser.php 类中定义这些函数。
<?php namespace App\Repositories\User; use App\Repositories\EloquentRepo; use App\Models\User; class EloquentUser extends EloquentRepo implements IUserRepo { /** * Configure the Model * */ public function model() { return new User; } public function register($fields) { $fields["confirmation_code"] = str_random(30); $result = $this->create($fields)->attachRole("customer"); return $this->finalize($result); } private function attachRole($role) { if (!$this->model) { return false; } $result = $this->model->roles()->attach($role); return $this->finalize($this->model); } }
此库中的 Repository 模式旨在缓存模型对象的当前状态。这使我们能够对同一模型对象调用多个函数(如上面的 create 和 attachRole)。为了实现这一点,您必须返回 finalize 函数并传递您的模型。
请注意,并非所有函数都旨在保存状态(例如,当您有复杂的查询返回数据集合时),在这种情况下,您应该清除模型的状态,并仅返回 Eloquent 结果。