Giter Club home page Giter Club logo

laravel-pipeline-passable's Introduction

Passable

Latest Version on Packagist Total Downloads

Laravel's Pipeline class only takes a single input parameter. This unfortunately limits how you can use that class to your advantage. This is an opinionated solution using a Passable class that extends Spatie's Data Transfer Object. It's just something I needed myself in several projects, so I extracted it to a package.

Installation

Via Composer

$ composer require corbosman/laravel-pipeline-passable

Usage

You should be familiar on how Laravel Pipelines work. They're used in the Router and Middleware. It lets you pass a variable through a set of classes, and return the result. Sort of like array_reduce. Since you can't pass in multiple variables, it's not possible to send in some kind of input and output that are separate. That's where this package comes in. It allows you to create a class with multiple properties, and in the end return one of the properties as the result of the pipeline. By default, it just returns the class itself as the result, but you can implement a return method that returns whatever you want.

This package extends Spatie's DataTransferObject, so you can check their docs on how you can instantiate this class. It helps to understand what a DTO is and how you can use it. There is two common patterns, you either pass your class properties through the constructor, or you create a static method.

Constructor

use CorBosman\Pipeline\Passable;

class MyPipeline extends Passable
{
    public string $username;
    public array $output;

    public function return() : array
    {
        return $this->output;
    }
}

To use this pipeline, construct it using the properties as parameters, then give it some classes to pipe through.

$output = (new MyPipeline(['username' => $foo, 'output' => []])->pipeline([
    PipeClass1::class,
    PipeClass2::class
    ...
]);

Factory

Sometimes it's easier to use a factory method. For example, you can use it to initialise a response DataTransferObject.

use CorBosman\Pipeline\Passable;

class MyPipeline extends Passable
{
    public string $username;
    public Output $output;
    
    public static function factory($username) : self
    {
        return new self([
            'username' => $username,
            'output'   => new Output
        ]);
    }

    public function return() : array
    {
        return $this->output->toArray();
    }
}
$output = MyPipeline::factory($username)->pipeline([
    PipeClass1::class,
    PipeClass2::class
    ...
]);

Pipeline classes

To send this Passable through Laravel's Pipeline, you call the pipeline method with an array of classes. Just like with middleware classes, you have to make sure that your class calls the next class, as shown in the examples below. The Pipeline class allows several different options.

  • Normal class with a handle method
class Uppercase
{
    public function handle(MyPipeline $passable, $next)
    {
        $passable->output = strtoupper($passable->username);

        return $next($passable);
    }
}
  • An invokable class
class Uppercase
{
    public function __invoke(MyPipeline $passable, $next)
    {
        $passable->output = strtoupper($passable->username);

        return $next($passable);
    }
}
  • An object, note that the object should have the handle method on it
$uppercase = new Uppercase;
$result = MyPipeline::factory($input)->pipeline([$uppercase]);

Changing the called method

By default the handle() method is called on each pipeline class. If you want to override that, you can set the method name as the second parameter of the pipeline() method.

$result = MyPipeline::factory($input)->pipeline([...], 'filter');

Change log

Please see the changelog for more information on what has changed recently.

Contributing

Please see contributing.md for details and a todolist.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

MIT. Please see the license file for more information.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.