tomrf/autowire

autowire dependency injection from PSR-11 containers

0.1.2 2023-08-02 13:19 UTC

This package is auto-updated.

Last update: 2024-12-01 00:20:19 UTC


README

PHP Version Require Latest Stable Version License

A simple PHP library that facilitates autowired dependency injection by fetching class constructor dependencies from one or more PSR-11 containers.

Autowire uses the PHP reflection API to peek at constructor parameter types and injects required and optional dependencies from assigned containers.

📔 Go to documentation

Installation

Installation via composer:

composer require tomrf/autowire

Usage

$autowire = new \Tomrf\Autowire\Autowire();

$instance = $autowire->instantiateClass(MyClassWithDependencies::class, $container);

Testing

composer test

License

This project is released under the MIT License (MIT). See LICENSE for more information.

Documentation

📂 Tomrf\Autowire\Autowire::class

Minimal class that facilitates dependency injection by autowiring class constructor dependencies from one or more PSR-11 containers.

resolveDependencies()

Returns array of resolved dependencies for a class constructor.

Dependencies are resolved from one or more PSR-11 containers.

Throws AutowireException if a required dependency could not be met using available containers.

public function resolveDependencies(
    object|string $classOrObject,
    Psr\Container\ContainerInterface $containers
): array

@throws   \Tomrf\Autowire\AutowireException
@return   (null|object)[]

instantiateClass()

Return a new instance of a class after successfully resolving all required dependencies using provided containers.

Throws AutowireException if the class does not exist or if a required dependency could not be met using available containers.

public function instantiateClass(
    string $class,
    Psr\Container\ContainerInterface $containers
): object

@throws   \Tomrf\Autowire\AutowireException

listDependencies()

Returns an array of all dependencies (method parameters) and relevant attributes for a given class or object/callable.

public function listDependencies(
    object|string $classOrObject,
    string $methodName = '__construct'
): array

@throws   \Tomrf\Autowire\AutowireException
@return   array<string,bool|string>[] Array of parameters with attributes

Generated 2022-06-28T01:41:09+02:00 using 📚tomrf/readme-gen