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()). It could complement Laravel’s existing tooling for middleware, event handling, and service layer logic.decorate() for cross-cutting concerns).Closure chaining in jobs, commands, or providers.App\Exceptions\Handler).fun()->middleware(), fun()->eloquent())?pipe() vs. compose())?Illuminate\Support\Facades\Pipe or third-party packages (e.g., spatie/laravel-pipes) suffice?Kernel::middleware() groups with functional composition.AuthDecorator, CacheDecorator).fun()->tap()->retry()).Closure chaining in jobs/commands.Fun::middleware()) for Laravel conventions.// Before
$middleware = app()->make(LoggingMiddleware::class);
// After
$middleware = Fun::decorate(
app()->make(LoggingMiddleware::class),
fn ($next) => fn ($request) => logger()->info('Before'), // Pre-decorator
);
collect()).| Phase | Action | Risk Mitigation |
|---|---|---|
| Evaluation | Benchmark 3 use cases (middleware, job, event listener). | Compare with native Laravel patterns. |
| Pilot | Replace 1-2 manual Closure chains in a non-production service. |
Rollback plan for performance issues. |
| Adoption | Standardize decorators in service layer (e.g., App\Services\*). |
Document patterns for the team. |
| Optimization | Add Laravel-specific helpers (e.g., Fun::eloquent()). |
Open PR to upstream if maintainable. |
AuthDecorator runs before LogDecorator").compose(), decorate()).## Decorating Eloquent Queries
```php
$query = Fun::decorate(
User::query(),
fn ($query) => $query->where('active', true),
fn ($query) => $query->with('posts'),
);
*Decorator suffix for classes).| Risk | Mitigation Strategy | Detection Method |
|---|---|---|
| Undefined Behavior | Use Fun::safe() to wrap composed functions. |
PHPStan + runtime assertions. |
| Performance Regression | Set baseline benchmarks for critical paths. | Laravel Forge + Blackfire. |
| Debugging Nightmares | Log decorator execution order. | Fun::tap() for inspection. |
| Package Abandonment | Fork critical features if upstream stalls. | Monitor GitHub activity. |
fun() after 3 months.// Old
class LoggingMiddleware implements Middleware { ... }
// New (with fallback)
$middleware = Fun::decorate(
app()->make(LoggingMiddleware::class),
// Fallback to class if fun() fails
fn ($next) => fn ($request) => $next($request),
);
How can I help you explore Laravel packages today?