cals/rsp
Composer 安装命令:
composer require cals/rsp
包简介
RSP Architecture for Laravel
README 文档
README
About RSP
RSP is a web application library used to make things easier while you building a plain Laravel application. Besides, it provides some efficient commands to let you free while you're coding.
RSP is short for Repository Service and Presenter.
How to use
Installation
You can run composer require cals/rsp to install RSP from
Packagist.
Or, you can add "cals/rsp": "~2.0" to your composer.json and then run composer update in your
terminal to install it.
Configuration
After you install the RSP, you should put Cals\RSP\RSPServiceProvider::class in your
config/app.php providers array to make it work.
If you use Laravel 5.5 and after, you don't need do it cause laravel will auto load the provider.
Then you should run php artisan vendor:publish --tag=rsp to publish rsp.php.
Usage
Repository
You can use make:repository to create a repository and its' interface, or you can create them manually if
you want to. Remember that the repository should extends Cals\RSP\Repositories\Implementations\Repository
if you create it manually.
The repository is like below:
<?php namespace App\Repositories\Implementations; use App\Models\Example; use App\Repositories\Interfaces\ExampleRepositoryInterface; use Cals\RSP\Repositories\Implementations\Repository; class ExampleRepository extends Repository implements ExampleRepositoryInterface { /** * Get the full name of model. * * @return mixed */ function model() { return Example::class; } // Put your code here... }
And the interface is like below:
<?php namespace App\Repositories\Interfaces; interface ExampleRepositoryInterface { // Put your code here... }
As you can see, RSP use Eloquent ORM to operate database, so you should create models to map tables. We
recommend you and only put your models in app/Models/ instead of app/ which will make your project more plain.
The repository we extended provides nine methods:
store(array $inputs)You can use it to store data. The type of returned value is subclass ofIlluminate\Database\Eloquent\Model.all()You can use it to get all records. One thing you should notice is that the type of returned value isIlluminate\Database\Eloquent\Collectionrather thanarray.paginate(array $credentials = null, $page, $perPage = 15)You can use it to paginate records. One thing you should notice is that the type of returned value isIlluminate\Pagination\LengthAwarePaginatorrather thanarray.get(array $credentials = null, array $columns = ['*'])You can use it to get records. One thing you should notice is that the type of returned value isIlluminate\Database\Eloquent\Collectionrather thanarray.getRecordsSortBy(array $credentials = null, array $columns = ['*'], $field = 'id', $asc = true)You can use it to get records sort by the field you give. One thing you should notice is that while$ascistruemeans the returned value is ascending sorted otherwise is descending sorted. The other is that the type of returned value isIlluminate\Database\Eloquent\Collectionrather thanarray.find(array $credentials = null)You can use it to find a record which satisfy credentials. The type of returned value is subclass ofIlluminate\Database\Eloquent\Model.update(array $credentials, array $inputs)You can use it to update data which satisfy credentials. The type of returned value isboolean.destroy(array $credentials)You can destroy data which satisfy credentials. The type of returned value isboolean.builder(array $credentials = null)You can use it to create you own method. The type of returned value isIlluminate\Database\Eloquent\Builder.
While the returned value has only one record when you use
get(array $columns = ['*'],array $crendentials = null), it is still an instance ofIlluminate\Database\Eloquent\Collection. So if you want to find only one record and wish its' type isIlluminate\Database\Eloquent\Model, you can usefind(array $credentials = null)or finish it yourself using the methodbuilder()we provided.
While we use RSP, we do not use repository directly in controller. Repository should always provide methods to let service to use it.
Service
You can use make:service to create a service and its' interface, or you can create them manually if you want to.
Remember that the service should extends
Cals\RSP\Services\Implementations\Service if you create it manually.
The service is like below:
<?php namespace App\Services\Implementations; use App\Repositories\Interfaces\ExampleRepositoryInterface; use App\Services\Interfaces\ExampleServiceInterface; use Cals\RSP\Services\Implementations\Service; class ExampleService extends Service implements ExampleServiceInterface { /** * ExampleService constructor. * * @param ExampleRepositoryInterface $repository */ public function __construct(ExampleRepositoryInterface $repository) { $this->repository = $repository; } // Put your code here... }
And the interface is like below:
<?php namespace App\Services\Interfaces; interface ExampleServiceInterface { // Put your code here... }
The service we extended provides eight methods:
store(array $inputs)all()paginate(array $credentials = null, $page, $perPage = 15)get(array $credentials = null, array $columns = ['*'])getRecordsSortBy(array $credentials = null, array $columns = ['*'], $field = 'id', $asc = true)find(array $credentials = null)update(array $credentials, array $inputs)destroy(array $credentials)
These eight methods call methods provided by repository simply. So you can override it to satisfy your needs. And the type of returned value is like above listed in Repository.
Presenter
You can use make:presenter to create a presenter and its' interface, or you can create them manually if you want to.
Remember that the presenter should extends Cals\RSP\Presenters\Implementations\Presenter if you create it
manually.
The presenter is like below:
<?php namespace App\Presenters\Implementations; use App\Presenters\Interfaces\ExamplePresenterInterface; use Cals\RSP\Presenters\Implementations\Presenter; class ExamplePresenter extends Presenter implements ExamplePresenterInterface { // Put your code here... }
And the interface is like below:
<?php namespace App\Presenters\Interfaces; interface ExamplePresenterInterface { // Put your code here... }
The presenter we extended provides two methods:
limitLength($field, $length = 40)differentiateForHumans(Carbon $carbon)
The first method is used to limit length and the second method is used to show different form the time you give to now.
Bind
After you create your class, you need put it in rsp.php:
<?php /** * Created by PhpStorm. * User: Cals * Date: 2017/3/8 * Time: 14:03 */ return [ /* |-------------------------------------------------------------------------- | Repositories |-------------------------------------------------------------------------- | | This array is the list of your repository. The key should be the full | name of interface and the value should be the full name of | implementation. */ 'repositories' => [], /* |-------------------------------------------------------------------------- | Services |-------------------------------------------------------------------------- | | This array is the list of your service. The key should be the full name | of interface and the value should be the full name of implementation. */ 'services' => [], /* |-------------------------------------------------------------------------- | Presenters |-------------------------------------------------------------------------- | | This array is the list of your presenter. The key should be the full name | of interface and the value should be the full name of implementation. */ 'presenters' => [] ];
Such as:
'repositories' => [ 'App\Repositories\Interfaces\ExampleRepositoryInterface' => 'App\Repository\Implementations\ExampleRepository' ],
Then Laravel can bind the interface on the implementation.
Commands
We provide some commands for you to create files which have some basic codes, and you can put your own code into it to satisfy your needs.
The commands list below:
make:repositoryYou can use it to create repository class and its' interface.make:serviceYou can use it to create service class and its' interface.make:presenterYou can use it to create presenter class and its' interface.rsp:generateYou can use it to generate repositories, services and presenters based on registration.
Contributing
Thank you for considering contributing to the RSP Architecture library!
Author
Cals Ranna
License
The RSP Architecture is an open-sourced library licensed under the MIT license.
cals/rsp 适用场景与选型建议
cals/rsp 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 41 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 07 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 cals/rsp 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 cals/rsp 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 41
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-07-27