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.
Installation via composer:
composer require tomrf/autowire
$autowire = new \Tomrf\Autowire\Autowire();
$instance = $autowire->instantiateClass(MyClassWithDependencies::class, $container);
composer test
This project is released under the MIT License (MIT). See LICENSE for more information.
Minimal class that facilitates dependency injection by autowiring class constructor dependencies from one or more PSR-11 containers.
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)[]
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
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