Skip to content

๐Ÿ›๏ธ Autowire and configure dependencies

License

Notifications You must be signed in to change notification settings

seeren/container

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Seeren\Container

Build Require Coverage Download Codacy Badge Version

Autowire and configure dependencies


Installation

Seeren\Container is a PSR-11 container interfaces implementation

composer require seeren/container

Seeren\Container\Container

The container create, build, store and share instances

use Seeren\Container\Container;

$container = new Container();

$foo = $container->get('Dummy\Foo');

Autowiring

Dependencies are resolved using type declaration

namespace Dummy;

class Foo
{
    public function __construct(Bar $bar){}
}

class Bar 
{
    public function __construct(Baz $baz){}
}

class Baz {}

Interfaces

namespace Dummy;

class Foo {
    public function __construct(BarInterface $bar){}
}

Interfaces are resolved using configuration file by default in /config/services.json

{
  "parameters": {},
  "services": {
    "Dummy\\Foo": {
      "Dummy\\BarInterface": "Dummy\\Bar"
    }
  }
}

Include path can be specified at construction

project/
โ””โ”€ config/
   โ””โ”€ services.json

Parameters

Parameters and primitives are resolved using configuration file

namespace Dummy;

class Foo
{
    public function __construct(string $bar){}
}
{
  "parameters": {
    "message": "Hello"
  },
  "services": {
    "Dummy\\Foo": {
      "bar": ":message"
    }
  }
}

Methods

Methods can use autowiring

namespace Dummy;

class Foo
{

    public function __construct(BarInterface $bar){}

    public function action(int $id, Baz $baz)
    {
        return 'Hello';
    }

}
use Seeren\Container\Container;

$container = new Container();

$message = $container->call('Dummy\Foo', 'action', [7]);

echo $message; // Hello

License

This project is licensed under the MIT License

About

๐Ÿ›๏ธ Autowire and configure dependencies

Topics

Resources

License

Stars

Watchers

Forks

Languages