Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Added Pipeline Helper Function #46198

Closed
wants to merge 4 commits into from

Conversation

JordanDalton
Copy link

@JordanDalton JordanDalton commented Feb 21, 2023

Pipelines are quite powerful in Laravel. GIven how quick one may wish to utilize this feature I would like to propose the pipe() helper function so developers can quickly and cleanly leverage this feature.

@rodrigopedra
Copy link
Contributor

May I suggest an alternative implementation?

if (! function_exists('pipe')) {
    /**
     * Send the given payload through a pipeline, or return a pipeline instance.
     *
     * @param  $payload
     * @param  array  $pipes
     * @return mixed
     */
    function pipe()
    {
        $arguments = func_get_args();

        if (empty($arguments)) {
            return app(\Illuminate\Pipeline\Pipeline::class);
        }

        if (count($arguments) === 1) {
            return app(\Illuminate\Pipeline\Pipeline::class)
                ->send($arguments[0]);
        }

        $payload = array_shift($arguments);

        if (! is_array($arguments[1])) {
            throw new InvalidArgumentException(
                'When sending a value through a pipeline, you must pass an array of pipes.'
            );
        }

        return app(\Illuminate\Pipeline\Pipeline::class)
            ->send($arguments[0])
            ->through($arguments[1])
            ->thenReturn();
    }
}

Some other helpers use this approach of returning just a new instance when no arguments are passed. Check the cache() helper as an example.

This would allow usage like these:

$pipes = [
    // some pipes to send the value through
];

function doSomethingWithValue ($value) {
    // do something with the value
    return $value;
}

$result = pipe()
    ->send($value)
    ->through($pipes)
    ->then(doSomethingWithValue(...)); // First class callable syntax

// or

$result = pipe($value)
    ->through($pipes)
    ->then(doSomethingWithValue(...));

// or, with your original syntax

$result = doSomethingWithValue(pipe($value, $pipes));

@JordanDalton
Copy link
Author

JordanDalton commented Feb 21, 2023

The effect I was looking to achieve was not having to call send(), through(), and then() all the time.

@imanghafoori1
Copy link
Contributor

@henzeb
Copy link
Contributor

henzeb commented Feb 21, 2023

The effect I was looking to achieve was not having to call send(), through(), and then() all the time.

The alternative solution achieves the same, but also gives other users some more control when needed.

@JordanDalton
Copy link
Author

calling app(), send(), and through() will always be true, it's just the return that's the question, which makes the following solution more appealing:

#46170 (comment)

@nunomaduro nunomaduro changed the title Added Pipeline Helper Function [10.x] Added Pipeline Helper Function Feb 22, 2023
@rodrigopedra
Copy link
Contributor

rodrigopedra commented Feb 22, 2023

I'd prefer having it always return a Pipeline instance. But that makes me wonder, how is that much different from doing:

app(Pipeline::class)->send($value)->through($pipes)->thenReturn();

Or from adding a static constructor to the Pipeline class itself.

Aside of being shorter, of course. But from what I've been reading on the PR's since a few years ago, new global helpers are to be vetted very carefully, as often many complain when new ones are added due to global namespace conflict with locally defined helpers.

@JordanDalton
Copy link
Author

I've updated the PR so that it will return the Pipline so the user has choice if they want to use then() or thenReturn().

@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If possible, please consider releasing your code as a package so that the community can still take advantage of your contributions!

If you feel absolutely certain that this code corrects a bug in the framework, please "@" mention me in a follow-up comment with further explanation so that GitHub will send me a notification of your response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants