dinkara/repobuilder
Composer 安装命令:
composer require dinkara/repobuilder
包简介
Create Migration,Model and Repository from console
README 文档
README
Repobuilder is addon for PHP Laravel framework that implements Repository pattern and adds an extra level for database communication in our project architecture. Instead of accessing models and writing queries in your Controller classes or Models, you should place them here. This way you will create single access point with controlled set of functions to your database. Library also has necessary Laravel commands for easy creation of complete set of classes and files (migrations, models and repositories).
Main advantages are: * Code reusability * Code transparency * Global usage * Scalable
Requirements
- Laravel ~5.5 or higher
Composer Install
composer require dinkara/repobuilder
Publish service
Add the service provider in config/app.php:
Dinkara\RepoBuilder\RepositoryBuilderServiceProvider::class,
For publishing new services you need to execute the following line
php artisan vendor:publish --provider="Dinkara\RepoBuilder\RepositoryBuilderServiceProvider"
Now you are ready to use new features. To check if everything is fine, execute following command
php artisan list
If you can see make:repo option everything is ready.
How to use command
Examples :
php artisan make:repo User
In order to use your repositories as standard Laravel services you need to add following lines to your app/Providers/AppServiceProvider.php. This will point interface to proper class so Laravel knows which class to load when that interface is initialized.
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}"); } }
This command will create new folder User under App\Repositories and make two new files inside of them. Interface IUserRepo and class EloquentUser.
php artisan make:repo User --migation
This command will create same like first command but it will also create new migration with given name.
php artisan make:repo User --model
This command will create same like first command but it will also create new model with given name in App\Models.
php artisan make:repo User --all
This command will create migration, model and necessary repository classes.
How to use repositories
Repositories created with this library can be used in Controllers, Services, Seeders or any other class in your Laravel application.
Basic example:
<?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 ... */ }
Base interface with all available functions:
<?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); }
Function "__call" is a PHP magic function. It can be used to query database by attribute name.
Example:
$this->userRepo->findByEmail($email);
This can only be used for "findBy" function and it is recommended to use it only in combination with attributes that are unique, because it returns only first record.
Adding custom function to your repositories
If you want to add custom or override existing functions in your repositories, this can be done easily by changing interface and class for specific repository.
Let's have a look at our User example:
You should first add new functions to App\Repositories\User\IUserRepo.php interface like shown below.
<?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 ... */ }
Afterwards you should define that functions in App\Repositories\User\EloquentUser.php class.
<?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 pattern in this library is meant to cache state of your model object. This allows us to call multiple functions on the same model object (like we did with create and attachRole in the code above). To achieve this you have to return finalize function and pass it your model.
Do note that not all functions are meant to save state (for example when you have complex queries that return collection of data), in that case you should clear state of your model and just return eloquent result.
All suggestions and advices are welcome! So please send us your feedback and we will try to improve this library
dinkara/repobuilder 适用场景与选型建议
dinkara/repobuilder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 603 次下载、GitHub Stars 达 2, 最近一次更新时间为 2017 年 10 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「repository」 「laravel」 「eloquent」 「repository pattern」 「laravel repository pattern」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dinkara/repobuilder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dinkara/repobuilder 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dinkara/repobuilder 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
A Laravel Eloquent model trait for translatable resource
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Help to manage meta data on Laravel Eloquent model
A package to cast json fields, each sub-keys is castable
Laravel 5 - Repositories to the database layer
统计信息
- 总下载量: 603
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 5
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-10-16