apex / container
Light-weight DI container
Installs: 1 087
Dependents: 8
Suggesters: 0
Security: 0
Stars: 6
Watchers: 2
Forks: 0
Open Issues: 0
Type:package
Requires
- php: >=8.1
- psr/container: ^2.0
Requires (Dev)
- apex/signer: ^2.0
- phpunit/phpunit: ^9.5
README
A lightweight, straight forward dependency injection container that simply works, and works well. Supports config file and all standard injection methods -- constructor, setter, annotation, plus also attributes. Also includes a Di
wrapper class that allows container methods to be accessed statically for greater simplicity and efficiency.
Installation
Install via Composer with:
composer require apex/container
Basic Usage
Supports the standard container methods -- has, get, set, make, call
,
use Apex\Container\Container; $cntr = new Container('/path/to/definitions.php'); // Set and get $cntr->set('full_name', 'John Doe'); // Get service $logger = $cntr->get(LoggerInterface::class); $logger->info("Here I am"); // Make and set $user = $cntr->makeset(User::class, ['id' => 311]); // Call via method setter injection $cntr->call([$user, 'updateStatus'], ['status' => 'inactive']);
Constructor
Constructor takes the following parameters, none of which are required.
Mark Items as Services
You must explicitly mark necessary items as services, meaning the first time they are retrived from the container, they will be automatically instantiated with the new object instance set in the container for future use. Retrieving instantiable items from the container that are not marked as services will simply return the class name / closure, and will not instantiate the object.
You may easily mark items as services with the markItemAsService()
method, for example:
use Apex\Container\Container; use Apex\Psr\Log\LoggerInterface; $cntr = new Container(); $cntr->markItemAsService(LoggerInterface::class); $cntr->markItemAsService('debugger');
Methods
The following base methods are supported, as per all containers.
Singleton Di Class
This package includes the Di
class which acts as a singleton and allows container methods to be accessed statically, instead of passing the container object from class to class, method to method. This is done to provide efficiency and simplicity, and also assumes you're only managing one DI container per-request.
Modify the properties within the /src/Di.php
file as they are used as the constructor parameters for the container. Example usage is below.
namespace myApp; use Apex\Container\Di; use myApp\Users\User; // Get logger $logger = Di::get(LoggerInterface::Class); // Make user object Di::make(User::class, ['id' => 52]); // Set and get variable Di::set('full_name', 'John Doe'); $name = Di::get('full_name'); // John Doe
Follow Apex
Loads of good things coming in the near future including new quality open source packages, more advanced articles / tutorials that go over down to earth useful topics, et al. Stay informed by joining the mailing list on our web site, or follow along on Twitter at @mdizak1.