Download the PHP package flyokai/amphp-injector without Composer
On this page you can find all versions of the php package flyokai/amphp-injector. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download flyokai/amphp-injector
More information about flyokai/amphp-injector
Files in flyokai/amphp-injector
Package amphp-injector
Short Description A dependency injector for bootstrapping object-oriented PHP applications.
License MIT
Homepage https://github.com/amphp/injector
Informations about the package amphp-injector
flyokai/amphp-injector
User docs →
AGENTS.mdA dependency injection container for PHP 8.1+ with weaver-based parameter resolution, ordered compositions, attribute-driven wiring, and lifecycle management.
flyokai/amphp-injector is a substantially-evolved descendant of amphp/injector. The container is now driven by weavers (composable parameter resolvers), built around an immutable Application that owns a Container and an Injector, with first-class support for Composition collections, PHP 8 attributes, lifecycle hooks, and alias resolution.
Heads up. Despite the package name and namespace, this is a fork. If you arrived here looking for upstream
amphp/injector'sInjector::make(),define(),share(),delegate()API — that API is gone. Read on for what replaced it.
Features
Application— entry point that holds aContainerandInjector, implementsLifecycle- Definition helpers —
singleton(),object(),value(),factory(),injectableFactory(),compositionFactory(),compositionItem() - Weavers —
names(),types(),runtimeTypes(),automaticTypes(),any()for parameter resolution - PHP 8 attributes —
#[ServiceParameter],#[SharedParameter],#[PrivateParameter],#[FactoryParameter(Class)] - Compositions —
CompositionOrderedwith topological sort viabefore/after/depends - Aliases — one-way interface → implementation via
AliasResolverImpl - Lifecycle —
start()in dependency order,stop()in reverse - Lazy proxies — pluggable via
ProxyDefinition(seeexamples/proxy.php)
Installation
The package's composer.json replaces amphp/injector so the namespace Amp\Injector\… resolves to this fork.
Quick start
Build flow
- Create a
Definitionscollection containing service / object / value / factory / composition definitions. - Create an
Injectorwith a rootWeaver(typicallyany(...)chaining several weavers). - Construct
Application(injector, definitions, name, ?aliasResolver). - The
Applicationcallsdefinition->build($injector)for every definition and registers providers in theContainer. application->start()walks the providers and starts everyLifecycleinstance in dependency order.$container->get($id)retrieves services.application->stop()stops in reverse order.
Definition helpers
singleton(Definition, mustStart = false)
Wraps any definition to cache the instance — subsequent get() calls return the same object. mustStart=true requires the service to be started before first get().
object(string $class, ?Arguments $arguments = null)
Prototype factory — creates a new instance via constructor reflection every call. Wrap in singleton() for sharing.
value(mixed $value)
Wraps a literal — no construction logic.
factory(\Closure $factory, ?Arguments $arguments = null)
Prototype factory from a closure. The closure can accept a ProviderContext to inspect the injection site:
injectableFactory(string $class, ?\Closure $factory = null, ?Arguments $arguments = null)
Returns a callable from the container, not an instance. Some parameters are pre-injected; remaining ones are passed at call time:
compositionFactory(\Closure $factory, ?Definitions $itemDefinitions = null, ?Arguments $arguments = null)
Creates a composition — a collection of items built from sub-definitions. The factory receives all items as named arguments.
compositionItem(Definition $definition, array $before = [], array $after = [], array $depends = [])
Wraps a definition as a CompositionItem for use inside CompositionOrdered:
Weavers
Weavers resolve constructor / function parameters to definitions. They're chained inside Arguments — first match wins.
names(array $definitions = [])
Resolves by parameter name — the most common weaver:
types(array $definitions = [])
Resolves by parameter type — explicit class → definition mapping. Also indexes parent classes / interfaces.
runtimeTypes(Definitions $defs, AliasResolver $aliasResolver)
Resolves via PHP 8 attributes on parameters. Supported attributes:
| Attribute | Resolves to |
|---|---|
#[ServiceParameter] |
shared singleton instance of the parameter's type |
#[SharedParameter] |
shared instance scoped to the current definition |
#[PrivateParameter] |
new instance per injection site |
#[FactoryParameter(Class::class)] |
injectable factory (callable) returning a Class instance |
automaticTypes(Definitions $defs, AliasResolver $aliasResolver)
Auto-wires by type from all registered definitions. Returns a definition only if exactly one matches the type — ambiguous matches return null.
any(Weaver ...$weavers)
Tries multiple weavers in order, returns the first match. Typical setup:
Alias resolution
Maps interfaces to implementations via AliasResolverImpl. Aliases are one-way — requesting Foo yields FooImpl, but requesting FooImpl directly resolves through FooImpl's own definition.
Compositions (ordered collections)
CompositionOrdered items get topologically sorted via before / after / depends:
CompositionImpl is the simple unordered variant. Use selfFactory() as the factory closure for both.
Lifecycle
Services implementing Lifecycle are managed by the application:
start()— called after every definition is built; walks the dependency graph; starts in dependency orderstop()— called on shutdown; reverse ordersingleton($definition, mustStart: true)— service must be started before firstget()SingletonProvider->lazy()— defer initialization to firstget()instead ofstart()
Lazy proxies
Pluggable via custom Definitions using ocramius/proxy-manager. See examples/proxy.php:
The built-in ProxyDefinition currently throws not supported yet — provide your own Definition subclass.
Examples
The examples/ directory contains runnable scripts for every feature:
| File | Demonstrates |
|---|---|
singleton.php |
Basic singleton + value definitions |
runtime.php |
Attribute-driven runtime types + compositions |
delegation.php |
Factories and injectableFactory |
logger.php |
ProviderContext for site-aware factories |
proxy.php |
Lazy-loading proxy definitions |
benchmark.php |
Performance harness |
Gotchas
- Aliases are one-way.
Interface ⇒ Impllets you request the interface. RequestingImpldirectly usesImpl's own definition, not the alias. - Class names are normalised to lowercase internally — don't rely on case-sensitive keys.
- Ambiguous auto-wiring — if two definitions share a type,
automaticTypesreturnsnull. Disambiguate withnames()ortypes(). - Circular dependencies are not detected — they cause infinite recursion. Refactor or use
lazysingletons. - Containers are immutable — every
with()returns a clone; theApplicationholds the final reference. mustStartsingletons —get()beforeapplication->start()throwsLifecycleException.- Variadic parameters — only supported via
injectableFactory(). Plainfactory()doesn't pass variadics through. - Built-in
ProxyDefinitionis not implemented — seeexamples/proxy.phpfor a custom approach.
Differences from upstream amphp/injector
| Upstream | This fork |
|---|---|
Injector::make(), define(), share(), delegate(), prepare() |
gone — replaced by Application + Definitions + weavers |
define() arrays |
arguments(names()->with(...)) |
share() |
singleton(...) |
alias() (Injector method) |
AliasResolverImpl (separate object) |
| Per-injector instance API | Immutable Application / Container / Definitions |
| No compositions | Composition, CompositionOrdered, CompositionItem first-class |
| No attribute-driven wiring | #[ServiceParameter], #[SharedParameter], #[PrivateParameter], #[FactoryParameter] |
| No formal lifecycle | Lifecycle::start() / stop() walked in dependency order |
See also
flyokai/application— uses this DI as the runtime container; see alsovendor/flyokai/flyokai/docs/dependency-injection.mdfor diconfig structureflyokai/composition— module-level topological sort (different layer thanCompositionOrdered)flyokai/generic—TunerContainer/ExecutionContaineruse compositions internally- Original: https://github.com/amphp/injector
License
MIT