Resolver-Interop provides an interoperable package of standard interfaces for autowiring resolver functionality. It reflects, refines, and reconciles the common practices identified within several pre-existing projects.
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in BCP 14 (RFC 2119, RFC 8174).
This package defines the following interfaces:
-
ResolverService affords resolving classes, calls, parameter arrays, and types.
-
ReflectionParameterResolver affords resolving a ReflectionParameter to an argument value.
-
ReflectionMethodResolver affords invoking a method on an object.
-
ReflectionPropertyResolver affords setting a property on an object.
-
Resolvable affords allowing the implementing object to resolve itself to a value.
-
ResolverThrowable extends Throwable to mark an Exception as resolver-related.
ResolverService affords resolving classes, calls, parameter arrays, and types.
-
public function resolveClass( IocInterop\Interface\IocContainer $ioc, class-string<T> $class, mixed[] $arguments = [], ) : T;
-
Resolves the
$classto return a new instance. -
Directives:
-
Implementations MUST support parameter injection on the
$classconstructor using logic equivalent to that specified byresolveParameters(). -
Implementations MAY support ReflectionPropertyResolver attributes on the instantiated
$classproperties. -
Implementations MAY support ReflectionMethodResolver attributes on the instantiated
$classmethods. -
Implementations MAY support other forms of injection not specified herein.
-
Implementations MUST throw ResolverThrowable if the
$classcannot be resolved.
-
-
-
public function isResolvableClass(string $class) : bool;
- Does the
$classexist, and is it instantiable?
- Does the
-
public function resolveCall( IocInterop\Interface\IocContainer $ioc, callable $callable, mixed[] $arguments = [], ) : mixed;
-
Resolves the
$callableto return its result. -
Directives:
-
Implementations MUST support parameter injection on the
$callableusing logic equivalent to that specified byresolveParameters(). -
Implementations MUST throw ResolverThrowable if the
$callablecannot be resolved.
-
-
-
public function resolveParameters( IocInterop\Interface\IocContainer $ioc, ReflectionParameter[] $parameters, mixed[] $arguments = [], ) : mixed[];
-
Resolves the
$parametersinto the$arguments. -
Directives:
-
Implementations MUST NOT attempt to resolve a ReflectionParameter whose name or position already exists in the
$argumentskeys. -
When resolving a ReflectionParameter to an argument, implementations MUST do so using logic equivalent to that specified by ReflectionParameterResolver.
-
Implementations MUST retain each resolved ReflectionParameter by name in the
$arguments. -
Implementations MUST resolve all Resolvable objects in the
$arguments.
-
-
-
public function resolveType( IocInterop\Interface\IocContainer $ioc, ReflectionType $type, ) : ?string;
-
Resolves a ReflectionType to a string, or
nullif it cannot be be resolved. -
Notes:
- TBD Typically only for named types, but may help to convert union and intersection types to a single named type.
-
ReflectionParameterResolver affords resolving a ReflectionParameter to an argument value.
-
Notes:
- This interface can be implemented as an attribute. Doing so allows
implementors to define custom resolution approaches for consumers to
apply to specific ReflectionParameters. For example, implementors
may declare a
#[GetEnv($name)]attribute to resolve the ReflectionParameter to an environment value.
- This interface can be implemented as an attribute. Doing so allows
implementors to define custom resolution approaches for consumers to
apply to specific ReflectionParameters. For example, implementors
may declare a
-
public function resolveParameter( IocInterop\Interface\IocContainer $ioc, ReflectionParameter $parameter, ) : mixed;
-
Resolves the ReflectionParameter to an argument value.
-
Directives:
-
Implementations MUST resolve the
$parameterin this order:-
If the
$parameterhas an Attribute that implements ReflectionParameterResolver, implementations MUST resolve the$parameterusing that attribute. -
Otherwise, if the
$parametertype is resolvable using logic equivalent to the [ReflectionService][] methodresolveType()and the container has a service for that type, implementations MUST resolve the$parameterto that service. -
Otherwise, implementations MAY attempt to resolve the
$parameterusing implementation-specific logic; such logic is not defined herein. -
Otherwise, if the
$parameterhas a default value, implementations MUST resolve the$parameterto that value.
-
-
Implementations MUST throw ResolverThrowable if the
$parametercannot be resolved.
-
-
ReflectionMethodResolver affords invoking a method on an object.
-
Notes:
- This interface can be implemented as an attribute. Doing so allows implementors to define custom resolution approaches for consumers to apply to specific ReflectionMethods.
-
public function resolveMethod( IocInterop\Interface\IocContainer $ioc, ReflectionMethod $method, object $object, ) : void;
-
Invokes the ReflectionMethod on the
$object. -
Directives:
-
Implementations MUST support parameter injection using logic equivalent to that specified by ReflectionParametersResolver.
-
Implementations MUST throw ResolverThrowable if the
$methodcannot be resolved.
-
-
Notes:
- TBD $object is by reference so you can set to a replacement object a la immutability.
-
ReflectionPropertyResolver affords setting a property on an object.
-
Notes:
- This interface can be implemented as an attribute. Doing so allows implementors to define custom resolution approaches for consumers to apply to specific ReflectionPropertys.
-
public function resolveProperty( IocInterop\Interface\IocContainer $ioc, ReflectionProperty $property, object $object, ) : void;
-
Sets the ReflectionProperty on an object.
-
Directives:
- Implementations MUST throw ResolverThrowable if the
$propertycannot be resolved.
- Implementations MUST throw ResolverThrowable if the
-
Resolvable affords allowing the implementing object to resolve itself to a value.
-
Notes:
- TBD Use to defer container calls (i.e. lazy salls), then can use in $arguments without actually creating anything until the moment of resolution.
-
public function resolve(IocInterop\Interface\IocContainer $ioc) : mixed;
-
Resolves the implementing object to a value.
-
Directives:
-
Implementations MUST throw ResolverThrowable if the object cannot be resolved.
-
TBD Must recursively resolve all Resolvable in the resolved value.
-
-
ResolverThrowable extends Throwable to mark an Exception as resolver-related.
It adds no class members.
-
Directives:
- Implementations MAY define additional class members not defined in these interfaces.
-
Notes:
- Reference implementations may be found at https://github.com/resolver-interop/impl.