Upgrading to AOP 0.7
Version 0.7 replaces the earlier interception model with a single invocation object:
InterceptorInterface::intercept(object $target, ReflectionMethod $method, array $args, callable $next): mixedPipeline::through([...])->call($callable, $args)(a mutable fluent pipeline)PipelineHandler, an@internalstand-in used to provide aReflectionMethodto proxy interceptors- a per-call
ReflectionMethodconstruction in each generated proxy override
The new design uses one interceptor contract, chain implementation, and generated-code path.
Breaking changes
InterceptorInterface signature
// before
public function intercept(object $target, ReflectionMethod $method, array $args, callable $next): mixed;
// after
public function intercept(Invocation $invocation, callable $next): mixed;
$next now receives the Invocation instead of $args. Values previously read from
ReflectionMethod are available on the invocation:
| Before | After |
|---|---|
$method->getName() |
$invocation->method (null on the explicit path) |
$method->getDeclaringClass()->getName() |
$invocation->identity (or $invocation->target::class) |
$method->getAttributes(X::class) + newInstance() |
$invocation->advice(X::class) |
$method->getParameters() / getParameters()[$i]->getName() |
$invocation->parameterNames (proxy-baked) |
$next($args) |
$next($invocation) |
$target is now object|string|null. Explicit calls using closures or string callables
have no target object; interceptors that require an object must handle or reject those cases.
$next now takes the invocation, not the args
// before
public function intercept(object $target, ReflectionMethod $m, array $args, callable $next): mixed
{
return $next($args);
}
// after
public function intercept(Invocation $invocation, callable $next): mixed
{
return $next($invocation);
}
Pipeline::through() is deprecated; the chain is precomposed
Pipeline composes its chain once, in the constructor. Pass all interceptors there:
// before
$pipeline = (new Pipeline([$retry]))->through([$log]);
// after (preferred)
$pipeline = new Pipeline([$retry, $log]);
through() remains as a deprecated, immutable alias. It returns a new pipeline with the
receiver's interceptors followed by the supplied ones; it does not mutate the receiver.
Capture the returned instance:
$pipeline = (new Pipeline([$retry]))->through([$log]); // works, but capture the result
Pipeline::call() gained optional identity: and advice: parameters.
Pipeline::wrap() gained ?string $identity = null. Pipeline::handle(Invocation) is new.
Advice wins over constructor defaults (and is now uniform)
Both paths now prefer advice on the invocation and otherwise use constructor defaults:
new RetryInterceptor(defaultTimes: 5); // no advice present: 5 attempts
#[Retry(times: 2, backoff: 0)] // advice present: 2 attempts, not 5
public function flaky(): void { ... }
Previously, attributes were read through ReflectionMethod, so only proxies could use
them; Pipeline::call() had no advice parameter. Both paths now support
$invocation->advice(X::class), and explicit calls can pass an advice: map. Attribute
precedence is unchanged.
ProxyFactory::setCurrent() / ProxyFactory::current() removed
Generated proxies no longer use a process-wide "current factory." Each override embeds
the advice class names and interceptor namespace, then requests a plan from
AopRuntime::plan(<namespace>, class, method, [Advice::class, ...]). The runtime builds
and memoizes the pipeline and advice instances once per (class, method), rather than
per call. ProxyFactory::getInterceptor() remains for diagnostics.
The namespace derives from the proxy cache directory (ProxyFactory::namespace()).
setCacheDir() rebuilds proxies when that directory changes. Namespaces also isolate
factories in the same process, such as separate AppContext instances. Previously, one
factory could overwrite another's interceptors.
AppContext still calls registerInterceptor() and configures the factory.
ProxyFactory::register() publishes interceptors to AopRuntime; AppContext also
publishes the fully resolved map after invoking lazy factories. AppContext::registerInterceptor()
clears memoized plans so existing proxies use newly registered interceptors.
Generated proxies changed shape
Each generated call creates one Invocation and a by-reference variadic terminal closure
(fn(&...$a) => parent::method(...$a)). It no longer constructs a ReflectionMethod or
chain per call. Consequences:
- Reference parameters survive through the proxy and chain, including
&$argparameters and&...$argsvariadics. The 0.6 generator dropped by-reference variadics. parameterNamesis baked into the invocation, so#[Cache(key: 'order_{id}')]works on the proxied path with no per-call reflection.voidmethods are still emitted as a statement, not areturn(unchanged from 0.6).- Proxies declare no properties. The terminal closure is created per call because caching it on the instance caused clone binding, serialization, and property-name collision problems. The per-call closure adds about 0.2 µs per advised call.
GENERATOR_VERSION was bumped, so stale proxy cache files are ignored automatically; no manual cache flush is needed.
Explicit calls carry no advice by default
Pipeline::call() has no method attributes to read. Interceptors receive null advice
and use constructor defaults unless advice is passed explicitly:
$pipeline->call(
$callable,
args: [$id],
identity: 'orders.charge',
advice: [Retry::class => new Retry(times: 3, backoff: 100)],
);
Cache key templates resolve placeholders from argument names. On the explicit path, names
come from the $args array keys; Pipeline::call() has no separate parameterNames
argument. Pass named arguments such as ['id' => $id]. An unresolved placeholder raises
a LogicException instead of reusing one cache entry for different arguments.
New in 0.7
Azera\Aop\Invocation— the single interception context.Azera\Aop\AopRuntime::plan()— memoized(namespace, class, method) → planregistry used by generated proxies.@internal.Azera\Aop\AopRuntime::setInterceptors()/setInterceptor()/clearPlans()— per-namespace registry maintenance.@internal.Azera\Aop\AopPlan— the plan object (pipeline + advice map + parameter names), in its ownsrc/Aop/AopPlan.phpso PSR-4 can resolve it directly.@internal.ProxyFactory::namespace()— the interceptor namespace this factory publishes under, derived from its cache directory.@internal.Pipeline::ATTR_CALLABLE— the attribute key onInvocation::$attributesunder which the wrapped callable is stored ('azera.aop.callable').@internal; generated proxies andPipeline::call()use it to hand the terminal callable to the chain.Azera\Db\DatabaseManager::peek()— read a role's already-instantiated connection without triggering its factory. Used byTransactionalInterceptorto detect connections left inside a transaction.docs/16-AOP.md— rewritten for the new model; includes measured performance figures.
Proxy cache uses one file per class
Previously, the cache stored a content-addressed body (Proxy_<digest>.php) and a
per-class pointer (Proxy_<key>.ptr). Changed proxies used new paths, leaving old bodies
to be pruned.
Each class now has one file, Proxy_<key>.php, keyed by namespace and class. Its header
records the generator version, target and proxy class names, advice map, and source
mtime:size. A warm load reads the header without require or reflection and skips
generation when it matches. Updates replace the file in place.
The emitted-body digest now identifies the declared class instead of the filename. A new class name is required when the file changes, so workers that loaded the old class can continue using it.
Migration steps:
- No application changes are needed;
proxyPath()still returns the cache file path. ProxyFactory::pointerFileFor()is removed. UseproxyPath()to inspect or warm a cache entry.ProxyFactory::prune()andcache:pruneare removed because proxies no longer accumulate separate files.- After upgrading, run
cache:clear --only=aoponce. The generator cannot read or replace 0.6 files named after the emitted body.cache:statusreports how many remain.
Not changed
AppContext::registerInterceptor()— signature and semantics are unchanged; late registration still evicts cached#[Advised]definitions.#[Advised], method advice attributes, andAdviseddiscovery are unchanged. Proxy files are still written atomically withassertSafeCacheDir; the digest is now in the class name, and the per-class.ptrfile has been removed.- Existing built-in interceptor constructor arguments are unchanged. New optional
parameters were appended:
CacheInterceptoradds$defaultTtl,LogInterceptoradds$defaultLeveland$logArgs, andTransactionalInterceptoradds$defaultRoleand$detectLeakedConnections. These support explicit calls without advice. Subclasses that callparent::__construct(...)are unaffected; review subclasses that depend on the exact parameter list.
If you have written custom interceptors
Porting checklist:
- Change the signature to
intercept(Invocation $invocation, callable $next): mixed. - Replace
ReflectionMethodreads with$invocation->method/$invocation->parameterNames/$invocation->advice(). - Replace
$next($args)with$next($invocation). - For the class name, use
$invocation->target::classor$invocation->identity. - Remove cached
ReflectionMethodinstances; the proxy now owns per-method state.