ytake/starch

Dependency Injection Container For PHP

Maintainers

Details

github.com/ytake/starch

Source

Issues

Fund package maintenance!
ytake

1.0.0 2021-04-08 14:24 UTC

This package is auto-updated.

Last update: 2024-12-11 20:34:21 UTC


README

Dependency Injection Container For PHP.
Not Supported Autowiring.

Requirements

PHP 8.0 and above.

Installation

Composer is the recommended installation method.
To add Ytake\Starch to your project,
add the following to your composer.json then re-run composer:

{
  "require": {
    "ytake/starch": "^1.0"
  }
}

Run Composer commands using PHP like so:

$ composer install

or

$ composer require ytake/starch

Usage

First steps

Create Class

interface AnyInterface 
{

}
final class Any implements AnyInterface 
{
  // any
}

Bindings

use Ytake\Starch\Container;
use Ytake\Starch\Scope;

$container = new Container();
$container->bind(AnyInterface::class)
  ->to(Any::class)
  ->in(Scope::PROTOTYPE);

dependencies will be automatically resolved

$container->get(AnyInterface::class);

Scopes

use the Ytake\Starch\Scope class.

Providers

use Ytake\Starch\ProviderInterface.

use Ytake\Starch\ProviderInterface;

final class AnyProvider implements ProviderInterface {

  public function get(): AnyInterface {
    return new Any();
  }
}
$container->bind(AnyInterface::class)
  ->provider(new AnyProvider();