Skip to main content

Improving your Disco configuration

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

Last week we open-sourced our DI container called Disco (Dependency Injection Container). The general feedback on Twitter was really good, especially since we decided to go down the "Annotation" route which a lot of people do not really like (me included). Relying on a single class for the configuration seems a bit limited but traits will come in pretty handy. Let me show you what I mean.

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();
}
}

Sure, you can put all your configuration code in one class but this will soon turn into an unmanageable mess of code. By using traits we can structure our configuration code in a better way or even include configurations from other packages which turns out to be a really, really handy feature.

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

trait MySampleServiceTrait
{
/**
* @Bean
* @return SampleService
*/
public function mySampleService()
{
return new SampleService();
}
}

/**
* @Configuration
*/
class MyConfiguration
{
use MySampleServiceTrait;
}

In addition to that you can make use of all the traits goodness like conflict resolution which is really powerful, especially when you work with multiple packages that come with "their own" configuration code and you need to glue all that together. I might be biased but the more I use the container, the more I like our approach ;)