https://github.com/chipslays/container
📦 Yet another dependency injection container implementation with PSR-11 compliant for PHP 8.
https://github.com/chipslays/container
container di-container php-container php-di php-di-container
Last synced: 2 months ago
JSON representation
📦 Yet another dependency injection container implementation with PSR-11 compliant for PHP 8.
- Host: GitHub
- URL: https://github.com/chipslays/container
- Owner: chipslays
- Created: 2023-12-01T07:31:53.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-04-10T04:59:26.000Z (over 1 year ago)
- Last Synced: 2025-01-03T04:52:30.137Z (12 months ago)
- Topics: container, di-container, php-container, php-di, php-di-container
- Language: PHP
- Homepage:
- Size: 12.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 📦 DI Container
Yet another implementation of dependency injection container with PSR-11 compliant for PHP >8.0.
## Installation
```bash
composer require please/container
```
## Basic usage
This example always returns a new instance of `Mailer`.
```php
use Please\Container\Container;
use Please\Container\Support\Getter;
$container = new Container;
$container->bind(Mailer::class, function (Getter $get) {
return new Mailer::($get('user'), $get('password', 'qwerty')); // qwerty - default value
});
/** @var Mailer */
$mailer = $container->get(Mailer::class, [
'user' => 'admin',
// and password `qwerty` (default value)
]);
```
This example always return a same time.
```php
use Please\Container\Container;
$container = new Container;
$container->singleton('random', fn () => rand());
echo $container->get('random'); // 1234567890
echo $container->get('random'); // 1234567890
```
## Examples
You can find usage examples [here](/examples).
## Documentation
You can bind any abstract aliases.
```php
use Please\Container\Container;
$container = new Container;
$container->bind(Foo::class);
$container->get(Foo::class); // ok
$container->get('foo'); // error
// if you pass a class string
// it is also automatically bind as `Foo::class`
$container->bind('foo', Foo::class);
$container->get(Foo::class); // ok
$container->get('foo'); // ok
// array of aliases
$container->bind([Foo::class, 'foo', 'another-foo-alias'], Foo::class);
$container->get(Foo::class); // ok
$container->get('foo'); // ok
$container->get('another-foo-alias'); // ok
```
You can bind any primitive data types.
```php
$container->bind('foo', 'bar');
$container->bind('foo', 1337);
$container->bind('foo', 13.37);
$container->bind('foo', true || false);
$container->bind('foo', ['bar', 'baz']);
$container->bind('foo', new stdClass);
$container->bind('foo', fopen('php://stdout', 'r'));
$container->bind('baz', null);
$container->get('baz') // returns `baz`
```
Generates a singleton instance of a class.
> Singleton method always returns same value or instance like typical singleton pattern.
```php
$container->singleton('timestamp', fn () => time());
// or use bind class and `shared` parameter
$container->bind('timestamp', fn () => time(), shared: true);
```
Check if the container has a binding or instance for the given abstract.
```php
$container->bind('foo', 'bar');
$container->has('foo'); // true
$container->has('baz'); // false
```
## Singleton Pattern
If you really need to, you can use the container as a singleton.
```php
use Please\Container\Container as BaseContainer;
use Please\Container\Support\Traits\Singleton;
class Container extends BaseContainer
{
use Singleton;
}
...
$container = Container::getInstance();
...
```
You can also check singleton example [here](/examples/singleton.php).
## License
Open-sourced software licensed under the [MIT license](https://opensource.org/license/mit/).