https://github.com/0bbyte/container
PHP FIG PSR-11 container implementation
https://github.com/0bbyte/container
container dependency-injection php psr-11 psr-11-compliant
Last synced: 5 months ago
JSON representation
PHP FIG PSR-11 container implementation
- Host: GitHub
- URL: https://github.com/0bbyte/container
- Owner: 0bbyte
- License: mit
- Created: 2020-03-02T12:50:41.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-04-16T07:51:11.000Z (about 6 years ago)
- Last Synced: 2025-04-11T05:04:04.294Z (about 1 year ago)
- Topics: container, dependency-injection, php, psr-11, psr-11-compliant
- Language: PHP
- Size: 33.2 KB
- Stars: 5
- Watchers: 1
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
[![MIT License][license-shield]][license-url]
# Container
Dependency injection PHP FIG PSR-11 container implementation
## Requirements
`php > 7.1`
## Install
via Composer
```bash
$ composer require flotzilla/container
```
## Usage
Init service container with constructor parameters
```php
$container = new \flotzilla\Container\Container(
[
'EmptyTestClassDI' => EmptyTestClass::class, // class without dependencies, init by classname
'TestClassDI' => [TestClass::class, 'message'], // class with constructor string parameter
'TestClassDI2' => function () { // closure, that returns new class instance
return new TestClass('test');
},
'ClosureDI' => function ($x, $y) { // closure, that returns sum result
return $x + $y;
},
'TestClassWithDependecyDI' => [TestClassWithDependecy::class, 'TestClassDI'] // class with dependency of another service
]
);
```
Or with setter
```php
use \flotzilla\Container\Container;
$container = new Container();
$container->set('LoggerDI', function () { return new Logger();});
$container->set('ClosureDI', function ($x, $y) { return $x + $y;});
$container->set('EmptyTestClassDI', ClassWithoutConstructor::class);
$container->set('ClassDIWithDependency', [SomeClass::class, 'message']);
$container->set('AnotherClassWithDIDependency', [TestClass::class, 'LoggerDI']);
```
Get your component in code
```php
$logger = $container->get('LoggerDI');
```
Get your closure with arguments
```php
$container->set('ClosureDI', function ($x, $y) { return $x + $y;});
$logger = $container->getWithParameters('ClosureDI', [1, 2]); // will return 3
```
## Testing
```bash
$ composer test
```
## License
The MIT License (MIT). Please see [License File](https://github.com/flotzilla/container/blob/master/LICENCE.md) for more information.
[license-shield]: https://img.shields.io/github/license/othneildrew/Best-README-Template.svg?style=flat-square
[license-url]: https://github.com/flotzilla/container/blob/master/LICENCE.md