borobudur / di
Borobudur Dependency Injection Component
Installs: 595
Dependents: 2
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 0
Open Issues: 0
Type:borobudur-component
Requires
- php: >=5.4.0
Requires (Dev)
- codeclimate/php-test-reporter: dev-master
- phpunit/phpunit: ~4.0
This package is not auto-updated.
Last update: 2017-10-13 13:00:21 UTC
README
Borobudur\DependencyInjection
provides a dependency injection container system for php 5.4+
- Container parameter that can be inject to service
- Container parameter can be reference from service
- Immutable parameter
- Describe service as closure or object
- Share service as singleton
- Service definition
- Parameter, constructor, method and closure injection
- Explicit and implicit auto-resolution of typehinted on constructor, method and closure parameter values
- Inversion of Control (IoC) Container
Installation
- Get Composer
- Install Borobudur\DependencyInjection with
composer require borobudur/di
- Add composer autoload on your main PHP file:
require __DIR__.'/vendor/autoload.php';
Example
Example 1 - Basic usage
use Borobudur\DependencyInjection\Di; class Foo { public $message = 'this is foo'; } class Baz { public function meetFoo(Foo $foo) { return $foo->message.' meet baz'; } } $di = new Di(); $service = function(Foo $foo, $prefix = '') { return $prefix.$foo->message; }; $di->call($service); // Output: this is foo $di->call($service, array('prefix' => 'hello ')); // Output: hello this is foo $baz = new Baz(); $di->call(array($baz, 'meetFoo')); // Output: this is foo meet baz
Example 2 - Constructor, property and method injection with instance manager.
use Borobudur\DependencyInjection\Di; class Baz { public $message = 'this is baz'; } class Bar { public $message = 'this is bar'; } class Hello { public $greeting = 'hello'; private $baz; public function __construct(Baz $baz) { $this->baz = $baz; } public function greetingBaz() { return $this->greeting.' '.$this->baz->message; } public function greetingBar(Bar $bar) { return $this->greeting.' '.$bar->message; } } $di = new Di(); $instance = $di->createInstance('Hello'); $instance->callMethod('greetingBaz'); // Output: hello this is baz $instance->getOriginalInstance()->greetingBaz(); // Output: hello this is baz $instance->setProperty('greeting', 'hi'); $instance->callMethod('greetingBar'); // Output: hi this is bar
Example 3 - Service container
use Borobudur\DependencyInjection\Container; use Borobudur\DependencyInjection\Reference; class Counter { private $counter = 0; public function count() { ++$this->counter; } public function getCounter() { return $this->counter; } } class Response { private $counter; private $status; public function __construct(Counter $counter, $status) { $this->counter = $counter; $this->status = $status; } public function print($message) { $this->counter->count(); return sprintf('%s: %d (%s)', $message, $this->counter->getCounter(), $this->status); } } $container = new Container(); $container->getParameterBag()->add(array( 'status' => 'online', 'message' => 'Hits', 'counter' => new Reference('counter_service'), )); // Define singleton service to make counter always increment $container->share('counter_service', function() { return new Counter(); }); // Define view service $container->set('view', function(Response $response, $message) { return sprintf('Statistic - %s', $response->print($message)); }); $container->get('view'); // Output: Statistic - Hits: 1 (online) $container->get('view'); // Output: Statistic - Hits: 2 (online) $container->get('view', array('message' => 'Today %message%')); // Output: Statistic - Today Hits: 3 (online)
Example 4 - Inversion of Control (IoC)
use Borobudur\DependencyInjection\Container; use Borobudur\DependencyInjection\Definition; interface EventInterface { public function doIt(); } class TeachEvent implements EventInterface { public function doIt() { echo 'Teaching'; } } class FootballEvent implements EventInterface { public function doIt() { echo 'Playing football'; } } class College { private $event; public function __construct(EventInterface $event) { $this->event = $event; } public startEvent() { $this->event->doIt(); } } $container = new ContainerBuilder(); $container->setDefinition('teach', new AbstractDefinition('TeachEvent', 'EventInterface')); $container->setDefinition('college', new Definition('College')); $container->get('college')->startEvent(); // Output: Teaching $container->setDefinition('football', new AbstractDefinition('FootballEvent', 'EventInterface')); $container->get('college')->startEvent(); // Output: Playing football