Download the PHP package stefna/di without Composer
On this page you can find all versions of the php package stefna/di. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package di
Dependency Injection
This package is a lightweight dependency injection container that is framework-agnostic.
Requirements
PHP 8.2 or higher.
Installation
Usage
You will always have to use the ContainerBuilder
to create a Container
since we don't do any automatic autowiring, so you will have to define everything
you want to have access to
Configure container
To configure you container you add DefinitionSource
's to the container builder.
Bulk configure definitions
We provide 2 definition sources that can be used to bulk define stuff.
First you have FilterDefinition
that just allows you to provide any arbitrary filter that will be matched against
the requested class. And if it matches it will be given the definition.
Example:
We also provide a NamespaceFilterDefinition
that makes it easier to bulk define everything in a specific namespace.
The NamespaceFilterDefinition
just extends FilterDefinition
and makes it easier to use with namespaces.
Example:
If your going to use this to provide auto-wiring to lots of classes I would recommend extending FilterDefintion
and
implement PriorityAware
on the definition and return Priority::Low
so you easily can override the "default"
definition
Use with other container implementations
Autowiring
We do provide a helper that can do some lightweight autowiring.
The autowire helper will only look for dependencies in the container it will not try to auto create objects that aren't part of the container.
Attributes
You can augment the auto-wiring with attributes.
The auto-wire helper defaults to only fetch objects from the container.
We support 2 attribute interfaces
ResolverAttribute
can be used to resolve complex values from containerConfigureAttribute
can be used to reconfigure an object before injecting into class
ResolverAttribute
Can be useful when you want to resolve a scalar value from something like a config storage.
ConfigureAttribute
Can be useful when you want to reconfigure something that being injected for example setting a custom log channel for this class.
Factories
Everything in the definition is in practice a factory.
But we provide a factory helper that can help with deduplicate factory instances and lazy instantiate the factory.
Autowiring factories
There are 2 ways to auto wire a factory
Option 1 add it to the container like anything else
Option 2 use the factory auto wire helper
This is just a nice wrapper around option 1
Contribute
We are always happy to receive bug/security reports and bug/security fixes
License
The MIT License (MIT). Please see License File for more information.