Contracts

Introduction

Framework’s “contracts” are a set of interfaces that define the core services provided by the framework. For example, an MacropaySolutions\Kernel\Contracts\Queue\Queue contract defines the methods needed for queueing jobs, while the MacropaySolutions\Kernel\Contracts\Mail\Mailer contract defines the methods needed for sending e-mail.

Each contract has a corresponding implementation provided by the framework. For example, Framework provides a queue implementation with a variety of drivers, and a mailer implementation that is powered by Symfony Mailer.

All the Framework contracts live in their own GitHub repository. This provides a quick reference point for all available contracts, as well as a single, decoupled package that may be utilized when building packages that interact with Framework services.

Contracts vs. Container Helpers

Framework’s helper functions provide a simple way of utilizing Framework’s services without needing to type-hint and resolve contracts out of the service container.

Unlike helpers, which do not require you to inject them in your class’ constructor, 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 global helpers.

When to Use Contracts

The decision to use contracts or global helpers will come down to personal taste and the tastes of your development team. Both contracts and global functions can be used to create robust, well-tested Framework applications.

If you are building a package that integrates with multiple PHP frameworks you may wish to use the kernel/contracts package to define your integration with Framework’s services.

How to Use Contracts

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

Many types of classes in Framework are resolved through the service container, including controllers, event listeners, middleware, queued jobs. So, to get an implementation of a contract, you can just “type-hint” the interface in the constructor of the class being resolved.

For example, take a look at this event listener:

<?php

namespace App\Listeners;

use App\Events\OrderWasPlaced;
use App\Models\User;
use MacropaySolutions\Kernel\Contracts\Redis\Factory;

class CacheOrderInformation
{
    /**
     * Create a new event handler instance.
     */
    public function __construct(
        protected Factory $redis,
    ) {}

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

When the event listener is resolved, the service container will read the type-hints on the constructor 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 all the Framework contracts and their equivalent container bindings:

Contract Container Binding
MacropaySolutions\Kernel\Contracts\Auth\Access\Authorizable   
MacropaySolutions\Kernel\Contracts\Auth\Access\Gate MacropaySolutions\Kernel\Contracts\Auth\Access\Gate
MacropaySolutions\Kernel\Contracts\Auth\Authenticatable   
MacropaySolutions\Kernel\Contracts\Auth\CanResetPassword  
MacropaySolutions\Kernel\Contracts\Auth\Factory auth
MacropaySolutions\Kernel\Contracts\Auth\Guard auth.driver
MacropaySolutions\Kernel\Contracts\Auth\PasswordBroker auth.password.broker
MacropaySolutions\Kernel\Contracts\Auth\PasswordBrokerFactory auth.password
MacropaySolutions\Kernel\Contracts\Auth\StatefulGuard  
MacropaySolutions\Kernel\Contracts\Auth\SupportsBasicAuth  
MacropaySolutions\Kernel\Contracts\Auth\UserProvider  
MacropaySolutions\Kernel\Contracts\Bus\Dispatcher MacropaySolutions\Kernel\Bus\Dispatcher
MacropaySolutions\Kernel\Contracts\Bus\QueueingDispatcher MacropaySolutions\Kernel\Bus\Dispatcher
MacropaySolutions\Kernel\Contracts\Broadcasting\Factory MacropaySolutions\Kernel\Broadcasting\BroadcastManager
MacropaySolutions\Kernel\Contracts\Broadcasting\Broadcaster MacropaySolutions\Kernel\Contracts\Broadcasting\Broadcaster
MacropaySolutions\Kernel\Contracts\Broadcasting\ShouldBroadcast  
MacropaySolutions\Kernel\Contracts\Broadcasting\ShouldBroadcastNow  
MacropaySolutions\Kernel\Contracts\Cache\Factory cache
MacropaySolutions\Kernel\Contracts\Cache\Lock  
MacropaySolutions\Kernel\Contracts\Cache\LockProvider  
MacropaySolutions\Kernel\Contracts\Cache\Repository app('cache')->driver()
MacropaySolutions\Kernel\Contracts\Cache\Store  
MacropaySolutions\Kernel\Contracts\Config\Repository config
MacropaySolutions\Kernel\Contracts\Console\Application  
MacropaySolutions\Kernel\Contracts\Console\Kernel MacropaySolutions\Kernel\Contracts\Console\Kernel
MacropaySolutions\Kernel\Contracts\Container\Container app
MacropaySolutions\Kernel\Contracts\Cookie\Factory cookie
MacropaySolutions\Kernel\Contracts\Cookie\QueueingFactory cookie
MacropaySolutions\Kernel\Contracts\Database\ModelIdentifier  
MacropaySolutions\Kernel\Contracts\Debug\ExceptionHandler  
MacropaySolutions\Kernel\Contracts\Encryption\Encrypter encrypter
MacropaySolutions\Kernel\Contracts\Events\Dispatcher events
MacropaySolutions\Kernel\Contracts\Filesystem\Cloud app('filesystem')->cloud()
MacropaySolutions\Kernel\Contracts\Filesystem\Factory filesystem
MacropaySolutions\Kernel\Contracts\Filesystem\Filesystem app('filesystem')->disk()
MacropaySolutions\Kernel\Contracts\Foundation\Application app
MacropaySolutions\Kernel\Contracts\Hashing\Hasher hash
MacropaySolutions\Kernel\Contracts\Http\Kernel  
MacropaySolutions\Kernel\Contracts\Mail\MailQueue mailer
MacropaySolutions\Kernel\Contracts\Mail\Mailable  
MacropaySolutions\Kernel\Contracts\Mail\Mailer mailer
MacropaySolutions\Kernel\Contracts\Notifications\Dispatcher MacropaySolutions\Kernel\Notifications\ChannelManager
MacropaySolutions\Kernel\Contracts\Notifications\Factory MacropaySolutions\Kernel\Notifications\ChannelManager
MacropaySolutions\Kernel\Contracts\Pagination\LengthAwarePaginator  
MacropaySolutions\Kernel\Contracts\Pagination\Paginator  
MacropaySolutions\Kernel\Contracts\Pipeline\Hub  
MacropaySolutions\Kernel\Contracts\Pipeline\Pipeline pipeline
MacropaySolutions\Kernel\Contracts\Queue\EntityResolver  
MacropaySolutions\Kernel\Contracts\Queue\Factory queue
MacropaySolutions\Kernel\Contracts\Queue\Job  
MacropaySolutions\Kernel\Contracts\Queue\Monitor queue
MacropaySolutions\Kernel\Contracts\Queue\Queue queue.connection
MacropaySolutions\Kernel\Contracts\Queue\QueueableCollection  
MacropaySolutions\Kernel\Contracts\Queue\QueueableEntity  
MacropaySolutions\Kernel\Contracts\Queue\ShouldQueue  
MacropaySolutions\Kernel\Contracts\Redis\Factory redis
MacropaySolutions\Framework\Http\ResponseFactory MacropaySolutions\Framework\Http\ResponseFactory
MacropaySolutions\Framework\Routing\UrlGenerator url
MacropaySolutions\Kernel\Contracts\Session\Session session.store
MacropaySolutions\Kernel\Contracts\Support\Arrayable  
MacropaySolutions\Kernel\Contracts\Support\Htmlable  
MacropaySolutions\Kernel\Contracts\Support\Jsonable  
MacropaySolutions\Kernel\Contracts\Support\MessageBag  
MacropaySolutions\Kernel\Contracts\Support\MessageProvider  
MacropaySolutions\Kernel\Contracts\Support\Renderable  
MacropaySolutions\Kernel\Contracts\Support\Responsable  
MacropaySolutions\Kernel\Contracts\Translation\Loader  
MacropaySolutions\Kernel\Contracts\Translation\Translator translator
MacropaySolutions\Kernel\Contracts\Validation\Factory validator
MacropaySolutions\Kernel\Contracts\Validation\ImplicitRule  
MacropaySolutions\Kernel\Contracts\Validation\Rule  
MacropaySolutions\Kernel\Contracts\Validation\ValidatesWhenResolved  
MacropaySolutions\Kernel\Contracts\Validation\Validator app('validator')->make()
MacropaySolutions\Kernel\Contracts\View\Engine  
MacropaySolutions\Kernel\Contracts\View\Factory view
MacropaySolutions\Kernel\Contracts\View\View app('view')->make()

This site uses Just the Docs, a documentation theme for Jekyll.