Componentization is a great way to build extensible and reliable software systems. It allows us to build large systems that are composed of decoupled, independent and reusable components. It gives us a plug-and-play approach to building software systems.

Laravel as a framework is richly composed of reusable components — some of which are third-party Symfony components — that are all well-defined and pieced together to make up the system.

Components

Most modern software systems are built by assembling small, self-contained, and reusable entities that provide specific services and functionality to the system. A software component is, essentially, a small unit, with usually well-defined interfaces that form the basis of composition for a larger system. It encapsulates a set of related functions (or data) into a reusable unit.

Driver-based Components

Components are usually entities that enforce separation of concerns in a software system. They are modular and are responsible for delivering specific services to an application, say, for example, a Session component that handles states in a web application. What’s interesting is, you can build components in a manner that allows them to deliver their service in different ways while still providing the same contract it promises. This is the driver-based approach to designing components.

At the very core, you design the component with extensibility in mind, in a way that allows its default behavior to be replaced by objects that implement the component’s contract.

A driver is a specific implementation of a component’s contract to a software system. It provides an interface to an underlying infrastructure upon which the component’s service is built.

This idea of drivers and driver-based components is built into laravel and it’s supported out-of-the-box by the framework. This is the aspect of the framework we want to explore and see how we can use this pattern in our applications to build driver-based components.

Managers

The Manager Class

use Illuminate\Support\Manager;class FooManager extends Manager
{
//
}

Creating Drivers

create[Drivername]Driver()

Obtaining a Driver

The Fallback Driver

Extending the Component

protected function callCustomCreator($driver)
{
return $this->customCreators[$driver]($this->app);
}

The SMS Component

'providers' => [
// Other service providers...

App\Providers\SmsServiceProvider::class,
],
/**
*
* Get the default SMS driver name.
*
*
@return string
*/
public function getDefaultDriver()
{
return $this->app['config']['sms.default'] ?? 'null';
}
/**
* Get a driver instance.
*
*
@param string|null $name
*
@return mixed
*/
public function channel($name = null)
{
return $this->driver($name);
}
<?phpnamespace App\Components\Sms\Contracts;interface SMS
{
/**
* Send the given message to the given recipient.
*
*
@return mixed
*/
public function send();
}
SMS::to($phoneNumber)
->content('Building driver-based components in Laravel')
->send();
SMS::channel('twilio')
->to($phoneNumber)
->content('Using twilio driver to send SMS')
->send();

Добавить комментарий

Ваш e-mail не будет опубликован. Обязательные поля помечены *

Любишь мемасики?

Подпишись на мой телеграм-канал!

Открыть
Закрыть