Skip to main content

Disco - An container-Interop compatible DI container

This blog post might be outdated!
This blog post was published more than one year ago and might be outdated!
· 2 min read
Stephan Hochdörfer
Head of IT Business Operations

For years I speak at conferences about Dependency Injection. 5 years ago at pfCongres 2010, I started my speaking career with talking about Dependency Injection in the real world. Today, 5 years later am I am finally able to push our own implementation to Github. This is a completely different version compared to what I talked about 5 years ago but still it might be useful for some of you.

Kudos to Marco Pivetta and Mike van Riel for their insane work which builds the foundation of our current implementation.

This is how you use it:

composer.phar require bitexpert/disco

First, create a configuration class. The class needs to be marked with an @Configuration annotation. Add a public method for each instance you want to manage. The method name will be later used in the has() or get() method to retrieve the instance. Each method needs the @Bean annotation as well as the @return annotation.

use bitExpert\Disco\Annotations\Bean;
use bitExpert\Disco\Annotations\Configuration;
use bitExpert\Disco\Helper\SampleService;

/**
* @Configuration
*/
class MyConfiguration
{
/**
* @Bean
* @return SampleService
*/
public function mySampleService()
{
return new SampleService();
}
}

To instantiate the \bitExpert\Disco\AnnotationBeanFactory proceed as follows:

$parameters = [];

$beanFactory = new \bitExpert\Disco\AnnotationBeanFactory(MyConfiguration::class, $parameters);
\bitExpert\Disco\BeanFactoryRegistry::register($beanFactory);

Since Disco implements the container-interop, you are able to call the has() and get() method as expected:

$beanFactory->has('mySampleService');

$beanFactory->get('mySampleService');

For more documentation check out the Disco Github repo or run the test suite :)