All Projects → yiisoft → injector

yiisoft / injector

Licence: BSD-3-Clause License
PSR-11 compatible injector

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to injector

inject
[Archived] See https://github.com/goava/di.
Stars: ✭ 49 (+48.48%)
Mutual labels:  dependency-injection, injector
SocketHook
Socket hook is an injector based on EasyHook (win only) which redirect the traffic to your local server.
Stars: ✭ 38 (+15.15%)
Mutual labels:  dependency-injection, injector
linker
Dependency Injection and Inversion of Control package
Stars: ✭ 33 (+0%)
Mutual labels:  dependency-injection, injector
dodrugs
A macro-powered dependency injector for Haxe
Stars: ✭ 29 (-12.12%)
Mutual labels:  dependency-injection, injector
DependencyInjector
Lightweight dependency injector
Stars: ✭ 30 (-9.09%)
Mutual labels:  dependency-injection, injector
wedi
[Deprecated] A lightweight dependency injection (DI) library for TypeScript, along with a binding for React.
Stars: ✭ 22 (-33.33%)
Mutual labels:  dependency-injection, injector
common-injector
Heavily influenced by Angular and it's dependency injection. Inspired by Angular and Indiv.
Stars: ✭ 18 (-45.45%)
Mutual labels:  dependency-injection, injector
MVVM-Sample
Swift MVVM Sample project. Made with ReactiveCocoa, Swinject and Routers
Stars: ✭ 21 (-36.36%)
Mutual labels:  dependency-injection
aspnet-mvc5-starter-template
Asp.Net MVC 5 Starter Kit is a S.O.L.I.D, clean and globalized template with all the necessary boilerplate, ready to go.
Stars: ✭ 39 (+18.18%)
Mutual labels:  dependency-injection
fusion
A simple automated dependency injection library for TypeScript, supporting React class and functional components.
Stars: ✭ 18 (-45.45%)
Mutual labels:  dependency-injection
koin.dart
A pragmatic lightweight dependency injection library. This is a port of Koin for Dart projects.
Stars: ✭ 50 (+51.52%)
Mutual labels:  dependency-injection
injector io
InjectorIO - Dependency Injection for Flutter
Stars: ✭ 73 (+121.21%)
Mutual labels:  injector
Hangfire.StructureMap
Hangfire background job activator based on the StructureMap IoC container
Stars: ✭ 16 (-51.52%)
Mutual labels:  dependency-injection
google-guice-tutorials
4 part tutorial series on Google Guice
Stars: ✭ 28 (-15.15%)
Mutual labels:  dependency-injection
dingo
Generated dependency injection containers in go (golang)
Stars: ✭ 75 (+127.27%)
Mutual labels:  dependency-injection
files
Useful methods to manage files and directories
Stars: ✭ 27 (-18.18%)
Mutual labels:  yii3
csharp-libs
This repository contains my own C# libraries which I use for different projects
Stars: ✭ 25 (-24.24%)
Mutual labels:  dependency-injection
hs-di
Haskell Dependency Injection
Stars: ✭ 14 (-57.58%)
Mutual labels:  dependency-injection
yii-gii
Yii code generator extension
Stars: ✭ 27 (-18.18%)
Mutual labels:  yii3
saber
Dependency injection (DI) & Inversion of Control (IoC) command line tool for Swift based on code generation
Stars: ✭ 21 (-36.36%)
Mutual labels:  dependency-injection

Yii Injector


Latest Stable Version Total Downloads Build status Code Coverage Scrutinizer Code Quality Mutation testing badge static analysis type-coverage

A dependency injection implementation based on autowiring and PSR-11 compatible dependency injection containers.

Features

  • Injects dependencies when calling functions and creating objects
  • Works with any dependency injection container (DIC) that is PSR-11 compatible
  • Accepts additional dependencies and arguments passed as array
  • Allows passing arguments by parameter name in the array
  • Resolves object type dependencies from the container and the passed array by parameter type declaration
  • Resolves variadic arguments i.e. function (MyClass ...$a)

Requirements

  • PHP 7.4 or higher.

Installation

The package could be installed with composer:

composer require yiisoft/injector --prefer-dist

About

Injector can automatically resolve and inject dependencies when calling functions and creating objects.

It therefore uses Reflection to analyze the parameters of the function to call, or the constructor of the class to instantiate and then tries to resolve all arguments by several strategies.

The main purpose is to find dependency objects - that is arguments of type object that are declared with a classname or an interface - in a (mandatory) PSR-11 compatible dependency injection container (DIC). The container must therefore use the class or interface name as ID.

In addition, an array with arguments can be passed that will also be scanned for matching dependencies. To make things really flexible (and not limited to objects), arguments in that array can optionally use a function parameter name as key. This way basically any callable can be invoked and any object be instantiated by the Injector even if it uses a mix of object dependencies and arguments of other types.

Basic Example

// A function to call
$fn = function (\App\Foo $a, \App\Bar $b, int $c) { /* ... */ };

// Arbitrary PSR-11 compatible object container
$container = new \some\di\Container([
    'App\Foo' => new Foo(), // will be used as $a
]);

// Prepare the injector
$injector = new Injector($container);

// Use the injector to call the function and resolve dependencies
$result = $injector->invoke($fn, [
    'c' => 15,  // will be used as $c
    new Bar(),  // will be used as $b
]);

Documentation

Testing

Unit testing

The package is tested with PHPUnit. To run tests:

./vendor/bin/phpunit

Mutation testing

The package tests are checked with Infection mutation framework with Infection Static Analysis Plugin. To run it:

./vendor/bin/roave-infection-static-analysis-plugin

Static analysis

The code is statically analyzed with Psalm. To run static analysis:

./vendor/bin/psalm

License

The Yii Injector is free software. It is released under the terms of the BSD License. Please see LICENSE for more information.

Maintained by Yii Software.

Support the project

Open Collective

Follow updates

Official website Twitter Telegram Facebook Slack

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].