- How do I integrate phpProxyBuilder with Laravel’s Service Container to wrap a repository like UserRepository?
- Bind the proxied class to Laravel’s container using `bind()` or `singleton()`, then configure interceptors. For example: `$this->app->bind(UserRepository::class, fn() => ProxyBuilder::build(UserRepository::class)->addMethodInterceptor('find', new LoggingInterceptor()));`. This ensures the proxy is injected transparently via Laravel’s DI system.
- Can phpProxyBuilder replace Laravel Middleware for non-HTTP cross-cutting concerns like caching or retries?
- Yes, but with broader scope. Middleware targets HTTP requests, while phpProxyBuilder can wrap any service method (e.g., `UserRepository::find()`) for caching, retries, or logging. Use Middleware for HTTP-specific logic and proxies for service-layer concerns. Both can coexist in Laravel.
- What Laravel versions and PHP requirements does phpProxyBuilder support?
- Requires **Laravel 9+** (PHP 8+) for attribute support, which aligns with Laravel’s modern DI system. PHP 8.0+ is mandatory for annotation-based proxy generation. For older Laravel versions, consider alternatives like `easycorp/proxy-manager` or annotations via `ramsey/uuid` compatibility layers.
- How do I test a proxied service in Laravel unit tests? Can I mock interceptors?
- Use `ProxyBuilder::getProxy()` to inspect the proxy instance and verify interceptor behavior. For mocking, replace the advice (e.g., `CachingAdvice`) with a test double in your test setup. Laravel’s `Mockery` or PHPUnit’s `createMock()` works seamlessly with proxied interfaces. Example: `$proxy = ProxyBuilder::build(Service::class)->addMethodInterceptor('method', $mockAdvice);`.
- Will phpProxyBuilder work with Laravel Queues or Job execution? Can I add retry logic dynamically?
- Absolutely. Wrap job classes or queue workers with interceptors for retries, telemetry, or validation. Example: Bind a proxied job to the container: `$this->app->bind(ProcessPaymentJob::class, fn() => ProxyBuilder::build(ProcessPaymentJob::class)->addMethodInterceptor('handle', new RetryInterceptor()));`. This applies to both Laravel’s native queues and third-party queue systems.
- What happens if proxy generation fails (e.g., unsupported class, circular dependencies)?
- The package throws exceptions with clear error messages (e.g., `UnsupportedClassException` or `CircularDependencyException`). Handle these in your container binding logic. For production, wrap bindings in try-catch blocks or use Laravel’s `bindIf()` to conditionally register proxies. Example: `try { $this->app->bind(..., fn() => ProxyBuilder::build(...)); } catch (Exception $e) { Log::error($e); }`.
- How do I pre-generate proxies to reduce runtime reflection overhead in Laravel?
- Use the `php-proxy-builder:generate` command (if available in future versions) or manually generate proxies during deployment. For now, leverage PHP’s `eval()` or a custom Artisan command to compile proxies at build time. Store generated proxies in `bootstrap/cache/` and bind them to Laravel’s container. This avoids runtime reflection costs for critical paths.
- Can phpProxyBuilder work with Laravel Facades? Will it break type hints or instanceof checks?
- Facades can use proxies if the underlying class is bound to the container. However, the current magic-methods implementation **does not** pass `instanceof` checks (e.g., `$proxiedService instanceof UserRepository` returns `false`). Future versions will support interface-based proxies for full type compatibility. For now, use dependency injection directly instead of facades for proxied services.
- Are there alternatives to phpProxyBuilder for AOP in Laravel? How does it compare to Middleware or Events?
- Alternatives include `easycorp/proxy-manager` (more mature, supports interfaces), Laravel’s native **Middleware** (HTTP-only), and **Events** (for decoupled workflows). phpProxyBuilder excels at **method-level interception** (e.g., logging before/after `UserRepository::find()`) without modifying business logic, while Middleware targets HTTP requests and Events are async. Choose based on scope: proxies for services, Middleware for HTTP, Events for async workflows.
- How do I monitor or debug proxy invocations in Laravel (e.g., with Telescope or OpenTelemetry)?
- Instrument interceptors to log or emit telemetry. For Laravel Telescope, use `Telescope::log()` inside custom advice. For OpenTelemetry, inject a tracer into interceptors (e.g., `new OpenTelemetryInterceptor($tracer)`). Example: `ProxyBuilder::build(Service::class)->addMethodInterceptor('method', new TelemetryInterceptor($tracer));`. Combine with Laravel’s `app['log']` for centralized monitoring.