LaraflareDocsFrameworkPackagesReleases13.x

Docs / Digging Deeper / Contracts

Contracts

Introduction

laraflare's "contracts" are a set of interfaces that define the core services provided by the framework. For example, a Queue contract defines the methods needed for queueing jobs, while the Mail Factory contract defines the methods needed for sending e-mail.

Each contract has a corresponding implementation provided by the framework. For example, laraflare provides a queue implementation with a variety of drivers, and a mailer implementation that sends e-mail through Cloudflare Email Service.

All of the contracts live in packages/framework/src/Illuminate/Contracts, one file per contract, grouped by the service they describe. This provides a quick reference point for all available contracts.

TypeScript's interfaces are gone once the code is compiled, so each contract is two things with the same name: an interface that describes the methods, and a value made with contract() that can be tested at run time. A class states that it implements a contract the way it states a trait, with a mixin: class SendNotification extends ShouldQueue() {}. implementsContract(SendNotification, ShouldQueue) then answers whether a class or an instance implements the contract.

Contracts vs. Facades

laraflare's facades and helper functions provide a simple way of utilizing laraflare's services without needing to list and resolve contracts out of the service container. In most cases, each facade has an equivalent contract.

Unlike facades, which do not require you to list them in your class' dependencies, contracts allow you to define explicit dependencies for your classes. Some developers prefer to explicitly define their dependencies in this way and therefore prefer to use contracts, while other developers enjoy the convenience of facades. In general, most applications can use facades without issue during development.

When to Use Contracts

The decision to use contracts or facades will come down to personal taste and the tastes of your development team. Both contracts and facades can be used to create robust, well-tested laraflare applications. Contracts and facades are not mutually exclusive. Some parts of your applications may use facades while others depend on contracts. As long as you are keeping your class' responsibilities focused, you will notice very few practical differences between using contracts and facades.

In general, most applications can use facades without issue during development. If you are building a package that integrates with laraflare's services, you may wish to depend on its contracts rather than on its concrete implementations, so that your package works with whichever implementation the application uses.

How to Use Contracts

So, how do you get an implementation of a contract? It's actually quite simple.

Many types of classes in laraflare are resolved through the service container, including controllers, event listeners, middleware, queued jobs, and even route closures. So, to get an implementation of a contract, you can just list the contract in the static dependencies of the class being resolved and receive it in the constructor.

For example, take a look at this event listener:

import { Factory } from '@laraflare/framework';
import type { OrderWasPlaced } from '../Events/OrderWasPlaced';

export class CacheOrderInformation {
  static dependencies = [Factory];

  /**
   * Create the event listener.
   */
  constructor(protected redis: Factory) {}

  /**
   * Handle the event.
   */
  handle(event: OrderWasPlaced): void {
    // ...
  }
}

Note

The contracts that stand for a service the container holds under a name are exported with a name that says which service they are, such as CacheFactory, ConfigRepository, EventsDispatcher, Hasher, QueueFactory and Registrar; App.make(CacheFactory) is the same as App.make('cache'). The others are in packages/framework/src/Illuminate/Contracts.

When the event listener is resolved, the service container will read the static dependencies of the class, and inject the appropriate value. To learn more about registering things in the service container, check out its documentation.

Contract Reference

This table provides a quick reference to the contracts laraflare has and their equivalent facades. Each contract's file is under packages/framework/src/Illuminate/Contracts:

Contract References Facade
Auth/Access/Authorizable  
Auth/Factory Auth
Auth/PasswordBrokerFactory Password
Auth/SupportsBasicAuth  
Broadcasting/Broadcaster Broadcast.connection()
Broadcasting/Factory Broadcast
Broadcasting/ShouldBroadcast  
Broadcasting/ShouldBroadcastNow  
Bus/Dispatcher Bus
Bus/QueueingDispatcher Bus.dispatchToQueue()
Cache/Factory Cache
Cache/LockProvider  
Console/Application  
Console/Kernel Artisan
Container/Container App
Cookie/Factory Cookie
Cookie/QueueingFactory Cookie.queue()
Database/ModelIdentifier  
Debug/ExceptionHandler  
Encryption/StringEncrypter Crypt
Events/Dispatcher Event
Filesystem/Cloud Storage.cloud()
Filesystem/Factory Storage
Filesystem/Filesystem Storage.disk()
Foundation/Application App
Hashing/Hasher Hash
Http/Kernel  
Mail/Factory Mail
Mail/MailQueue Mail.queue()
Notifications/Dispatcher Notification
Notifications/Factory Notification
Pagination/LengthAwarePaginator  
Pagination/Paginator  
Queue/EntityResolver  
Queue/Factory Queue
Queue/Monitor Queue
Queue/Queue Queue.connection()
Queue/QueueableCollection  
Queue/QueueableEntity  
Queue/ShouldQueue  
Redis/Factory Redis
Routing/BindingRegistrar Route
Routing/Registrar Route
Routing/UrlRoutable  
Session/Session Session.driver()
Support/Arrayable  
Support/Htmlable  
Support/MessageProvider  
Support/Responsable  
Validation/ValidatesWhenResolved  
View/Engine  
View/Factory View
View/View View.make()
Adapted from Laravel's documentation, Copyright (c) Taylor Otwell, used under the MIT License.