nurse / di
Dependency injection library
Installs: 2 318
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 1
Open Issues: 0
Requires
- php: >=7.0
- container-interop/container-interop: ^1.2
- psr/container: ^1.0
Requires (Dev)
- phpunit/phpunit: ~6.0
- satooshi/php-coveralls: ~1.0.0
This package is not auto-updated.
Last update: 2024-12-21 17:53:44 UTC
README
Very simple Dependency Injection Container for PHP
Code information:
Package information:
Installing
Installing via Composer
Append the lib to your requirements key in your composer.json.
{ // composer.json // [..] require: { // append this line to your requirements "nurse/di": "dev-master" } }
For PHP 5.3 and 5.4 support use version 0.9.2.
Alternative install
- Learn composer. You should not be looking for an alternative install. It is worth the time. Trust me ;-)
- Follow this set of instructions
Issues/Features proposals
Here is the issue tracker.
Usage
Defining a dependency:
$container = new Nurse\Container; // Defining a dependency $container->set('connection', function ($container) { $params = $container->get('connection_params'); return new Connection($params); }) ->set('connection_params', function () { return array( 'schema' => 'someschema', 'username' => 'root', 'password' => 's3cr3t', ); }); // Retrieving the dependency (lazy loading) $connection = $container->get('connection'); // alternatively you can use the singleton instance of the container Nurse\Di::set('connection', function ($container) { $params = $container->get('connection_params'); return new Connection($params); }) ->set('connection_params', function () { return array( 'schema' => 'someschema', 'username' => 'root', 'password' => 's3cr3t', ); }); $connection = Nurse\Di::get('connection');
You can also create factories:
<?php namespace App; use Nurse\Factory\FactoryInterface; use Psr\Container\ContainerInterface; class ConnectionFactory implements FactoryInterface { public function createService(ContainerInterface $container) { $params = $container->get('connection_params'); return new Connection($params); } public function getKey() { return 'connection'; } }
And then:
$factory = new \Dummy\MyDummyFactory(); $actual = $container->addFactory($factory);
Contributing
Please refer to the contribuiting guide.
License
Authors
- Marcelo Jacobus
- Maicon Pinto
- Anthonny Machado
- Emerson Engroff
- Daniel CaƱa
- Elisandro Nabienger