Download the PHP package grithin/ioc-di without Composer
On this page you can find all versions of the php package grithin/ioc-di. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download grithin/ioc-di
More information about grithin/ioc-di
Files in grithin/ioc-di
Package ioc-di
Short Description IoC DI with service container
License GPL-3.0-only
Homepage https://github.com/grithin/php-ioc-di
Informations about the package ioc-di
IoC DI
A small IoC Dependency Injector and Service Locator with experimental features, including a Data Locator.
Use
HEADER
For the code examples, use this header to run
Basic
Service Locator injection of "NameInterface" typed parameter into newly constructed "Creature"
Mixing Service Locator And Provided Parameters
Sometimes it is useful to allow the ServiceLocator to find one parameter, to but provide another parameter instead of using the ServiceLocator for it.
Defaults
If the DependencyInjector can't resolve a dependency, you can provide a fallback default for the parameter.
Because it may be undesirable to first instantiate a default prior to it being used, special classes are available to prevent pre-instantiation.
These special classes include
- \Grithin\IoC\Service
- \Grithin\IoC\Datum
- \Grithin\IoC\Call
- \Grithin\IoC\Factory
See the Special Types
Special Types
Using Grithin\IoC\SpecialTypeInterface
Service Object
Used by: SL, DI
- If you want to point to a service with options.
- If you want to provide a parameter default as an object, but don't want to instantiate the object unless it is for default
Example of using Service
for a default
Datum
Used by: SL, DI
- If you want configure a service to be instantialized using datum that might be set later or change
Call
Used by: SL, DI, DL
- If you want to point to the result of some callable.
Factory
Used by: SL, DI, DL
- Used to indicate to DataLocator it should use thing as a factory.
Whereas, with Call
, a datum will be resolved by calling, with Factory
, it will be resolved by calling every time the data is accessed from the DataLocator
Resolution Of Parameters
For some method definition, how does the injector resolve the injection values?
- if
with
variant/option is used (call_with
andcall(..., ['with'=>[]]))
, usewith
array- by position
- by name
- by type declaration (ServiceLocator)
- if parameter starts with uppercase, attempt to match against
- Service
- Datum
- if
defaults
option is present, use it - use the default provided in the method/constructor definition
Parameter Name Based Resolution
As a shortcut for well-known frequently used expected parameters, injection can use the name alone so long as that name starts with a capital letter. And, in this case, both services and data are checked.
Static analysis and autocompletion within editors has made this usually a bad option.
ServiceLocator
Beyond The Basics
Singleton
For something like a Database, it is desirable that only one primary instance be created.
You can also just provide an existing singleton object to the ServiceLocator
Links
It may be useful to link one service id to another service
Special Types
It may be useful to resolve a service using a special type.
Here, a function is used to get a service
Using Service
for special configuration
Data Locator
A service locator for data.
Notes
By default, SL does not check all classes to resolve an interface or abstract class, it only checks what is within the services (by id) or available without further file inclusion. You can make it check everything though: