jalameta / jps-support
该软件包已被废弃,不再维护。未建议替代软件包。
v0.1.16
2020-10-22 22:21 UTC
Requires
- ext-json: *
- illuminate/database: >=5.6
- illuminate/http: >=5.6
- illuminate/support: >=5.6
- nesbot/carbon: ^2.41
- ramsey/uuid: >=3.8
Suggests
- elasticsearch/elasticsearch: You know, for search!
- keiko/uuid-shortener: A simple shortener library for RFC 4122 compatible UUIDs. Change your 36 chars long UUID into it's shorter equivalent. You must install this package if you want to be able to use UUID Shortener
README
JPS Support 是一套提高应用开发生产力的工具集。本软件包包含 3 个重点工具,考虑了总线、数据库和缓存。
需求: Laravel 或 Lumen >= 5.6
入门
安装
composer require jalameta/jps-support
功能
1. 总线
JPS Support Bus 为 Laravel Job 提供类。我们的指南将业务逻辑从控制器中分离出来。然而,将业务逻辑分离到作业中可以简化您的应用开发过程,减少编写重复代码的时间消耗。我们鼓励我们的开发者思考业务逻辑的抽象。一个业务逻辑可以分解成几个用例,这些用例可以在另一个业务逻辑中再次使用。如何使用
您只需要扩展 BaseJob 并实现 run()
模板方法,然后在 run 方法内处理作业。该方法需要布尔返回类型,以指示作业是否成功运行。
基本用法
use App\Bus\BaseJob;
class MakePayment extends BaseJob
{
/**
* Run the actual command process.
*
* @return mixed
*/
public function run()
{
$payment = new Payment();
$payment->total = $this->request->input('total');
$payment->method = $this->request->input('method');
$payment->user()->associate(auth()->user());
return $payment->save();
}
}
从上面的例子中,您可以直接从 request
属性访问请求。请求属性是从原始请求和类构造函数中输入的数组组合而成的。
注册回调
use Jalameta\Support\Bus\BaseJob;
class MakePayment extends BaseJob
{
/**
* @var Payment
*/
public $payment;
/**
* Run the actual command process.
*
* @return mixed
*/
public function run()
{
// Callbacks are running when the job
// running successfully, based on the return value of run() method
$this->onSuccess(function () {
dispatch_now(new GenerateInvoice($this->payment))
});
$this->payment = new Payment();
$this->payment->total = $this->request->input('total');
$this->payment->method = $this->request->input('method');
$this->payment->user()->associate(auth()->user());
return $this->payment->save();
}
}
可用的回调包括 onIdle
、onRunning
、onSuccess
和 onFailed
取消作业
有时您需要在作业完成之前取消作业。您可以注册 onAbort
回调并调用 $this->abort()
方法
2. 组合键
如果您有一个具有组合键的表,这是一个辅助 trait。您可以直接在 Eloquent 模型中使用 HasCompositeKeys
trait。它在使用组合键更新 Eloquent 模型时减少了样板代码。
基本用法
use Jalameta\Support\Database\Eloquent\HasCompositeKeys;
class Reserve extends Model
{
use HasCompositeKeys;
}
3. 使用 UUID 作为主键
As the composite key, this is the trait helper too. This will set the UUID key automatically for you. Prequities you need is define the primary key of the table to the Eloquent model. Don't forget to use the `UuidAsPrimaryKey` trait into your Eloquent model.
基本用法
use Jalameta\Support\Database\Eloquent\UuidAsPrimaryKey;
class User extends Authenticable
{
use UuidAsPrimaryKey;
protected $primaryKey = 'uuid';
}