Skip to content

Contracts

Introduction

LaraGram's "contracts" are a set of interfaces that define the core services provided by the framework. For example, an LaraGram\Contracts\Queue\Queue contract defines the methods needed for queueing jobs.

Each contract has a corresponding implementation provided by the framework..

All of the LaraGram 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 LaraGram services.

Contracts vs. Facades

LaraGram's facades and helper functions provide a simple way of utilizing LaraGram's services without needing to type-hint 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 require 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 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 LaraGram 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 multiple PHP frameworks you may wish to use the laraxgram/contracts package to define your integration with LaraGram's services without the need to require LaraGram's concrete implementations in your package's composer.json file.

How to Use Contracts

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

Many types of classes in LaraGram are resolved through the service container, including controllers, event listeners, middleware, queued jobs, and even listen closures. 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
<?php

namespace App\Listeners;

use App\Events\OrderWasPlaced;
use App\Models\User;
use LaraGram\Contracts\Redis\Factory;

class CacheOrderInformation
{
    /**
     * Create the event listener.
     */
    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 of the LaraGram contracts and their equivalent facades:

ContractReferences Facade
LaraGram\Contracts\Auth\Access\Authorizable 
LaraGram\Contracts\Auth\Access\GateGate
LaraGram\Contracts\Auth\Authenticatable 
LaraGram\Contracts\Auth\FactoryAuth
LaraGram\Contracts\Auth\UserProvider 
LaraGram\Contracts\Bus\DispatcherBus
LaraGram\Contracts\Bus\QueueingDispatcherBus::dispatchToQueue()
LaraGram\Contracts\Cache\FactoryCache
LaraGram\Contracts\Cache\Lock 
LaraGram\Contracts\Cache\LockProvider 
LaraGram\Contracts\Cache\RepositoryCache::driver()
LaraGram\Contracts\Cache\Store 
LaraGram\Contracts\Config\RepositoryConfig
LaraGram\Contracts\Console\Application 
LaraGram\Contracts\Console\KernelCommander
LaraGram\Contracts\Container\ContainerApp
LaraGram\Contracts\Database\ModelIdentifier 
LaraGram\Contracts\Debug\ExceptionHandler 
LaraGram\Contracts\Encryption\EncrypterCrypt
LaraGram\Contracts\Events\DispatcherEvent
LaraGram\Contracts\Filesystem\FactoryStorage
LaraGram\Contracts\Filesystem\FilesystemStorage::disk()
LaraGram\Contracts\Foundation\ApplicationApp
LaraGram\Contracts\Hashing\HasherHash
LaraGram\Contracts\Bot\Kernel 
LaraGram\Contracts\Pipeline\Hub 
LaraGram\Contracts\Pipeline\PipelinePipeline
LaraGram\Contracts\Queue\EntityResolver 
LaraGram\Contracts\Queue\FactoryQueue
LaraGram\Contracts\Queue\Job 
LaraGram\Contracts\Queue\MonitorQueue
LaraGram\Contracts\Queue\QueueQueue::connection()
LaraGram\Contracts\Queue\QueueableCollection 
LaraGram\Contracts\Queue\QueueableEntity 
LaraGram\Contracts\Queue\ShouldQueue 
LaraGram\Contracts\Redis\FactoryRedis
LaraGram\Contracts\Listening\BindingRegistrarListen
LaraGram\Contracts\Listening\RegistrarListen
LaraGram\Contracts\Listening\ResponseFactoryResponse
LaraGram\Contracts\Listening\PathGeneratorURL
LaraGram\Contracts\Listening\PathListenable 
LaraGram\Contracts\Support\Arrayable 
LaraGram\Contracts\Support\Htmlable 
LaraGram\Contracts\Support\Jsonable 
LaraGram\Contracts\Support\MessageBag 
LaraGram\Contracts\Support\MessageProvider 
LaraGram\Contracts\Support\Renderable 
LaraGram\Contracts\Support\Responsable 
LaraGram\Contracts\Translation\Loader 
LaraGram\Contracts\Translation\TranslatorLang
LaraGram\Contracts\Validation\FactoryValidator
LaraGram\Contracts\Validation\ValidatesWhenResolved 
LaraGram\Contracts\Validation\ValidationRule 
LaraGram\Contracts\Validation\ValidatorValidator::make()
LaraGram\Contracts\Template\Engine 
LaraGram\Contracts\Template\FactoryTemplate
LaraGram\Contracts\Template\TemplateTemplate::make()

Released under the MIT License.