ejsmont-artur/php-proxy-builder
Runtime proxy builder for PHP that wraps any object with Advice (AOP-style) to add cross-cutting behavior like caching. Proxies transparently delegate method calls to the target while keeping proxy, advice, and client code fully decoupled.
app() binding) and Middleware (for HTTP proxies).UserRepository).CacheableInterface).HandleIncoming, Middleware).bind() or singleton().$this->app->bind(
UserRepository::class,
fn() => ProxyBuilder::build(UserRepository::class)
->addMethodInterceptor('find', new LoggingInterceptor())
);
php-proxy-builder:generate) to mitigate runtime cost.| Risk Area | Mitigation Strategy |
|---|---|
| Reflection Overhead | Pre-generate proxies for performance-critical services. |
| Attribute Support | Ensure PHP 8+ and Laravel 9+ (attributes are stable). Fallback to annotations if needed. |
| Container Conflicts | Test with Laravel’s reset() and bindIf() to avoid singleton collisions. |
| Debugging Complexity | Use ProxyBuilder::debug() or Xdebug to inspect proxy chains. |
| Testing | Mock proxies in unit tests; verify interceptors via ProxyBuilder::getProxy(). |
UserService) or specific methods (e.g., findById)?resolveCallback) conflict with proxy method resolution?LoggingInterceptor) be registered and managed (e.g., via config, DI, or annotations)?brick/aspect).ProxyBuilder’s attribute support (fallback to easycorp/proxy-manager if needed).LoggerService to add context to logs.UserRepository).php artisan proxy:generate --class=App\Services\UserService --interceptors=Logging,Cache
| Component | Compatibility Notes |
|---|---|
| Laravel DI Container | Fully compatible; proxies can replace or extend bindings. |
| Interfaces/Abstracts | Proxies work best with interfaces (e.g., CacheableInterface). |
| Closures/Lambdas | Avoid proxying closures; use concrete classes instead. |
| Laravel Events | Proxies can wrap Event::dispatch() for cross-cutting logic. |
| Queues | Proxies can wrap job payloads or execution (e.g., add retries). |
| Blade/Directives | No direct impact, but proxies could wrap view composers or service providers. |
app() container and a service class.BootstrapServiceProvider.ProxyBuilder::debug().PROXY_PATTERN.md to explain proxy usage, interceptors, and debugging.ProxyException handler for generation failures.php-proxy-builder:generate to compile proxies at deploy time.opcache.reset()).| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Proxy Generation Failure | App crashes on missing service | Fallback to raw class instantiation. |
| Interceptor Exception | Breaks method chain | Wrap interceptors in try-catch. |
| Circular Proxy Dependency | Infinite loop | Detect cycles via ProxyBuilder. |
| OPcache Invalidation |
How can I help you explore Laravel packages today?