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. It complements Laravel’s existing tooling without forcing a paradigm shift.decorate() for cross-cutting concerns).Pipe facade but offers additional utilities (e.g., retry, race, tap).App\Exceptions\Handler).Illuminate\Support\Facades\Pipe or third-party alternatives (e.g., spatie/laravel-pipes)?Fun::middleware(), Fun::eloquent())?collect())?pipe() vs. compose())?Kernel::middleware() groups with functional composition.AuthDecorator, CacheDecorator).Fun::tap()->retry()).pipe($request, validate(), sanitize(), serialize())).| Phase | Action | Deliverables | Risk Mitigation |
|---|---|---|---|
| Evaluation | Benchmark 3 use cases (middleware, job, event listener) against native Laravel patterns. | Performance comparison report (CPU/memory). | Compare with Pipe and manual Closure chains. |
| Pilot | Replace 1-2 manual Closure chains in a non-production service (e.g., a custom middleware). |
Updated middleware with Fun composition. |
Rollback plan for performance issues. |
| Adoption | Standardize decorators in the service layer (e.g., App\Services\*). |
Internal documentation + code examples. | Enforce naming conventions (e.g., *Decorator). |
| Optimization | Add Laravel-specific helpers (e.g., Fun::eloquent(), Fun::middleware()). |
Custom facade or wrapper class. | Open PR to upstream if maintainable. |
| Scaling | Integrate with CI/CD (e.g., PHPStan rules for composed functions). | Updated linting/configuration. | Gradual rollout to avoid breaking changes. |
collect(), tap()).Illuminate\Support\Facades\Pipe.// Before: Manual closure chain
$request->pipe(fn($r) => $r->merge(['logged_at' => now()]))
->pipe(fn($r) => $r->validate(['email' => 'required']));
// After: Fun composition
use Fun\Compose;
$pipeline = Compose::pipe(
fn($r) => $r->merge(['logged_at' => now()]),
fn($r) => $r->validate(['email' => 'required'])
);
$request->pipe($pipeline);
Closure chaining in jobs, commands, or providers.Fun::middleware()) for Laravel conventions.// Custom middleware using Fun
$middleware = Fun::decorate(
app()->make(LoggingMiddleware::class),
fn($next) => fn($request) => logger()->info('Request received'), // Pre-decorator
fn($next) => fn($request) => $next($request), // Original middleware
fn($response) => fn($response) => logger()->info('Response sent') // Post-decorator
);
Fun::tap() for inspection).AuthDecorator runs before LogDecorator").How can I help you explore Laravel packages today?