https://github.com/wp-forge/container
A lightweight, PHP 7.0+ compatible, PSR-11 dependency injection container.
https://github.com/wp-forge/container
Last synced: about 1 year ago
JSON representation
A lightweight, PHP 7.0+ compatible, PSR-11 dependency injection container.
- Host: GitHub
- URL: https://github.com/wp-forge/container
- Owner: wp-forge
- Created: 2020-03-22T22:05:19.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-04-30T01:03:19.000Z (about 3 years ago)
- Last Synced: 2025-04-11T05:09:07.587Z (about 1 year ago)
- Language: PHP
- Homepage:
- Size: 13.7 KB
- Stars: 4
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Container
A lightweight, PHP 8.0+ compatible, PSR-11 dependency injection container.
## Usage
Basic manipulation of items.
```php
set('email', 'webmaster@site.com');
// Check if a value exists
$exists = $container->has('email');
// Get a value
$value = $container->get('email');
// Delete a value
$container->delete('email');
```
Basic manipulation of items using array syntax.
```php
set( 'session', $container->factory( function( Container $c ) {
return new Session( $c->get('session_id') );
} ) );
// Get a factory instance.
$factory = $container->get( 'session' );
// Check if an item is a factory
$isFactory = $container->isFactory( $factory );
```
Register a service. Services return the same class instance every time you fetch them.
```php
set( 'session', $container->service( function( Container $c ) {
return new Session( $c->get('session_id') );
} ) );
// Get a service instance.
$service = $container->get( 'session' );
// Check if an item is a service
$isService = $container->isService( $service );
```
Register a computed value callback.
```php
'John',
'last_name' => 'Doe',
] );
$container->set( 'full_name', $container->computed( function ( Container $container ) {
return implode( ' ', array_filter( [
$container->has( 'first_name' ) ? $container->get( 'first_name' ) : '',
$container->has( 'last_name' ) ? $container->get( 'last_name' ) : '',
] ) );
} ) );
$full_name = $container->get( 'full_name' );
```
Extend a previously registered factory or service.
```php
extend( 'session', function( $instance, Closure $c ) {
$instance->setShoppingCart( $c->get('shopping_cart') );
return $instance;
} );
```