- How does php-standard-library/fun compare to Laravel’s built-in `Pipe` facade for middleware or service composition?
- Fun offers more advanced functional utilities like `retry()`, `debounce()`, and `race()` beyond Laravel’s `pipe()`, making it ideal for complex workflows (e.g., retries with exponential backoff). However, for simple middleware chains, Laravel’s `Pipe` may be sufficient. Fun’s decorators also enable cross-cutting concerns (e.g., logging, caching) without subclassing.
- Can I use this package in Laravel 9/10 without breaking changes?
- Yes, the package requires PHP 8.1+ and has no Laravel-specific dependencies, so it works seamlessly in Laravel 9/10. It integrates with Laravel’s service container via closures or bound classes, avoiding version conflicts. Always check the [documentation](https://php-standard-library.dev) for minor updates.
- Will this package slow down my Laravel application if I compose many decorators?
- Performance depends on use case: simple compositions (e.g., 2–3 decorators) have negligible overhead, but heavy pipelines (10+ decorators) may introduce latency from closure execution. Benchmark critical paths using `microtime(true)` or Laravel’s `Benchmark` facade before production deployment.
- How do I decorate a Laravel service method (e.g., a repository) with Fun’s utilities?
- Use `Fun::decorate()` to wrap your service method. For example, to add logging and retry logic to a `UserRepository::find()` call, define a decorator closure: `Fun::decorate(fn($repo) => $repo->find($id), [Fun::log(), Fun::retry(3)])`. Bind the decorated closure to Laravel’s container if needed.
- Does this package support async operations like Laravel’s queues or Horizon?
- Yes, Fun’s `race()` and `promise()` utilities work with Laravel’s queues. For example, you can compose async retries: `Fun::retry(3, fn() => Bus::dispatch(new ProcessPodcast()))`. However, test thoroughly in staging, as async composition adds complexity to error handling.
- Can I use Fun’s memoization (`Fun::memoize()`) to cache Eloquent query results?
- While technically possible, avoid memoizing Eloquent queries directly due to Laravel’s dynamic query builder. Instead, memoize the *result* of a repository method (e.g., `Fun::memoize(fn() => $userRepository->getActiveUsers())`) and cache the output in Redis or the file system for persistence.
- What are the alternatives to php-standard-library/fun for functional programming in Laravel?
- Laravel’s native `Pipe` facade is the simplest option for basic chaining. For advanced use cases, consider `spatie/laravel-pipes` (Laravel-specific) or `league/pipe` (generic). Fun stands out for its execution control utilities (e.g., `debounce`, `throttle`) and decorator pattern, which aren’t covered by alternatives.
- How do I test middleware composed with Fun in Laravel’s PHPUnit/Pest tests?
- Mock the composed middleware in tests by replacing Fun’s utilities with test doubles. For example, use Pest’s `spies` to verify decorator calls: `spy(Fun::log()); $response = $this->call('GET', '/endpoint'); Fun::log()->shouldHaveReceived('__invoke').` Test edge cases like retries or throttling with `sleep()` or `Clock::freeze()`.
- Will this package work with Laravel’s first-party testing tools like HTTP tests or Dusk?
- Absolutely. Fun’s utilities are pure PHP and integrate with Laravel’s HTTP tests (e.g., composing middleware in `routes/web.php`) and Dusk (e.g., decorating browser interactions). Use `Fun::tap()` to inspect request/response objects during tests, similar to Laravel’s `tap()` helper.
- Are there plans to add Laravel-specific features like Eloquent query decorators or custom facades?
- The package is currently framework-agnostic, but the maintainers welcome contributions for Laravel integrations. Propose a feature (e.g., `Fun::eloquent()`) via GitHub issues or PRs. For now, extend Fun’s core utilities manually—its decorator pattern is designed for customization.