All Projects → slince → di

slince / di

Licence: other
🐑 A flexible dependency injection container; It is an implementation of PSR-11

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to di

Reflex
Minimal dependency injection framework for Unity
Stars: ✭ 263 (+1215%)
Mutual labels:  container, injection, ioc-container, di
CNeptune
CNeptune improve productivity & efficiency by urbanize .net module with meta-code to lay foundation for frameworks
Stars: ✭ 30 (+50%)
Mutual labels:  container, injection, di
Container Ioc
Inversion of Control container & Dependency Injection for Javascript and Node.js apps powered by Typescript.
Stars: ✭ 89 (+345%)
Mutual labels:  container, injection, di
vesselize
⛵ A JavaScript IoC container that works seamlessly with Vue.js and React.
Stars: ✭ 22 (+10%)
Mutual labels:  injection, ioc-container, di
React Ioc
Hierarchical Dependency Injection with new React 16 Context API
Stars: ✭ 133 (+565%)
Mutual labels:  injection, ioc-container
Poodinis
A dependency injection framework for D with support for autowiring.
Stars: ✭ 57 (+185%)
Mutual labels:  injection, ioc-container
typeioc
Dependency injection container for typescript / javascript
Stars: ✭ 32 (+60%)
Mutual labels:  container, injection
Typhoon
Powerful dependency injection for Objective-C ✨✨ (https://PILGRIM.PH is the pure Swift successor to Typhoon!!)✨✨
Stars: ✭ 2,711 (+13455%)
Mutual labels:  ioc-container, di
laravel-auto-binder
Bind interfaces to concrete classes automatically
Stars: ✭ 67 (+235%)
Mutual labels:  container, ioc-container
Di
Dependency injection container in go (golang)
Stars: ✭ 390 (+1850%)
Mutual labels:  container, di
Di
psr/container implementation for humans
Stars: ✭ 69 (+245%)
Mutual labels:  container, di
Di
PSR-11 compatible DI container and injector
Stars: ✭ 141 (+605%)
Mutual labels:  container, di
Kangaru
🦘 A dependency injection container for C++11, C++14 and later
Stars: ✭ 297 (+1385%)
Mutual labels:  injection, ioc-container
DependencyInjector
Lightweight dependency injector
Stars: ✭ 30 (+50%)
Mutual labels:  injection, ioc-container
waiter
Dependency injection, Inversion of control container for rust with compile time binding.
Stars: ✭ 71 (+255%)
Mutual labels:  container, di
Container
A lightweight yet powerful IoC container for Go projects
Stars: ✭ 160 (+700%)
Mutual labels:  container, ioc-container
di
🛠 A full-featured dependency injection container for go programming language.
Stars: ✭ 156 (+680%)
Mutual labels:  ioc-container, di
Singularity
A extremely fast ioc container for high performance applications
Stars: ✭ 63 (+215%)
Mutual labels:  ioc-container, di
Mydi
moved to https://github.com/cekta/di
Stars: ✭ 21 (+5%)
Mutual labels:  container, di
Hiboot
hiboot is a high performance web and cli application framework with dependency injection support
Stars: ✭ 150 (+650%)
Mutual labels:  container, di

Dependency Injection Container

Build Status Coverage Status Total Downloads Latest Stable Version Scrutinizer

This package is a flexible IOC container for PHP with a focus on being lightweight and fast as well as requiring as little configuration as possible. It is an implementation of PSR-11

Installation

Install via composer.

{
    "require": {
        "slince/di": "^3.0"
    }
}

Alternatively, require package use composer cli:

composer require slince/di ^3.0

Usage

Container is dependency injection container. It allows you to implement the dependency injection design pattern meaning that you can decouple your class dependencies and have the container inject them where they are needed.

namespace Acme;

class Foo
{
   /**
     * @var \Acme\Bar
     */
    public $bar;

    /**
     * Construct.
     */
    public function __construct(Bar $bar)
    {
        $this->bar = $bar;
    }
}

class Bar
{
    public $foo;
    public $baz;
    
    public function __construct($foo, $baz)
    {
        $this->foo = $foo;
        $this->baz = $baz;
    }
}

$container = new Slince\Di\Container();

$container->register(Acme\Foo::class);
$foo = $container->get(Acme\Foo::class);

var_dump($foo instanceof Acme\Foo);      // true
var_dump($foo->bar instanceof Acme\Bar); // true

Make Service References

$container->register('bar', Acme\Bar::class);
$container->register('foo', Acme\Foo::class)
    ->addArgument(new Slince\Di\Reference('bar')); //refer to 'bar'
    
var_dump($container->get('bar') === $container->get('foo')->bar));    // true

Use a Factory to Create Services

Suppose you have a factory that configures and returns a new NewsletterManager object by calling the static createNewsletterManager() method:

class NewsletterManagerStaticFactory
{
    public static function createNewsletterManager($parameter)
    {
        $newsletterManager = new NewsletterManager($parameter);

        // ...

        return $newsletterManager;
    }
}
// call the static method
$container->register(
    NewsletterManager::class, 
    array(NewsletterManagerStaticFactory::class, 'createNewsletterManager')
)->addArgument('foo');

If your factory is not using a static function to configure and create your service, but a regular method, you can instantiate the factory itself as a service too.

// call a method on the specified factory service
$container->register(NewsletterManager::class, [
    new Reference(NewsletterManagerFactory::class),
    'createNewsletterManager'
]);

Create Service Aliases

$container->register(Acme\Foo::class);
$container->setAlias('foo-alias', Acme\Foo::class);
$foo = $container->get('foo-alias');

var_dump($foo instanceof Acme\Foo);      // true

Configure container

  • Singleton
$container->setDefaults([
    'share' => false
]);
$container->register('foo', Acme\Foo::class);
var_dump($container->get('foo') === $container->get('foo'));      // false
  • Autowiring
$container->setDefaults([
    'autowire' => false,
]);
$container->register('foo', Acme\Foo::class)
    ->addArgument(new Acme\Bar());  // You have to provide $bar
    
var_dump($container->get('foo') instanceof Acme\Foo::class);  // true

Container Parameters

$container->setParameters([
    'foo' => 'hello',
    'bar' => [
        'baz' => 'world'
    ]
]);

$container->register('bar', Acme\Bar::class)
     ->setArguments([
        'foo' => $container->getParameter('foo'),
        'baz' => $container->getParameter('bar.baz')
    ]);

$bar = $container->get('bar');
var_dump($bar->foo);  // hello
var_dump($bar->bar); // world

Work with Service Tags

$container->register('foo')->addTag('my.tag', array('hello' => 'world'));

$serviceIds = $container->findTaggedServiceIds('my.tag');

foreach ($serviceIds as $serviceId => $tags) {
    foreach ($tags as $tag) {
        echo $tag['hello'];
    }
}

License

The MIT license. See MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].