-
Notifications
You must be signed in to change notification settings - Fork 11k
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
Conversation
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 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)); |
The effect I was looking to achieve was not having to call send(), through(), and then() all the time. |
This may be helpful: |
The alternative solution achieves the same, but also gives other users some more control when needed. |
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: |
I'd prefer having it always return a 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. |
…hen() or thenReturn()
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(). |
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. |
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.