https://github.com/psr-framework/di
Very simple PSR-11 DI Container for PHP 7.4+
https://github.com/psr-framework/di
dependency-injection framework php php7 psr-11
Last synced: 5 months ago
JSON representation
Very simple PSR-11 DI Container for PHP 7.4+
- Host: GitHub
- URL: https://github.com/psr-framework/di
- Owner: PSR-Framework
- Created: 2020-07-14T10:07:09.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-07-22T15:32:50.000Z (almost 5 years ago)
- Last Synced: 2024-07-30T20:05:09.642Z (9 months ago)
- Topics: dependency-injection, framework, php, php7, psr-11
- Language: PHP
- Homepage:
- Size: 15.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## DI Container
Very simple PSR-11 DI Container for PHP 7.4+
[](https://github.com/Furious-PHP/di/releases)
[](https://scrutinizer-ci.com/g/Furious-PHP/di/build-status/master)
[](https://scrutinizer-ci.com/code-intelligence)
[](https://scrutinizer-ci.com/g/Furious-PHP/di)
[](https://codeclimate.com/github/Furious-PHP/di/maintainability)
[](https://packagist.org/packages/furious/container)
[](https://packagist.org/packages/furious/container)
[](LICENSE)Install:
composer require furious/container
Use:
$container = new Container();
// get
$container->get('key');
// has
$container->has('key');
// put integers
$container->set(1, 100);
$container->put(2, 200); // use set or put
// put string
$container->set('some string', 'some value');
// put array
$container->set('config', [
'debug' => true,
'some key' => 'some value'
]);
// put clojure
// return SomeClass instance
$container->set(SomeClass::class, function (ContainerInterface $container) {
return new SomeClass();
});
// put object
$container->set(SomeAnotherClass::class, new SomeAnotherClass('value', 'value'));
// autowiring
class A
{
public function __construct(B $b)
{
}
}
class B
{
}
$container->get(A::class);
// Factories
class Factory
{
public function __invoke(ContainerInterface $container)
{
return ExampleClass(
$container->get('key'),
$container->get('key'),
$container->get('key'),
$container->get('key')
);
}
}
$container->set(ExampleClass::class, function (ContainerInterface $container) {
return (new Factory())($container);
});
// Interfaces
interface SomeInterface
{
}
class SomeClass implements SomeInterface
{
}
$container->get(SomeInterface::class, function (ContainerInterface $container) {
return $container->get(SomeClass::class);
});