Download the PHP package njasm/container without Composer
On this page you can find all versions of the php package njasm/container. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download njasm/container
More information about njasm/container
Files in njasm/container
Package container
Short Description Dependency Container for PHP
License MIT
Homepage http://github.com/njasm/container
Informations about the package container
Dependency Container / Service Locator
Features
- Alias Service Keys support
- Circular Dependency guard
- Primitive data-types registration
- Automatic Constructor Dependency Resolution and Injection for non-registered Services
- Lazy and Eager instantiation approaches
- Lazy and Eager Instantiation Singleton services registration
- Support for public Setter injection/Method calls after service instantiation
- Support for public Properties/Attributes Injection after Service instantiation
- Ability to override existing dependency (Properties & Setters) declarations by supplying new ones when call to
Container::get
- Nested Providers/Containers support
- Comply with
Container-Interop
interfaces
Requirements
- PHP 5.3 or higher.
Installation
Include njasm\container
in your project, by adding it to your composer.json
file.
Usage
To create a container, simply instantiate the Container
class.
Using Alias
There are time that your key
are too long to be convenient for your client code, one example for instance,
is when binding an interface
to an implementation
or when using for your key
the FQCN of your classes.
Defining Services
Services are defined with two params. A key
and a value
.
The order you define your services is irrelevant.
Defining Services - Primitive data-types
Defining Services - Binding Services (Lazy Loading)
You can bind a key
to a instantiable FCQN value
.
If you want to bind a Service, and register that Service as a Singleton
Service.
Both Container::bind
and Container::bindSingleton
uses Lazy Loading approach,
so that \My\Namespace\SomeClass
will only be evaluated/instantiated when MyKey
is requested.
When binding a service, constructor dependencies can be declared, public attributes be set and methods called with arguments, so they are injected/setted when instantiating the service.
Defining Services - Objects (Eager Loading)
Overwriting existent declared dependencies is also possible for set
definitions.
Defining Services - Complex builds (Lazy Loading)
There are time when you'll want to instantiate an object, but the build process is reall complex and you want to control that process. You use anonymous functions for that.
Defining Services - Complex builds With Nested Dependencies (Lazy Loading)
Creation of nested dependencies is also possible. You just need to pass the container to the closure.
Defining Singleton Services
For registering singleton services, you use the singleton method invocation.
Defining Sub/Nested Containers
Nesting container is possible as long as you use an existing Container Adapter for your application existing container.
The Adapter class must implement the ServicesProviderInterface
for more examples please see the Adapter
folder.
Automatic Resolution of Services
When the Container is requested for a service that is not registered, it will try to find the class, and will automatically try to resolve your class's constructor dependencies.
Roadmap
In no Particular order - check Milestones for a more organized picture.
- [ ] Load definitions from configuration files
- [ ] Optimizations
Contributing
Do you wanna help on feature development/improving existing code through refactoring, etc? Or wanna discuss a feature/bug/idea? Issues and Pull Requests are welcome as long as you follow some guidelines for PRs:
Pull Requests must:
- Be PSR-2 compliant.
- Submit tests with your pull request to your own changes / new code introduction.
- having fun.