bitexpert / disco
Dependency Injection Container
Installs: 1 756
Dependents: 3
Suggesters: 0
Security: 0
Stars: 139
Watchers: 16
Forks: 17
Open Issues: 10
Requires
- php: ^7.2.0
- bitexpert/slf4psrlog: ^0.1.3
- doctrine/annotations: ^1.3.0
- ocramius/proxy-manager: ^2.2.0
- psr/container: ^1.0.0
Requires (Dev)
- bitexpert/phing-securitychecker: ^0.3.0
- bookdown/bookdown: @dev
- mikey179/vfsstream: ^1.6
- monolog/monolog: ^1.22.1
- phing/phing: ^2.16.0
- phpbench/phpbench: ^0.13.0
- phpdocumentor/phpdocumentor: v2.9.0
- phpunit/phpunit: ^6.1.3
- squizlabs/php_codesniffer: ^2.9.00
Provides
- dev-master
- v0.10.0
- v0.9.0
- v0.8.0
- v0.7.0
- v0.6.3
- v0.6.2
- v0.6.1
- v0.6.0
- v0.5.0
- v0.4.0
- 0.3.0
- 0.2.2
- 0.2.1
- 0.2.0
- 0.1.2
- 0.1.1
- 0.1.0
- dev-release/r0.9.0
- dev-review-docs
- dev-release/r0.8.0
- dev-release/r0.7.0
- dev-release/r0.6.0
- dev-release/r0.5.0
- dev-release/r0.4.0
- dev-release/r0.3.0
- dev-release/r0.2.0
- dev-release/r0.1.0
This package is auto-updated.
Last update: 2024-11-06 10:20:17 UTC
README
This package provides a PSR-11 compatible, annotation-based dependency injection container. Have a look at the disco-demos project to find out how to use Disco.
Installation
The preferred way of installing bitexpert/disco
is through Composer.
You can add bitexpert/disco
as a dependency, as follows:
composer.phar require bitexpert/disco
Usage
To instantiate Disco use the following code in your bootstrapping logic.
Create an instance of the \bitExpert\Disco\AnnotationBeanFactory
and register the instance with the \bitExpert\Disco\BeanFactoryRegistry
.
The second step is important as Disco needs to grab the active container instance in a few locations where it does not have access to the container instance itself.
<?php $beanFactory = new \bitExpert\Disco\AnnotationBeanFactory(MyConfiguration::class); \bitExpert\Disco\BeanFactoryRegistry::register($beanFactory);
Next up you need to create a configuration class MyConfiguration
and document it with a @Configuration
annotation.
<?php use bitExpert\Disco\Annotations\Configuration; /** * @Configuration */ class MyConfiguration { }
To declare a configuration entry, 1) add a method to your MyConfiguration
class, and 2) annotate the method with the @Bean
annotation.
Doing this registers the instance with Disco and uses the type specified by the method’s return value. The primary identifier is the method name:
<?php use bitExpert\Disco\Annotations\Bean; use bitExpert\Disco\Annotations\Configuration; use bitExpert\Disco\Helper\SampleService; /** * @Configuration */ class MyConfiguration { /** * @Bean */ public function mySampleService() : SampleService { return new SampleService(); } }
To let Disco return the entry with the id mySampleService
call the get()
method of \bitExpert\Disco\AnnotationBeanFactory
, as follows:
<?php $beanFactory->get('mySampleService');
Documentation
Documentation is in the docs tree, and can be compiled using bookdown.
$ php ./vendor/bin/bookdown docs/bookdown.json $ php -S 0.0.0.0:8080 -t docs/html/
Then point your browser to http://localhost:8080/
Contribute
Please feel free to fork and extend existing or add new features and send a pull request with your changes! To establish a consistent code quality, please provide unit tests for all your changes and adapt the documentation.
Want To Contribute?
If you feel that you have something to share, then we’d love to have you. Check out the contributing guide to find out how, as well as what we expect from you.
Resources
- sitepoint.com: Disco with Design Patterns: A Fresh Look at Dependency Injection
- php[architect]: Education Station: Your Dependency Injection Needs a Disco
- Presentation: Disco - A Fresh Look at DI at PHPUGFFM V/2016
License
Disco is released under the Apache 2.0 license.