- How can I use Fun to simplify Laravel middleware composition instead of manually stacking middleware groups?
- Fun lets you compose middleware as callable pipelines. Replace `$middleware->handle($request)` chains with `Fun::compose([$middleware1, $middleware2])($request)`. This avoids repetitive `app()->make()` calls and keeps middleware logic DRY. Works seamlessly with Laravel’s `Kernel` or custom middleware stacks.
- Does Fun work with Laravel’s built-in `pipe()` method, or is it a replacement?
- Fun complements Laravel’s `pipe()` but offers more advanced features like decorators, memoization, and execution control. Use `Fun::pipe()` for functional-style chaining (e.g., `Fun::pipe($request, [$middleware1, $middleware2])`), while `pipe()` is limited to simple callable sequences. Fun provides finer-grained control for complex workflows.
- Can I use Fun to decorate Eloquent models or repositories for cross-cutting concerns like logging or caching?
- Yes. Use `Fun::decorate()` to wrap repository methods or model observers. For example, decorate a `UserRepository` with logging: `Fun::decorate($repo, fn($next) => fn($userId) => logger()->info('Fetching user'), $next($userId))`. This avoids scattering logic across multiple classes and keeps decorators reusable.
- What Laravel versions and PHP requirements does Fun support, and will it conflict with Laravel’s functional helpers?
- Fun requires PHP 8.1+ and works with Laravel 9+. It won’t conflict with Laravel’s `collect()`, `tap()`, or `pipe()` since it operates at a lower level (callable composition). Tested alongside Laravel’s functional tools, but avoid mixing `Fun::compose()` with `collect()->pipe()` in the same chain for clarity.
- How do I test Fun-decorated functions in Laravel’s Pest or PHPUnit?
- Mock composed functions like any callable. For example, in Pest: `Fun::decorate($service, fn($next) => fn() => $next())->shouldBeCalled()`. Use `partialMock()` for partial mocking of decorated methods. Fun’s pure PHP design makes it easy to isolate and test individual decorators or composed pipelines.
- Is Fun suitable for production use in Laravel, or should I stick to native middleware/classes?
- Fun is production-ready for functional-style workflows, but benchmark first. For simple middleware or single-method decorators, native classes may be clearer. Use Fun where composition reduces boilerplate (e.g., event listeners, job pipelines). Monitor memory usage in high-traffic apps, as deep composition can add overhead.
- Are there performance trade-offs for using Fun’s decorators vs. traditional Laravel service classes?
- Decorators add minimal overhead (~1-5ms per call for simple decorators), but deep composition (10+ decorators) can slow execution. Compare with native Laravel patterns: Fun’s `decorate()` is faster than manual `tap()` chains but slower than direct method calls. Profile with `microtime(true)` before/after critical paths.
- Does Fun integrate with Laravel’s service container, or do I need to manually bind decorators?
- Fun works with Laravel’s container via closures or bound classes. Bind decorators like any service: `$app->bind(LoggingDecorator::class, fn($app) => Fun::decorate($app->make(Service::class), $loggingDecorator))`. This keeps decorators testable and container-managed, just like native Laravel services.
- What alternatives to Fun exist for functional programming in Laravel, and when should I choose them?
- Alternatives include `spatie/laravel-pipes` (simpler piping) and Laravel’s native `pipe()`. Choose Fun for advanced features like `memoize()`, `throttle()`, or `retry()`. Use `spatie/laravel-pipes` for lightweight middleware composition, or native `pipe()` for basic callable chaining. Fun excels in cross-cutting concerns (logging, caching) where decorators shine.
- How can I contribute to Fun or request Laravel-specific features like Eloquent query decorators?
- Contribute via GitHub issues or pull requests to the [PHP Standard Library repo](https://github.com/php-standard-library/fun). For Laravel-specific features (e.g., Eloquent decorators), propose a design in the issues section. The team welcomes extensions, but focus on reusable functional patterns first. Check the [contributing guide](https://github.com/php-standard-library/php-standard-library/blob/next/CONTRIBUTING.md) for setup.