Skip to content

Interoperable interfaces for autowiring resolvers in PHP.

License

Notifications You must be signed in to change notification settings

resolver-interop/interface

Repository files navigation

Resolver-Interop Standard Interface Package

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).

Interfaces

This package defines the following interfaces:

ResolverService

ResolverService affords resolving a class name to a new instance of that class.

ResolverService Methods

  • public function resolve(
        IocInterop\Interface\IocContainer $ioc,
        class-string $class,
        mixed[] $arguments = [],
    ) : T;
    • Returns a new instance of the $class with constructor $arguments.

    • Directives:

      • Implementations MUST support constructor injection using logic equivalent to that specified by ReflectionParametersResolver.

      • Implementations MAY support ReflectionPropertyResolver attributes on the instantiated class properties.

      • Implementations MAY support ReflectionMethodResolver attributes on the instantiated class methods.

      • Implementations MAY support other forms of injection not specified herein.

      • Implementations MUST throw ResolverThrowable if the $class cannot be resolved.

  • public function isResolvable(string $class) : bool;
    • Does the $class exist, and is it instantiable?

ReflectionParametersResolver

ReflectionParametersResolver affords resolving an array of ReflectionParameters to an array of named arguments.

ReflectionParametersResolver Methods

  • public function resolveParameters(
        IocInterop\Interface\IocContainer $ioc,
        ReflectionParameter[] $parameters,
        mixed[] $arguments = [],
    ) : mixed[];

ReflectionParameterResolver

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.

ReflectionParameterResolver Methods

  • public function resolveParameter(
        IocInterop\Interface\IocContainer $ioc,
        ReflectionParameter $parameter,
    ) : mixed;
    • Resolves the ReflectionParameter to an argument value.

    • Directives:

      • Implementations MUST resolve the $parameter in this order:

        • If the $parameter has an Attribute that implements ReflectionParameterResolver, implementations MUST resolve the $parameter using that attribute.

        • Otherwise, if the $parameter type is a ReflectionNamedType, and the container has a service for that type, implementations MUST resolve the $parameter to that service.

        • Otherwise, implementations MAY attempt to resolve the $parameter using implementation-specific logic; such logic is not defined herein.

        • Otherwise, if the $parameter has a default value, implementations MUST resolve the $parameter to that value.

      • Implementations MUST throw ResolverThrowable if the $parameter cannot be resolved.

ReflectionMethodResolver

ReflectionMethodResolver affords invoking a method on an object.

  • Notes:

    • TBD Marks a method for setter injection or post-instantiation invocation.

    • This interface can be implemented as an attribute. Doing so allows implementors to define custom resolution approaches for consumers to apply to specific ReflectionMethods.

ReflectionMethodResolver Methods

  • public function resolveMethod(
        IocInterop\Interface\IocContainer $ioc,
        ReflectionMethod $method,
        object $object,
    ) : object;
    • Invokes the ReflectionMethod on the $object, returning either the $object itself or a replacement object.

    • Directives:

    • Notes:

      • TBD Implement on TARGET_METHOD attributes for setter or immutable injection.

ReflectionPropertyResolver

ReflectionPropertyResolver affords setting a property on an object.

  • Notes:

    • TBD Marks a property for injection.

    • This interface can be implemented as an attribute. Doing so allows implementors to define custom resolution approaches for consumers to apply to specific ReflectionPropertys.

ReflectionPropertyResolver Methods

  • public function resolveProperty(
        IocInterop\Interface\IocContainer $ioc,
        ReflectionProperty $property,
        object $object,
    ) : void;

CallResolverService

CallResolverService affords invoking a callable with resolved ReflectionParameters and argument overrides.

CallResolverService Methods

  • public function resolveCall(
        IocInterop\Interface\IocContainer $ioc,
        callable $callable,
        mixed[] $arguments = [],
    ) : mixed;

InvokableResolver

InvokableResolver affords resolving the implementing object to a value.

  • Notes:

    • TBD Use e.g. to defer container calls (i.e. Lazy), then can use in $arguments without actually creating anything until the moment of resolution.

InvokableResolver Methods

  • public function __invoke(IocInterop\Interface\IocContainer $ioc) : mixed;
    • Resolves the implementing object to a value.

    • Directives:

ResolverThrowable

ResolverThrowable extends Throwable to mark an Exception as resolver-related. It adds no class members.

Implementations

Q & A


About

Interoperable interfaces for autowiring resolvers in PHP.

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages