{"id":15692647,"url":"https://github.com/inmanturbo/pipes","last_synced_at":"2025-05-07T14:26:25.306Z","repository":{"id":252713738,"uuid":"841164901","full_name":"inmanturbo/pipes","owner":"inmanturbo","description":"Pipes for php with a simple api","archived":false,"fork":false,"pushed_at":"2025-02-03T01:37:46.000Z","size":67,"stargazers_count":7,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-30T19:57:02.896Z","etag":null,"topics":["functional-programming","gleam","laravel","php","php-library"],"latest_commit_sha":null,"homepage":"https://github.com/inmanturbo/pipes","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/inmanturbo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-08-11T20:37:20.000Z","updated_at":"2025-02-03T01:37:44.000Z","dependencies_parsed_at":"2024-08-12T02:28:04.629Z","dependency_job_id":"ff623848-22ff-4e05-b46d-f6345c3e3943","html_url":"https://github.com/inmanturbo/pipes","commit_stats":null,"previous_names":["inmanturbo/pipes"],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inmanturbo%2Fpipes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inmanturbo%2Fpipes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inmanturbo%2Fpipes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inmanturbo%2Fpipes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/inmanturbo","download_url":"https://codeload.github.com/inmanturbo/pipes/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252894003,"owners_count":21820907,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["functional-programming","gleam","laravel","php","php-library"],"created_at":"2024-10-03T18:37:02.815Z","updated_at":"2025-05-07T14:26:25.280Z","avatar_url":"https://github.com/inmanturbo.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Pipes for php with a simple api based on functional composition\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/inmanturbo/pipes.svg?style=flat-square)](https://packagist.org/packages/inmanturbo/pipes)\n[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/inmanturbo/pipes/run-tests.yml?branch=main\u0026label=tests\u0026style=flat-square)](https://github.com/inmanturbo/pipes/actions?query=workflow%3Arun-tests+branch%3Amain)\n[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/inmanturbo/pipes/fix-php-code-style-issues.yml?branch=main\u0026label=code%20style\u0026style=flat-square)](https://github.com/inmanturbo/pipes/actions?query=workflow%3A\"Fix+PHP+code+style+issues\"+branch%3Amain)\n[![Total Downloads](https://img.shields.io/packagist/dt/inmanturbo/pipes.svg?style=flat-square)](https://packagist.org/packages/inmanturbo/pipes)\n\nSimply put, it takes the output of the last one and pipes it to the next one. Sorta like bash  `cat ./file | grep -e 'waldo'`\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require inmanturbo/pipes\n```\n\nOr just copy or download the [`functions.php`](https://github.com/inmanturbo/pipes/blob/main/functions.php) file from this repository.\n\n# Usage\n\n## pipe()\n\n```php\n\nrequire __DIR__.'/../vendor/autoload.php';\n\nuse function Inmanturbo\\Pipes\\pipe;\n\n$addOne = fn ($number = 0) =\u003e $number + 1;\n\n$five = pipe($addOne) // 1\n    -\u003epipe(fn ($number) =\u003e $number + 1) // 2\n    -\u003epipe($addOne) // 3\n    -\u003epipe($addOne) // 4\n    -\u003ethenReturn(); // 4\n```\n\nIt doesn't delay execution\n\n```php\n$fifty = pipe(1);\n\n    while ($fifty-\u003eresult() \u003c 50) {\n        $fifty-\u003epipe(fn ($number) =\u003e ++$number);\n    }\n\necho $fifty-\u003eresult();\n// 50\n     \n```\n\nYou can also pass a class or a class string\n\n```php\n\nuse function Inmanturbo\\Pipes\\pipe;\n\nclass Subtract\n{\n    public function __invoke($number)\n    {\n        return $number - 1;\n    }\n}\n\n$addOne = fn ($number = 0) =\u003e $number + 1;\n\n$six = pipe($addOne, 1)\n    -\u003epipe($addOne)\n    -\u003epipe($addOne)\n    -\u003epipe($addOne)\n    -\u003ethen(fn ($number) =\u003e ++$number);\n\n$five = pipe($six)\n    -\u003epipe(Subtract::class)\n    -\u003ethenReturn();\n\n$three = pipe(new Subtract, $five)\n    -\u003epipe(new Subtract)\n    -\u003ethenReturn();\n\n```\n\n## Returning results\n\nresults can be returned three ways:\n\n`then()` or `thenReturn()` both a take final callback and return the result, or `result()`, which simply returns the result.\n\n```php\n$addOne = fn ($number = 0) =\u003e $number + 1;\n\n$six = pipe($addOne, 1)\n    -\u003epipe($addOne)\n    -\u003epipe($addOne)\n    -\u003epipe($addOne)\n    -\u003ethen(fn ($number) =\u003e ++$number);\n\n$sixAgain = pipe($addOne, 1)\n    -\u003epipe($addOne)\n    -\u003epipe($addOne)\n    -\u003epipe($addOne)\n    -\u003ethenReturn(fn ($number) =\u003e ++$number);\n\n$five = pipe($addOne, 1)\n    -\u003epipe($addOne)\n    -\u003epipe($addOne)\n    -\u003epipe($addOne)\n    -\u003eresult();\n```\n\n## Halting the pipeline\n\n## halt()\n\nYou can return `halt()` from a callback to halt the chain. `halt` takes an optional result as an argument which you can pass as the final `result()` of the chain. Subsequent calls to `-\u003epipe()` will not affect the final result.\n\n```php\n\n    use function Inmanturbo\\Pipes\\{pipe, halt};\n\n    $fortyFive = pipe(1);\n\n    $count = 1;\n    while ($count \u003c 50) {\n        $fortyFive-\u003epipe(fn ($number) =\u003e $number \u003c 45 ? ++$number : halt($number));\n\n        $count ++;\n    }\n\n    echo $fortyFive-\u003eresult();\n\n    // 45\n\n    echo $fortyFive-\u003epipe(fn ($number) =\u003e ++$number)-\u003eresult();\n\n    // 45\n\n```\n\nYou can also call halt on the pipe itself\n\n```php\n\n    use function Inmanturbo\\Pipes\\{pipe, halt};\n\n    $fortyFive = pipe(1);\n\n    $count = 1;\n    while ($count \u003c 50) {\n        \n        if (($number = $fortyFive-\u003eresult()) \u003e= 45) {\n            $fortyFive-\u003ehalt($number);\n        }\n\n        $fortyFive-\u003epipe(fn ($number) =\u003e ++$number);\n\n        $count ++;\n    }\n\n    echo $fortyFive-\u003eresult();\n\n    // 45\n\n    echo $fortyFive-\u003epipe(fn ($number) =\u003e ++$number)-\u003eresult();\n\n    // 45\n\n```\n\n## resume()\n\nYou can resume the piping with resume. `pipe()-\u003eresume()` takes an optional callback and behaves the same as `pipe()-\u003epipe()` if a callback is passed\n\n```php\n\nuse function Inmanturbo\\Pipes\\{pipe, halt};\n\n$fortySix = pipe(1);\n\n$count = 1;\nwhile ($count \u003c 50) {\n    \n    if (($number = $fortySix-\u003eresult()) \u003e= 45) {\n        $fortySix-\u003ehalt($number);\n    }\n\n    $fortySix-\u003epipe(fn ($number) =\u003e ++$number);\n\n    $count ++;\n}\n\necho $fortySix-\u003eresult();\n\n// 45\n\necho $fortySix-\u003eresume(fn ($number) =\u003e ++$number)-\u003eresult();\n\n// 46\n\n```\n\n## hop() and Laravel\n\nThis package doesn't require laravel to use pipe or `hop()`, but `hop()` (higher-order-pipe) is a higher order function intended for working with Laravel's [Pipeline](https://laravel.com/docs/11.x/helpers#pipeline) helper. This higher-order-function takes a callback which takes a single argument, and wraps the `$callback` for you in a closure which implements `function($next, $passable)`.\n\n```php\n\nuse Illuminate\\Pipeline\\Pipeline;\n\nuse function Inmanturbo\\Pipes\\hop;\n\nclass Add {\n    public function add($number)\n    {\n        return $number +1;\n    }\n}\nclass InvokeAdd {\n    public function __invoke($number)\n    {\n        return $number +1;\n    }\n}\n\n$five = (new Pipeline)-\u003esend(1)\n    -\u003epipe(hop(fn($number) =\u003e $number +1))\n    -\u003epipe(hop(new InvokeAdd))\n    -\u003epipe(hop(InvokeAdd::class))\n    -\u003epipe(hop(fn($number) =\u003e (new Add)-\u003eadd($number)))\n-\u003ethenReturn();\n\n// 5\n\n```\n\nYou can optionally pass a single `middleware` as a second argument to `hop()`, and it will get called before the first argument, which allows you to determine if the pipeline should halt before the `$callback` ever gets executed.\n\n```php\n\n$limitThreeMiddleware = function ($number, $next) {\n    if($number \u003e= 3) {\n        Log::info('Limit hit');\n        return $number;\n    }\n\n    return $next($number);\n};\n\n$five = (new Pipeline)-\u003esend(1)\n    -\u003epipe(hop(fn($number) =\u003e $number +1, $limitThreeMiddleware))\n    -\u003epipe(hop(new InvokeAdd, $limitThreeMiddleware))\n    // Limit hit\n    -\u003epipe(hop(InvokeAdd::class, $limitThreeMiddleware))\n    -\u003epipe(hop(fn($number) =\u003e (new Add)-\u003eadd($number), $limitThreeMiddleware))\n-\u003ethenReturn();\n\n// 3\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finmanturbo%2Fpipes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Finmanturbo%2Fpipes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finmanturbo%2Fpipes/lists"}