Download the PHP package ody/container without Composer
On this page you can find all versions of the php package ody/container. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ody/container
More information about ody/container
Files in ody/container
Package container
Short Description dependency injection container for ODY framework
License MIT
Informations about the package container
container
⚠️ IMPORTANT: This repository is automatically generated from the ody repo and is read-only.
Please submit any pull requests or issues to the main ody repo instead.
Container
Overview
The ODY Container is a service container that manages class dependencies and performs dependency injection. Using a container makes it easy to manage and centralize the way objects are created in your application.
Installation
Basic Usage
Creating a Container
Binding
Basic Binding
Singleton Binding
Singleton bindings only resolve the object once and return the same instance on subsequent calls:
Instance Binding
You can bind an existing instance into the container:
Resolving
Auto-Resolution
The container can automatically resolve classes with their dependencies:
Advanced Features
Contextual Binding
Contextual binding allows you to specify different implementations based on the class that is requesting the dependency:
Tagged Services
You can tag related bindings and then resolve them all at once:
Extending Resolved Objects
You can extend a resolved object to add additional functionality:
Method Invocation with Dependency Injection
The container can call methods with automatic dependency injection:
Working with Swoole Coroutines
When working with Swoole's coroutines, you might need to scope certain bindings to the current coroutine:
API Reference
Container Methods
Binding
bind($abstract, $concrete = null, $shared = false)
: Register a binding with the containersingleton($abstract, $concrete = null)
: Register a shared bindingscoped($abstract, $concrete = null)
: Register a binding scoped to the current coroutineinstance($abstract, $instance)
: Register an existing instance as sharedextend($abstract, Closure $closure)
: Extend a resolved instancebindIf($abstract, $concrete = null, $shared = false)
: Register a binding if not already registeredsingletonIf($abstract, $concrete = null)
: Register a shared binding if not already registeredscopedIf($abstract, $concrete = null)
: Register a scoped binding if not already registered
Resolving
make($abstract, array $parameters = [])
: Resolve a type from the containerget($id)
: Resolve a type from the container (PSR-11 compatible)build($concrete)
: Instantiate a concrete instance of a classfactory($abstract)
: Get a closure to resolve the given typecall($callback, array $parameters = [], $defaultMethod = null)
: Call a method with dependency injection
Alias and Tagging
alias($abstract, $alias)
: Alias a type to a different nametag($abstracts, $tags)
: Assign tags to bindingstagged($tag)
: Resolve all bindings for a tag
Contextual Binding
when($concrete)
: Define a contextual bindingaddContextualBinding($concrete, $abstract, $implementation)
: Add a contextual binding
Lifecycle Events
resolving($abstract, Closure $callback = null)
: Register a resolving callbackafterResolving($abstract, Closure $callback = null)
: Register an after resolving callbackbeforeResolving($abstract, Closure $callback = null)
: Register a before resolving callback
Container Management
flush()
: Clear all bindings and resolved instancesforgetInstance($abstract)
: Remove a resolved instanceforgetInstances()
: Clear all resolved instancesforgetScopedInstances()
: Clear all scoped instancesbound($abstract)
: Check if a binding existsresolved($abstract)
: Check if a type has been resolvedhas($id)
: Check if a binding exists (PSR-11 compatible)
ContextualBindingBuilder Methods
needs($abstract)
: Define the abstract target that depends on the contextgive($implementation)
: Define the implementation for the contextual bindinggiveTagged($tag)
: Define tagged services to use as the implementationgiveConfig($key, $default = null)
: Define a configuration value to use as the implementation
Exception Handling
The container throws various exceptions in different scenarios:
BindingResolutionException
: Thrown when a concrete cannot be built or resolvedCircularDependencyException
: Thrown when circular dependencies are detectedEntryNotFoundException
: Thrown when a binding is not found (implements PSR-11)
Integration with ODY Framework
The ContainerHelper
class provides utility methods for integrating the container with the ODY framework:
License
The ODY Container is open-sourced software licensed under the MIT license.