Download the PHP package slava-basko/container without Composer
On this page you can find all versions of the php package slava-basko/container. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download slava-basko/container
More information about slava-basko/container
Files in slava-basko/container
Informations about the package container
Dependency Injection Container
For those who understand what it is and strive for simplicity.
Zero dependencies and PHP 7.1+.
Features
- Simple API: Container API is as simple as regular array.
- Tags: Organize services using tags and fetch them in groups (consider them aliases).
- Shared Instances: Easily create singletons with a built-in helper.
- Service Extenders: Modify/Extend services after creation and before they are fully resolved.
- Circular Dependency Detection: Automatically prevents and reports circular dependencies.
- Autowiring: Container will automatically resolve dependencies.
- Rewrite Protection: Reports about accidental service rewrite.
- Service Providers: Package your services into a provider for better organization.
- Symlinks: Runtime hot-swap.
- Graph: Generate DOT script to visualize dependencies (Graphviz).
Simple API
Consider Container as a regular array.
Easier than ever for simple script/app.
Tags
Things get a bit more complicated when you need tags. Use the Container::add() method to setup tags.
Use the Container::getByTag() method to retrieve services by tags.
This separate method exists because regular get should return a specific service or throw a NotFoundException
exception.
Clear and strict separation: Container::get() returns a service or throws NotFoundException,
and the Container::getByTag() returns an array of services of throws NotFoundException.
Alias
This container has no separate feature like alias for two main reasons.
- Container has a
tagsfeature, basicallyalias. - To keep container code as simple as possible.
Use tagging functionality to achieve the same $service = $container->getByTag('tag')[0];.
Don't worry about Warning: Undefined array key 0 because container does not return an empty array. It will return
an array of services or throw NotFoundException like regular get.
Most likely, you want to use alias to avoid class name exposure. For example when you want to get service
by requests param.
Shared Instances
Use Container::addShared() method to create a shareable service.
Container will return the same instance each time.
Service Extenders
Use Container::extend() to modify services by a common parent.
Circular Dependency Detection
Container will let you know if you have a circular dependency.
Autowiring
Use AutowireContainer for automatic dependency resolving.
Rewrite Protection
Container will throw an exception if you accidentally rewrite the service definition.
But what if I need to overwrite definition!
Shift your mindset from "overwrite" to "setup based on X condition" and do it in the user-land code.
This approach, together with Rewrite Protection, will keep your code strict and more understandable.
There will be no hidden overwrites.
Service Providers
Providers give you the benefit of organizing your definitions.
Then, add the provider to the container.
Symlinks
You can create symlinks that allow you to get different services on the run, without changing user-land code.
Note: Only one symlink to a specific service can exist at a time.
Graph
Using the DOT language, build a graph (as in nodes and edges, not as in bar charts). Note: Graph not working
with AutowireContainer.
For example, you can visualize the output of Graphviz::build() with https://dreampuf.github.io/GraphvizOnline.
Graph element explanation:
- Oval — parameter
- Solid Rectangle — service
- Dashed Rectangle — shared service
- Parallelogram — non-existent service or param
PSR-11
Install psr/container package first.
Then wrap Container in PsrContainer and pass it to whoever expected Psr\Container\ContainerInterface.