Interface ContextualAttributeInterface
Interface for contextual PHP attributes that resolve an argument with access to the reflection parameter and the container.
Unlike {@see AttributeInterface::resolve()}, which is parameter-blind, this receives the parameter being resolved (name, type, default) and the container, enabling resolution by parameter name/type, pulling dependencies from the container, or combining configuration with the parameter default as fallback:
#[Attribute(Attribute::TARGET_PARAMETER)]
final class ConfigValue implements ContextualAttributeInterface
{
public function __construct(private string $key) {}
public function resolve(ReflectionParameter $parameter, ContainerInterface $container): mixed
{
return Configure::read($this->key)
?? ($parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : null);
}
}
Namespace: Cake\Container\Attribute
Method Summary
-
resolve() public
Resolve the value for the given parameter.
Method Detail
resolve() ΒΆ public
resolve(ReflectionParameter $parameter, Psr\Container\ContainerInterface $container): mixed
Resolve the value for the given parameter.
Parameters
-
ReflectionParameter$parameter The parameter being resolved.
-
Psr\Container\ContainerInterface$container The container (parent container when available).
Returns
mixed