契约
介绍
Laravel 的契约是一组接口,定义了框架提供的核心服务。例如, Illuminate\Contracts\Queue\Queue
契约定义了队列作业所需的方法,而 Illuminate\Contracts\Mail\Mailer
契约定义了发送电子邮件所需的方法。
每个契约都有框架提供的相应实现。例如,Laravel 提供了具有多种驱动程序的队列实现,以及由 SwiftMailer 驱动的邮件实现。
所有 Laravel 契约都位于 它们自己的 GitHub 仓库 中。这为所有可用契约提供了一个快速参考点,同时也是一个可以被包开发者利用的单一、解耦的包。
契约与门面
Laravel 的门面和辅助函数提供了一种简单的方法来利用 Laravel 的服务,而无需在服务容器中进行类型提示和解析契约。在大多数情况下,每个门面都有一个等效的契约。
与门面不同,契约允许您为类定义显式依赖项。某些开发人员更喜欢以这种方式显式定义其依赖项,因此更喜欢使用契约,而其他开发人员则喜欢门面的便利性。
大多数应用程序无论您是偏好门面还是契约都可以正常运行。然而,如果您正在构建一个包,您应该强烈考虑使用契约,因为它们在包上下文中更容易测试。
何时使用契约
如其他地方所述,使用契约或门面的决定很大程度上取决于个人品味和开发团队的品味。契约和门面都可以用来创建健壮、经过良好测试的 Laravel 应用程序。只要您保持类的职责集中,您会发现使用契约和门面之间的实际差异很小。
然而,您可能仍然对契约有几个问题。例如,为什么要使用接口?使用接口不是更复杂吗?让我们将使用接口的原因提炼为以下标题:松耦合和简洁性。
松耦合
首先,让我们回顾一些与缓存实现紧密耦合的代码。考虑以下内容:
<?php
namespace App\Orders;
class Repository
{
/**
* 缓存实例。
*/
protected $cache;
/**
* 创建一个新的仓库实例。
*
* @param \SomePackage\Cache\Memcached $cache
* @return void
*/
public function __construct(\SomePackage\Cache\Memcached $cache)
{
$this->cache = $cache;
}
/**
* 根据 ID 检索订单。
*
* @param int $id
* @return Order
*/
public function find($id)
{
if ($this->cache->has($id)) {
//
}
}
}
在这个类中,代码与给定的缓存实现紧密耦合。它紧密耦合是因为我们依赖于来自包供应商的具体缓存类。如果该包的 API 发生变化,我们的代码也必须更改。
同样,如果我们想用另一种技术(如 Redis)替换我们的底层缓存技术(Memcached),我们又必须修改我们的仓库。我们的仓库不应该对谁提供数据或如何提供数据有太多了解。
相反,我们可以通过依赖于一个简单的、与供应商无关的接口来改进我们的代码:
<?php
namespace App\Orders;
use Illuminate\Contracts\Cache\Repository as Cache;
class Repository
{
/**
* 缓存实例。
*/
protected $cache;
/**
* 创建一个新的仓库实例。
*
* @param Cache $cache
* @return void
*/
public function __construct(Cache $cache)
{
$this->cache = $cache;
}
}
现在,代码不再与任何特定供应商甚至 Laravel 耦合。由于契约包不包含实现和依赖项,您可以轻松编写任何给定契约的替代实现,从而允许您在不修改任何使用缓存的代码的情况下替换缓存实现。
简洁性
当 Laravel 的所有服务都在简单的接口中定义时,很容易确定给定服务提供的功能。契约作为框架功能的简洁文档。
此外,当您依赖于简单的接口时,您的代码更易于理解和维护。与其在一个大型复杂的类中查找可用的方法,不如参考一个简单、干净的接口。
如何使用契约
那么,如何获得契约的实现呢?其实很简单。
Laravel 中的许多类型的类都是通过服务容器解析的,包括控制器、事件监听器、中间件、队列作业,甚至是路由闭包。因此,要获得契约的实现,您只需在被解析的类的构造函数中“类型提示”接口即可。
例如,看看这个事件监听器:
<?php
namespace App\Listeners;
use App\User;
use App\Events\OrderWasPlaced;
use Illuminate\Contracts\Redis\Database;
class CacheOrderInformation
{
/**
* Redis 数据库实现。
*/
protected $redis;
/**
* 创建一个新的事件处理程序实例。
*
* @param Database $redis
* @return void
*/
public function __construct(Database $redis)
{
$this->redis = $redis;
}
/**
* 处理事件。
*
* @param OrderWasPlaced $event
* @return void
*/
public function handle(OrderWasPlaced $event)
{
//
}
}
当事件监听器被解析时,服务容器将读取类构造函数上的类型提示,并注入适当的值。要了解有关在服务容器中注册内容的更多信息,请查看其文档。
契约参考
此表提供了所有 Laravel 契约及其等效门面的快速参考:
契约 | 参考门面 |
---|---|
[Illuminate\Contracts\Auth\Access\Authorizable](https://github.com/illuminate/contracts/blob/5.6/Auth/Access/Authorizable.php) | |
[Illuminate\Contracts\Auth\Access\Gate](https://github.com/illuminate/contracts/blob/5.6/Auth/Access/Gate.php) | `Gate` |
[Illuminate\Contracts\Auth\Authenticatable](https://github.com/illuminate/contracts/blob/5.6/Auth/Authenticatable.php) | |
[Illuminate\Contracts\Auth\CanResetPassword](https://github.com/illuminate/contracts/blob/5.6/Auth/CanResetPassword.php) | |
[Illuminate\Contracts\Auth\Factory](https://github.com/illuminate/contracts/blob/5.6/Auth/Factory.php) | `Auth` |
[Illuminate\Contracts\Auth\Guard](https://github.com/illuminate/contracts/blob/5.6/Auth/Guard.php) | `Auth::guard()` |
[Illuminate\Contracts\Auth\PasswordBroker](https://github.com/illuminate/contracts/blob/5.6/Auth/PasswordBroker.php) | `Password::broker()` |
[Illuminate\Contracts\Auth\PasswordBrokerFactory](https://github.com/illuminate/contracts/blob/5.6/Auth/PasswordBrokerFactory.php) | `Password` |
[Illuminate\Contracts\Auth\StatefulGuard](https://github.com/illuminate/contracts/blob/5.6/Auth/StatefulGuard.php) | |
[Illuminate\Contracts\Auth\SupportsBasicAuth](https://github.com/illuminate/contracts/blob/5.6/Auth/SupportsBasicAuth.php) | |
[Illuminate\Contracts\Auth\UserProvider](https://github.com/illuminate/contracts/blob/5.6/Auth/UserProvider.php) | |
[Illuminate\Contracts\Bus\Dispatcher](https://github.com/illuminate/contracts/blob/5.6/Bus/Dispatcher.php) | `Bus` |
[Illuminate\Contracts\Bus\QueueingDispatcher](https://github.com/illuminate/contracts/blob/5.6/Bus/QueueingDispatcher.php) | `Bus::dispatchToQueue()` |
[Illuminate\Contracts\Broadcasting\Factory](https://github.com/illuminate/contracts/blob/5.6/Broadcasting/Factory.php) | `Broadcast` |
[Illuminate\Contracts\Broadcasting\Broadcaster](https://github.com/illuminate/contracts/blob/5.6/Broadcasting/Broadcaster.php) | `Broadcast::connection()` |
[Illuminate\Contracts\Broadcasting\ShouldBroadcast](https://github.com/illuminate/contracts/blob/5.6/Broadcasting/ShouldBroadcast.php) | |
[Illuminate\Contracts\Broadcasting\ShouldBroadcastNow](https://github.com/illuminate/contracts/blob/5.6/Broadcasting/ShouldBroadcastNow.php) | |
[Illuminate\Contracts\Cache\Factory](https://github.com/illuminate/contracts/blob/5.6/Cache/Factory.php) | `Cache` |
[Illuminate\Contracts\Cache\Lock](https://github.com/illuminate/contracts/blob/5.6/Cache/Lock.php) | |
[Illuminate\Contracts\Cache\LockProvider](https://github.com/illuminate/contracts/blob/5.6/Cache/LockProvider.php) | |
[Illuminate\Contracts\Cache\Repository](https://github.com/illuminate/contracts/blob/5.6/Cache/Repository.php) | `Cache::driver()` |
[Illuminate\Contracts\Cache\Store](https://github.com/illuminate/contracts/blob/5.6/Cache/Store.php) | |
[Illuminate\Contracts\Config\Repository](https://github.com/illuminate/contracts/blob/5.6/Config/Repository.php) | `Config` |
[Illuminate\Contracts\Console\Application](https://github.com/illuminate/contracts/blob/5.6/Console/Application.php) | |
[Illuminate\Contracts\Console\Kernel](https://github.com/illuminate/contracts/blob/5.6/Console/Kernel.php) | `Artisan` |
[Illuminate\Contracts\Container\Container](https://github.com/illuminate/contracts/blob/5.6/Container/Container.php) | `App` |
[Illuminate\Contracts\Cookie\Factory](https://github.com/illuminate/contracts/blob/5.6/Cookie/Factory.php) | `Cookie` |
[Illuminate\Contracts\Cookie\QueueingFactory](https://github.com/illuminate/contracts/blob/5.6/Cookie/QueueingFactory.php) | `Cookie::queue()` |
[Illuminate\Contracts\Database\ModelIdentifier](https://github.com/illuminate/contracts/blob/5.6/Database/ModelIdentifier.php) | |
[Illuminate\Contracts\Debug\ExceptionHandler](https://github.com/illuminate/contracts/blob/5.6/Debug/ExceptionHandler.php) | |
[Illuminate\Contracts\Encryption\Encrypter](https://github.com/illuminate/contracts/blob/5.6/Encryption/Encrypter.php) | `Crypt` |
[Illuminate\Contracts\Events\Dispatcher](https://github.com/illuminate/contracts/blob/5.6/Events/Dispatcher.php) | `Event` |
[Illuminate\Contracts\Filesystem\Cloud](https://github.com/illuminate/contracts/blob/5.6/Filesystem/Cloud.php) | `Storage::cloud()` |
[Illuminate\Contracts\Filesystem\Factory](https://github.com/illuminate/contracts/blob/5.6/Filesystem/Factory.php) | `Storage` |
[Illuminate\Contracts\Filesystem\Filesystem](https://github.com/illuminate/contracts/blob/5.6/Filesystem/Filesystem.php) | `Storage::disk()` |
[Illuminate\Contracts\Foundation\Application](https://github.com/illuminate/contracts/blob/5.6/Foundation/Application.php) | `App` |
[Illuminate\Contracts\Hashing\Hasher](https://github.com/illuminate/contracts/blob/5.6/Hashing/Hasher.php) | `Hash` |
[Illuminate\Contracts\Http\Kernel](https://github.com/illuminate/contracts/blob/5.6/Http/Kernel.php) | |
[Illuminate\Contracts\Mail\MailQueue](https://github.com/illuminate/contracts/blob/5.6/Mail/MailQueue.php) | `Mail::queue()` |
[Illuminate\Contracts\Mail\Mailable](https://github.com/illuminate/contracts/blob/5.6/Mail/Mailable.php) | |
[Illuminate\Contracts\Mail\Mailer](https://github.com/illuminate/contracts/blob/5.6/Mail/Mailer.php) | `Mail` |
[Illuminate\Contracts\Notifications\Dispatcher](https://github.com/illuminate/contracts/blob/5.6/Notifications/Dispatcher.php) | `Notification` |
[Illuminate\Contracts\Notifications\Factory](https://github.com/illuminate/contracts/blob/5.6/Notifications/Factory.php) | `Notification` |
[Illuminate\Contracts\Pagination\LengthAwarePaginator](https://github.com/illuminate/contracts/blob/5.6/Pagination/LengthAwarePaginator.php) | |
[Illuminate\Contracts\Pagination\Paginator](https://github.com/illuminate/contracts/blob/5.6/Pagination/Paginator.php) | |
[Illuminate\Contracts\Pipeline\Hub](https://github.com/illuminate/contracts/blob/5.6/Pipeline/Hub.php) | |
[Illuminate\Contracts\Pipeline\Pipeline](https://github.com/illuminate/contracts/blob/5.6/Pipeline/Pipeline.php) | |
[Illuminate\Contracts\Queue\EntityResolver](https://github.com/illuminate/contracts/blob/5.6/Queue/EntityResolver.php) | |
[Illuminate\Contracts\Queue\Factory](https://github.com/illuminate/contracts/blob/5.6/Queue/Factory.php) | `Queue` |
[Illuminate\Contracts\Queue\Job](https://github.com/illuminate/contracts/blob/5.6/Queue/Job.php) | |
[Illuminate\Contracts\Queue\Monitor](https://github.com/illuminate/contracts/blob/5.6/Queue/Monitor.php) | `Queue` |
[Illuminate\Contracts\Queue\Queue](https://github.com/illuminate/contracts/blob/5.6/Queue/Queue.php) | `Queue::connection()` |
[Illuminate\Contracts\Queue\QueueableCollection](https://github.com/illuminate/contracts/blob/5.6/Queue/QueueableCollection.php) | |
[Illuminate\Contracts\Queue\QueueableEntity](https://github.com/illuminate/contracts/blob/5.6/Queue/QueueableEntity.php) | |
[Illuminate\Contracts\Queue\ShouldQueue](https://github.com/illuminate/contracts/blob/5.6/Queue/ShouldQueue.php) | |
[Illuminate\Contracts\Redis\Factory](https://github.com/illuminate/contracts/blob/5.6/Redis/Factory.php) | `Redis` |
[Illuminate\Contracts\Routing\BindingRegistrar](https://github.com/illuminate/contracts/blob/5.6/Routing/BindingRegistrar.php) | `Route` |
[Illuminate\Contracts\Routing\Registrar](https://github.com/illuminate/contracts/blob/5.6/Routing/Registrar.php) | `Route` |
[Illuminate\Contracts\Routing\ResponseFactory](https://github.com/illuminate/contracts/blob/5.6/Routing/ResponseFactory.php) | `Response` |
[Illuminate\Contracts\Routing\UrlGenerator](https://github.com/illuminate/contracts/blob/5.6/Routing/UrlGenerator.php) | `URL` |
[Illuminate\Contracts\Routing\UrlRoutable](https://github.com/illuminate/contracts/blob/5.6/Routing/UrlRoutable.php) | |
[Illuminate\Contracts\Session\Session](https://github.com/illuminate/contracts/blob/5.6/Session/Session.php) | `Session::driver()` |
[Illuminate\Contracts\Support\Arrayable](https://github.com/illuminate/contracts/blob/5.6/Support/Arrayable.php) | |
[Illuminate\Contracts\Support\Htmlable](https://github.com/illuminate/contracts/blob/5.6/Support/Htmlable.php) | |
[Illuminate\Contracts\Support\Jsonable](https://github.com/illuminate/contracts/blob/5.6/Support/Jsonable.php) | |
[Illuminate\Contracts\Support\MessageBag](https://github.com/illuminate/contracts/blob/5.6/Support/MessageBag.php) | |
[Illuminate\Contracts\Support\MessageProvider](https://github.com/illuminate/contracts/blob/5.6/Support/MessageProvider.php) | |
[Illuminate\Contracts\Support\Renderable](https://github.com/illuminate/contracts/blob/5.6/Support/Renderable.php) | |
[Illuminate\Contracts\Support\Responsable](https://github.com/illuminate/contracts/blob/5.6/Support/Responsable.php) | |
[Illuminate\Contracts\Translation\Loader](https://github.com/illuminate/contracts/blob/5.6/Translation/Loader.php) | |
[Illuminate\Contracts\Translation\Translator](https://github.com/illuminate/contracts/blob/5.6/Translation/Translator.php) | `Lang` |
[Illuminate\Contracts\Validation\Factory](https://github.com/illuminate/contracts/blob/5.6/Validation/Factory.php) | `Validator` |
[Illuminate\Contracts\Validation\ImplicitRule](https://github.com/illuminate/contracts/blob/5.6/Validation/ImplicitRule.php) | |
[Illuminate\Contracts\Validation\Rule](https://github.com/illuminate/contracts/blob/5.6/Validation/Rule.php) | |
[Illuminate\Contracts\Validation\ValidatesWhenResolved](https://github.com/illuminate/contracts/blob/5.6/Validation/ValidatesWhenResolved.php) | |
[Illuminate\Contracts\Validation\Validator](https://github.com/illuminate/contracts/blob/5.6/Validation/Validator.php) | `Validator::make()` |
[Illuminate\Contracts\View\Engine](https://github.com/illuminate/contracts/blob/5.6/View/Engine.php) | |
[Illuminate\Contracts\View\Factory](https://github.com/illuminate/contracts/blob/5.6/View/Factory.php) | `View` |
[Illuminate\Contracts\View\View](https://github.com/illuminate/contracts/blob/5.6/View/View.php) | `View::make()` |