Download the PHP package anax/di without Composer
On this page you can find all versions of the php package anax/di. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package di
Short Description Anax DI module, dependency injection (PHP-FIG PSR-11).
License MIT
Homepage https://dbwebb.se/anax
Informations about the package di
Anax DI
Anax DI service container for dependency injection of framework services using creation and lazy loading of services.
The implementation of the container is compliant with PHP-FIG PSR-11: Container interface
Table of content
- Install
- Development
- Basic usage
- Shared service
- Lazy loading
- Usage in Anax
- Anax initialization of services
- Anax configuration of services
- Dependency
- License
You can also read this documentation online.
Install
You can install the module from anax/di
on Packagist using composer.
Development
To work as a developer you clone the repo and install the local environment through make. Then you can run the unit tests.
Basic usage
This is the basic usage of the container.
First you create the container.
Add services onto the container.
Check if the service is loaded.
Get and use the service.
Shared service
A shared service always return the same object. The object is instantiated once and then stored for the next wanting to use the service. There is only one instance af a shared service. The service is shared among all its users.
A service that is not shared will return a new instance each time it is get from the container.
Lazy loading
The services added to the container are not activated until accessed. They are lazy loaded to ensure they are only active when used.
You can see what services are loaded onto the container and you can see what services are activated.
Usage in Anax
In Anax all services are loaded during the bootstrap phase in the file htdocs/index.php
. It looks like this.
The variable $di
is the only global variable within the framework. The main dependency, as a global variable, is to the be accessable within the view template files and view helpers.
Anax initialization of services
All the framework services are loaded from files in the directory ANAX_INSTALL_PATH . "/config/di"
. Each file contains one service. A file can contain several services.
This is a example of a service configuration file for the service "request".
A service may contain a callback that initiates the service when it is loaded. The callback should return the initiated object.
The service can be defined through its class name when the service needs no extra initialisation.
One common way to initialize the service is to inject $di
into the service. This is an example of that.
Anax configuration of services
During the initialization phase some services might want to read additional configuration information. They can do so by using anax/configuration
, like this.
First, lets have a look at the service "configuration".
It is a service to read configuration files from those directories that the framework specifies. It can be used like this, when creating other services that depends on their own configuration files.
The configuration setting "active" states wether the service should be activated when its loaded, or if it should be lazy loaded.
In the callback, first the service "configuration" is retrieved from $di. Then it is used to read the configuration file named "session".
The actual configuration is returned from the configuration file "session" and can then be used to configure and setup the object, before returning it as a framework service.
A configuration file is in general stored by its service name in ANAX_INSTALL_PATH . "/config/servicename.php"
. It should return something, like this.
Dependency
Using psr11 through psr/container
.
License
This software carries a MIT license. See LICENSE.txt for details.