Conia Wire
Wire provides an autowiring object creator that utilizes PHP's reflection capabilities to automatically resolve constructor arguments recursively. It additionally comes with classes that assist in resolving arguments of callables such as functions, methods, closures or class constructors. It can be combined with a PSR-11 dependency injection container.
Installation
composer require conia/wire
How to create objects
To create an object without knowing its classes' constructor arguments, simply
create a Creator
instance and pass the fully qualified class
name to its resolver method:
use Conia\Wire\Wire;
$creator = Wire::creator(); // creates a `Creator` instance
$model = $creator->create(Model::class);
For a complete introduction and fully working examples, see The Creator
How to resolve arguments
To get a list of instantiated arguments for a callable or a constructor you can
use one of the resolvers. Here you can see simplified example on how to use the
CallableResolver
:
function readValue(Value $value): string
{
return $value->str;
}
$resolver = Wire::callableResolver();
$args = $resolver->resolve('readValue');
readValue(...$args);
More information on callable and constructor argument resolvers together with fully functioning examples can be found here: Argument resolvers
Other features
- To be able to resolve generally unresolvable constructor or callable argument
types like literals (
string
,int
, etc.), interfaces, or abstract classes, Wire's classes can be combined with a PSR-11 compatible container implementation. See PSR-11 Containers. - If an object needs additional method calls after instantiation to be properly
initialized you can use the
Call
class attribute. - To override or change the default behaviour of the resolvers, or handle literal values, you can annotate parameters of callables and constructors with the Inject Attribute.