Skip to content
⚡️ Instance Container Implementation.
Branch: develop
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
tests
.coveralls.yml
.gitattributes
.gitignore
.travis.yml
LICENSE.md
README.md
composer.json
composer.lock
phpcs.xml
pre-commit

README.md

Webino Instance Container

Instance Container implementation.

Build Status Coverage Status Code Quality Latest Stable Version

Setup

PHP from Packagist

composer require webino\instance-container

Quick Use

Getting same class instance:

use Webino\InstanceContainer;

class TestInstance
{
}

$instances = new InstanceContainer;

$testInstance = $instances->get(TestInstance::class);

Creating new class instance:

use Webino\InstanceContainer;

class TestInstance
{
    public function __construct(stdClass $dependency)
    {
    }
}

$instances = new InstanceContainer;

$testInstance = $instances->create(TestInstance::class, new stdClass);

Getting class instance with a factory method:

use Webino\InstanceContainer;
use Webino\CreateInstanceEventInterface;

class TestInstance
{
    public static function create(CreateInstanceEventInterface $event)
    {
        $container = $event->getContainer();
        return new static($container->get(stdClass::class));
    }
    
    public function __construct(stdClass $dependency)
    {    
    }
}

$instances = new InstanceContainer;

$testInstance = $instances->get(TestInstance::class);

Setting class instance:

use Webino\InstanceContainer;

class TestInstance
{
}

$instances = new InstanceContainer;

$instances->set(TestInstance::class, new TestInstance);

Unsetting class instance:

use Webino\InstanceContainer;

class TestInstance
{
}

$instances = new InstanceContainer;

$instances->set(TestInstance::class, null);

Binding factory class:

use Webino\InstanceContainer;
use Webino\CreateInstanceEventInterface;

class TestInstanceFactory implements InstanceFactoryInterface
{
    public function createInstance(CreateInstanceEventInterface $event)
    {
        $container = $event->getContainer();
        return new TestInstance($container->get(stdClass::class));
    }
}

class TestInstance
{
    public function __construct(stdClass $dependency)
    {    
    }
}

$instances = new InstanceContainer;

$instances->bind(TestInstance::class, TestInstanceFactory::class);

$testInstance = $instances->get(TestInstance::class);

Binding callback factory:

use Webino\InstanceContainer;
use Webino\CreateInstanceEventInterface;

class TestInstance
{
    public function __construct(stdClass $dependency)
    {    
    }
}

$instances = new InstanceContainer;

$instances->bind(TestInstance::class, function (CreateInstanceEventInterface $event) {
    $container = $event->getContainer();
    return new TestInstance($container->get(stdClass::class));
});

$testInstance = $instances->get(TestInstance::class);

Getting interface default class instance:

use Webino\InstanceContainer;

interface TestInstanceInterface
{
}

class TestInstance implements TestInstanceInterface
{
    public function __construct(stdClass $dependency)
    {    
    }
}

$instances = new InstanceContainer;

$testInstance = $instances->get(TestInstanceInterface::class);

Setting alias interface to a class:

use Webino\InstanceContainer;

interface TestAliasInterface
{
}

class TestInstance implements TestAliasInterface
{
    public function __construct(stdClass $dependency)
    {    
    }
}

$instances = new InstanceContainer;

$instances->alias(TestInstance::class, TestAliasInterface::class);

$testInstance = $instances->get(TestAliasInterface::class);

Instance creation event handling:

use Webino\InstanceContainer;
use Webino\CreateInstanceEvent;

class TestInstance
{
    public $foo = false;
}

$instances = new InstanceContainer;

$instances->on(CreateInstanceEvent::class, function (CreateInstanceEvent $event) {
    $instance = $event->getInstance();
    $instance->foo = true;
});

$testInstance = $instances->get(TestAliasInterface::class);

Development

Build Status Coverage Status Code Quality Latest Unstable Version

Static analysis:

composer analyse

Coding style check:

composer check

Coding style fix:

composer fix

Testing:

composer test

Git pre-commit setup:

ln -s ../../pre-commit .git/hooks/pre-commit

Addendum

License Total Downloads GitHub code size in bytes

Please, if you are interested in this library report any issues and don't hesitate to contribute. We will appreciate any contributions on development of this library.

GitHub issues GitHub forks

You can’t perform that action at this time.