Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Injector Laravel Package

yiisoft/injector

yiisoft/injector is a lightweight PHP dependency injection container for building objects and wiring dependencies via constructor, method, and property injection. Supports factories, config-based definitions, and PSR-friendly integration for Yii apps and beyond.

View on GitHub
Deep Wiki
Context7

Getting Started

Start by installing via Composer: composer require yiisoft/injector. The core class is Yiisoft\Injector\Injector, which you instantiate with optional configuration. Its simplest use case is automatic constructor injection—just call $injector->get(MyService::class) and it will resolve all constructor dependencies via reflection. For quick prototyping or small apps, this zero-configuration autowiring is often enough. Check the src/Injector.php source file first for signature clarity; the constructor signature is public function __construct(array $definitions = []), so begin defining overrides where needed (e.g., Injector::class => Injector::class for self-reference, or service interfaces to concrete implementations). No bootstrap file is strictly required—you can instantiate per-request or as a singleton in your app.

Implementation Patterns

  • Service Location & Type Hints: Use $injector->get(SomeServiceInterface::class) in factories or controllers to obtain interface-typed services. Combine with typed properties (PHP 8+) for cleaner controllers: public function __construct(private SomeServiceInterface $service) {}.
  • Definition Overrides: Prepend configuration arrays to override behavior. For example, [LoggerInterface::class => fn() => new FileLogger('app.log')]. Use Injector::build() to create partial definitions.
  • Factory Patterns: For complex creation logic, define factories as callable definitions: [CachedClient::class => fn(Injector $injector) => new CachedClient($injector->get(HttpClient::class))].
  • Lazy Resolution: Wrap expensive dependencies in lazy() (via Injector::lazy() helper) to defer instantiation until accessed.
  • Method Injection: Use $injector->invoke([$service, 'methodName']) to inject arguments into non-constructor methods—useful for event handlers or console commands.
  • Library Integration: Package your library with yiisoft/injector-compatible constructors (type-hinted, no hardcoded dependencies), enabling downstream apps to wire services without vendor lock-in.

Gotchas and Tips

  • Circular Dependencies: The injector detects them (throws CircularReferenceException), but it doesn’t resolve them. Design with constructor immutability and prefer composition over deep chains.
  • Reflection Limitations: Non-public constructors or properties won’t be injected—ensure constructors are public and properties use setter injection if needed (though not auto-wired). Use Injector::with() to customize reflection behavior if extending the package.
  • Definition Priority: Definitions passed to the constructor take precedence over auto-wiring. Always double-check if you’re accidentally overriding a concrete class definition unintentionally.
  • Debugging Failures: When get() throws an NotFoundException or AmbiguousDependencyException, inspect the stack trace—the message includes the missing interface/concrete class and the class requesting it. Enable DEBUG mode (if available in your version) to see full resolution paths.
  • Performance: Reflection is cached internally in the Injector instance but runs on every get() call unless the definition is cached manually (e.g., via Injector::build() + serialize()/unserialize()). For production, pre-build and cache the injector configuration.
  • Extensibility: You can extend Injector to add custom resolution logic (e.g., support for custom attributes), but prefer composition (e.g., middleware-like decorators) where possible—this package is designed to stay lean.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport
twbs/bootstrap4