Installation Add the package via Composer:
composer require dudu/proxy-interceptors
Register the bundle in config/bundles.php:
return [
// ...
Dudu\ProxyInterceptorsBundle\DuduProxyInterceptorsBundle::class => ['all' => true],
];
First Use Case: Intercepting a Service
Define an interceptor for an existing service (e.g., App\Service\MyService):
namespace App\Interceptor;
use Dudu\ProxyInterceptorsBundle\Interceptor\InterceptorInterface;
use Dudu\ProxyInterceptorsBundle\Interceptor\MethodInterceptor;
use Dudu\ProxyInterceptorsBundle\Interceptor\Invocation;
class MyServiceInterceptor implements InterceptorInterface
{
public function intercept(Invocation $invocation)
{
// Pre-processing logic
$result = $invocation->proceed();
// Post-processing logic
return $result;
}
}
Configure the Interceptor
In config/packages/dudu_proxy_interceptors.yaml:
interceptors:
App\Service\MyService:
- App\Interceptor\MyServiceInterceptor
Inject the Intercepted Service Use dependency injection as usual:
public function __construct(private MyService $myService) {}
Method-Level Interception
Intercept specific methods (e.g., save() in a repository):
interceptors:
App\Repository\UserRepository:
- App\Interceptor\UserRepositoryInterceptor
class UserRepositoryInterceptor implements MethodInterceptor
{
public function intercept(Invocation $invocation)
{
if ($invocation->getMethodName() === 'save') {
// Logic before/after save
}
return $invocation->proceed();
}
}
Chaining Interceptors Multiple interceptors are executed in declaration order:
interceptors:
App\Service\OrderService:
- App\Interceptor\LogInterceptor
- App\Interceptor\CacheInterceptor
Conditional Interception
Use Invocation::getMethodName() or Invocation::getArguments() to conditionally apply logic.
Proxy Generation The bundle auto-generates proxies at runtime. Avoid manual proxy creation.
$container->set('interceptor.chain.App\Service\MyService', $mockChain);
Circular Dependencies Interceptors themselves should avoid depending on the intercepted service to prevent circular proxy generation.
Method Signature Mismatches
Ensure intercepted methods match the original service’s signature (return types, parameters). Use Invocation::proceed() to delegate to the original method.
Lazy Loading Issues
If intercepting lazy-loaded services (e.g., Doctrine entities), ensure the interceptor handles null or uninitialized states gracefully.
Configuration Overrides
Interceptor configurations in config/packages/ take precedence over environment-specific configs (e.g., .env).
DUDU_PROXY_INTERCEPTORS_DEBUG=true in .env to log interceptor invocations.var/cache/. Clear cache (php bin/console cache:clear) after config changes.DUDU_PROXY_INTERCEPTORS_DEBUG to confirm execution order.Custom Invocation Objects
Extend Dudu\ProxyInterceptorsBundle\Interceptor\Invocation for advanced use cases (e.g., adding context data).
Dynamic Interceptors
Implement Dudu\ProxyInterceptorsBundle\Interceptor\DynamicInterceptorInterface for runtime-registered interceptors:
$container->get('interceptor.dynamic_registry')->addInterceptor(
'App\Service\MyService',
new DynamicInterceptor()
);
Event-Based Interception
Combine with Symfony events (e.g., kernel.request) for hybrid interception logic.
Invocation::getResult() to cache responses if applicable.How can I help you explore Laravel packages today?