契约
Laravel 中的契约是指框架提供的一系列定义核心服务的接口,存放在Illuminate\Contracts文件夹下
。
Illuminate\Contracts\Queue\Queue
契约定义了队列任务需要实现的方法,每一个契约都有框架提供的相应实现。
这是一个非常重要的概念,面向接口编程。在具体实现之前定下契约,在使用时只需要确保传入的是实现此契约的某个类的实例就好了,比如队列契约,要求必须能push,用什么实现的根本不介意。我们去使用的时候也只需要考虑我要push,至于用什么实现的push对我们而言可以是“黑盒”。
laravel中契约是搭配服务容器使用的,网上很多细讲服务容器实现的文章,鉴于我的水平仅限于能理解而写不出(反射相关的函数实在是记不住...),就不写了。
<?php
namespace Illuminate\Contracts\Queue;
interface Queue
{
/**
* Get the size of the queue.
*
* @param string|null $queue
* @return int
*/
public function size($queue = null);
/**
* Push a new job onto the queue.
*
* @param string|object $job
* @param mixed $data
* @param string|null $queue
* @return mixed
*/
public function push($job, $data = '', $queue = null);
/**
* Push a new job onto the queue.
*
* @param string $queue
* @param string|object $job
* @param mixed $data
* @return mixed
*/
public function pushOn($queue, $job, $data = '');
/**
* Push a raw payload onto the queue.
*
* @param string $payload
* @param string|null $queue
* @param array $options
* @return mixed
*/
public function pushRaw($payload, $queue = null, array $options = []);
/**
* Push a new job onto the queue after a delay.
*
* @param \DateTimeInterface|\DateInterval|int $delay
* @param string|object $job
* @param mixed $data
* @param string|null $queue
* @return mixed
*/
public function later($delay, $job, $data = '', $queue = null);
/**
* Push a new job onto the queue after a delay.
*
* @param string $queue
* @param \DateTimeInterface|\DateInterval|int $delay
* @param string|object $job
* @param mixed $data
* @return mixed
*/
public function laterOn($queue, $delay, $job, $data = '');
/**
* Push an array of jobs onto the queue.
*
* @param array $jobs
* @param mixed $data
* @param string|null $queue
* @return mixed
*/
public function bulk($jobs, $data = '', $queue = null);
/**
* Pop the next job off of the queue.
*
* @param string|null $queue
* @return \Illuminate\Contracts\Queue\Job|null
*/
public function pop($queue = null);
/**
* Get the connection name for the queue.
*
* @return string
*/
public function getConnectionName();
/**
* Set the connection name for the queue.
*
* @param string $name
* @return $this
*/
public function setConnectionName($name);
}