https://github.com/blockifywp/container
Simple PHP dependency injection container with auto-wiring
https://github.com/blockifywp/container
Last synced: 9 days ago
JSON representation
Simple PHP dependency injection container with auto-wiring
- Host: GitHub
- URL: https://github.com/blockifywp/container
- Owner: blockifywp
- Created: 2024-04-24T08:45:55.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-05-16T13:57:32.000Z (about 2 years ago)
- Last Synced: 2026-03-27T15:31:09.786Z (2 months ago)
- Language: PHP
- Size: 7.81 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Blockify Container
Simple PHP dependency injection container with autowiring for WordPress plugins and themes.
## Installation
```bash
composer require blockify/container
```
## Usage
### PHP
First, require Composer's autoloader and then register the container:
```php
require_once __DIR__ . '/vendor/autoload.php';
namespace MyNamespace;
use Blockify\Container\Container;
use Blockify\Container\Registerable;
$container = new Blockify\Container\Container();
$service_providers = [
MyServiceProvider::class => [ __FILE__ ],
AnotherServiceProvider::class => [],
];
foreach ( $service_providers as $service_provider => $args ) {
$instance = $container->make( $service_provider, ...$args );
if ( $instance instanceof Registerable ) {
$instance->register();
}
}
```
Dependencies will be automatically resolved and injected into the constructor:
```php
namespace MyNamespace;
class MyServiceProvider {
private string $file;
public function __construct( string $file ) {
$this->dependency = $dependency;
}
public function getFile(): string {
return $this->file;
}
}
class AnotherServiceProvider implements Registerable {
private MyServiceProvider $my_service_provider;
public function __construct( MyServiceProvider $my_service_provider ) {
$this->my_service_provider = $my_service_provider;
}
public function register() {
echo $this->my_service_provider->getFile();
}
}
```