php-standard-library/fun
Functional programming utilities for PHP: compose and pipe callables, decorate functions, and control execution (memoize, throttle, debounce, retry, etc.). Part of PHP Standard Library with focused, reusable helpers for cleaner functional-style code.
collect(), tap(), pipe()), enabling cleaner middleware, event listeners, and service layer logic. Its focus on composition, decorators, and execution control directly addresses Laravel’s needs for modular, reusable logic (e.g., request/response transformations, async workflows).Closure chains with declarative composition (e.g., Fun::pipe(middleware1(), middleware2())).Fun::tap(job(), fn($result) => logger()->info($result))).reactphp), limiting use in event-driven architectures.Closure chaining can always replace fun() utilities.decorate() wrappers).App\Exceptions\Handler (e.g., converting functional errors to HTTP responses).Pipe or collect() may suffice for simple cases.Fun::decorate() reduce the need for custom decorator classes in the service layer?Fun::pipe() compare to Laravel’s collect()->pipe() or Illuminate\Pipeline\Pipeline?spatie/laravel-fun) be justified?Illuminate\Support\Facades\Pipe or spatie/laravel-pipes suffice?spatie/async-command or reactphp a better fit?Closure groups with functional composition (e.g., Fun::pipe(auth(), log(), validate())).Fun::decorate(userRepo(), cacheDecorator())).Fun::tap(job(), fn($result) => notify($result))).Fun::pipe(validate(), publish(), log())).app()->bind(LoggingDecorator::class, fn() => Fun::decorate(...))).Fun::mock() to stub composed functions in unit tests.| Phase | Action | Laravel-Specific Example | Risk Mitigation |
|---|---|---|---|
| Evaluation | Benchmark 3 use cases (middleware, job, event listener) against native Laravel patterns. | Compare Fun::pipe() vs. collect()->pipe() for request validation. |
Document findings for stakeholders. |
| Pilot | Replace 1-2 manual Closure chains in a non-production service (e.g., a legacy job). |
Convert a job with nested then() calls to Fun::pipe(). |
Rollback plan: revert to original code. |
| Adoption | Standardize decorators in the service layer (e.g., App\Services\*). |
Replace AuthService decorator classes with Fun::decorate(). |
Enforce via PR reviews. |
| Optimization | Add Laravel-specific helpers (e.g., Fun::middleware(), Fun::eloquent()). |
Create a facade: Fun::middleware(fn($next) => auth()->then($next)). |
Open PR upstream if maintainable. |
| Deprecation | Phase out custom decorator classes in favor of functional composition. | Deprecate AuthDecorator class after 6 months of Fun::decorate() usage. |
Provide migration scripts. |
collect()).Fun::mock() or Laravel’s container bindings.Fun::mock('retry', fn() => fn() => 'mocked_result');
Fun\* namespaces.then() calls with Fun::pipe().auth|log) to functional composition.Fun::eloquent()->where()).Fun::eventListener()).Fun::tap() for inspection).AuthDecorator runs before LogDecorator").rules:
- Fun\Decorators\DecoratorInterface::class must be type-hinted in composed functions.
How can I help you explore Laravel packages today?