Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/00f100/fcphp-di

Package to manage dependency injection
https://github.com/00f100/fcphp-di

dependency dependency-injection fcphp fcphp-di injection php7

Last synced: about 8 hours ago
JSON representation

Package to manage dependency injection

Awesome Lists containing this project

README

        

# FcPhp Dependency Injection

Package to manage dependencies of project.

[![Build Status](https://travis-ci.org/00F100/fcphp-di.svg?branch=master)](https://travis-ci.org/00F100/fcphp-di) [![codecov](https://codecov.io/gh/00F100/fcphp-di/branch/master/graph/badge.svg)](https://codecov.io/gh/00F100/fcphp-di) [![Total Downloads](https://poser.pugx.org/00F100/fcphp-di/downloads)](https://packagist.org/packages/00F100/fcphp-di)

## How to install

Composer:
```sh
$ composer require 00f100/fcphp-di
```

or add in composer.json
```json
{
"require": {
"00f100/fcphp-di": "*"
}
}
```

## How to use

```php
set(string $id, string $namespace, array $args = [], array $setters = [], bool $singleton = true);

/**
* Method to overwrite instance before make
*
* @param string $id Identify instance
* @param string $namespace Namespace of class
* @param array $args Args to construct class
* @param array $setters Setters to class
* @return FcPhp\Di\Interfaces\IDi
*/
$di->overwrite(string $id, string $namespace, array $args = [], array $setters = []) ;

/**
* Method to get instance of Container
*
* @param string $id Identify of instance
* @param array $args Args to construct instance
* @return FcPhp\Di\Interfaces\IContainer
*/
$di->get(string $id, array $args = [], array $setters = []);

/**
* Method to configure setters to class
*
* @param string $id Identify instance
* @param array $setters Setters to class
* @return FcPhp\Di\Interfaces\IDi
*/
$di->setter(string $id, array $setters);

/**
* Method instance of class
*
* @param string $id Identify of instance
* @param array $args Args to construct instance
* @return mixed
*/
$di->make(string $id, array $args = [], array $setters = []);
```

## Examples

```php
foo = $foo;
}
public function setAnotherFoo($foo) {
$this->anotherFoo = $foo;
}
public functio getAnotherFoo() {
return $this->anotherFoo;
}
}
class ExampleBar {
public $example;
__construct(Example $example) {
$this->example = $example;
}
}
}
*/
$di->set('Example', 'Namespace\To\Example', ['foo' => 'bar'], ['setAnotherFoo', 'AnotherBar']);
$di->set('ExampleBar', 'Namespace\To\ExampleBar', ['example' => $di->get('Example')]);

// Print "bar"
echo $di->make('ExampleBar')->example->foo

// Print "AnotherBar"
echo $di->make('ExampleBar')->example->getAnotherFoo();
```

## Events

```php
event([
'beforeSet' => function(string $id, string $namespace, array $args, array $setters, bool $singleton) {

},
'afterSet' => function(string $id, string $namespace, array $args, array $setters, bool $singleton, IInstance $instance) {

},
'beforeGet' => function(string $id, array $args, array $setters) {

},
'afterGet' => function(string $id, array $args, array $setters, IInstance $instance, IContainer $container) {

},
'beforeMake' => function(string $id, array $args, array $setters) {

},
'afterMake' => function(string $id, array $args, array $setters, IInstance $instance, IContainer $container, $class) {

}
]);
```