All Projects → reinfi → zf-dependency-injection

reinfi / zf-dependency-injection

Licence: MIT license
Advanced dependency injection for laminas framework

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to zf-dependency-injection

stefano-tree
Framework agnostic Nested Set (MPTT) implementation for PHP
Stars: ✭ 24 (+41.18%)
Mutual labels:  zend-framework, laminas
grpc-jwt-spring-boot-starter
Spring boot starter for gRPC framework with JWT authorization
Stars: ✭ 24 (+41.18%)
Mutual labels:  annotations, autowire
zend-di-config
PSR-11 PHP-DI container configurator for Laminas, Mezzio, ZF, Expressive applications or any framework that needs a PSR-11 container
Stars: ✭ 19 (+11.76%)
Mutual labels:  zend-framework, laminas
AnnotationInject
Compile-time Swift dependency injection annotations
Stars: ✭ 40 (+135.29%)
Mutual labels:  dependency-injection, annotations
ufw
A minimalist framework for rapid server side applications prototyping in C++ with dependency injection support.
Stars: ✭ 19 (+11.76%)
Mutual labels:  yaml, dependency-injection
Preferenceroom
🚚 Android processing library for managing SharedPreferences persistence efficiently and structurally.
Stars: ✭ 341 (+1905.88%)
Mutual labels:  dependency-injection, annotations
twbs-helper-module
Laminas (formerly Zend Framework) module for easy integration of Twitter Bootstrap
Stars: ✭ 18 (+5.88%)
Mutual labels:  zend-framework, laminas
Laravelyaml
Laravel ServiceProvider for using YAML configuration files
Stars: ✭ 8 (-52.94%)
Mutual labels:  yaml, dependency-injection
Ray.di
Guice style dependency injection framework for PHP
Stars: ✭ 175 (+929.41%)
Mutual labels:  dependency-injection, annotations
Hyperf
🚀 A coroutine framework that focuses on hyperspeed and flexibility. Building microservice or middleware with ease.
Stars: ✭ 4,206 (+24641.18%)
Mutual labels:  dependency-injection, annotations
Quicklib
Quick development library (AutoMapper, LinQ, IOC Dependency Injection, MemoryCache, Scheduled tasks, Config, Serializers, etc) with crossplatform support for Delphi/Firemonkey (Windows,Linux,OSX/IOS/Android) and freepascal (Windows/Linux).
Stars: ✭ 274 (+1511.76%)
Mutual labels:  yaml, dependency-injection
I18nplugin
Intellij idea i18next support plugin
Stars: ✭ 43 (+152.94%)
Mutual labels:  yaml, annotations
NoMansWallpaperApp
Looking for your next No Man's Sky wallpaper?
Stars: ✭ 35 (+105.88%)
Mutual labels:  dependency-injection
sanic-ext
Extended Sanic functionality
Stars: ✭ 26 (+52.94%)
Mutual labels:  dependency-injection
swift-di-explorations
Functional DI explorations in Swift
Stars: ✭ 28 (+64.71%)
Mutual labels:  dependency-injection
kibana-comments-app-plugin
An application plugin to add and visualize comments to your Kibana dashboards
Stars: ✭ 36 (+111.76%)
Mutual labels:  annotations
representable
Maps representation documents from and to Ruby objects. Includes JSON, XML and YAML support, plain properties and compositions.
Stars: ✭ 689 (+3952.94%)
Mutual labels:  yaml
ioc-ts
Inversion of control container on TypeScript
Stars: ✭ 14 (-17.65%)
Mutual labels:  dependency-injection
Container
🚀 PHP Service Container with fast and cachable dependency injection.
Stars: ✭ 28 (+64.71%)
Mutual labels:  dependency-injection
AvengersChat
💙 Android sample Avengers chat application using Stream Chat SDK based on MVVM (ViewModel, Coroutines, Room, Hilt, Repository) architecture.
Stars: ✭ 350 (+1958.82%)
Mutual labels:  dependency-injection

Build Status Coverage Status

Configure dependency injection in Laminas or Mezzio using annotations, yaml or autowiring.

Heavily inspired by https://github.com/mikemix/mxdiModule.

=======

  1. Installation
  2. AutoWiring
  3. Attributes
  4. Annotations
  5. YAML
  6. Caching
  7. Console commands

Installation

  1. Install with Composer: composer require reinfi/zf-dependency-injection.
  2. Enable the module via config in appliation.config.php under modules key:
    return [
        'modules' => [
            'Reinfi\DependencyInjection',
            // other modules
        ],
    ];

AutoWiring

To use autowiring for your service you need to specify the 'AutoWiringFactory' within the service manager configuration.

'service_manager' => [
    'factories' => [
        YourService::class => \Reinfi\DependencyInjection\Factory\AutoWiringFactory::class,
    ],
]

Fallback AutoWiring

If you are migrating your existing project to use zend service manager and don't want to register all autowired services by hand, you can register the abstract FallbackAutoWiringFactory factory.

Please make sure that you don't use the fallback mechanism for everything. You should try to write and register explicit factories for your services.

'service_manager' => [
    'abstract_factories' => [
        \Reinfi\DependencyInjection\AbstractFactory\FallbackAutoWiringFactory::class,
    ],
]
What can be autowired?

Every service registered within the service manager can be autowired. Plugins within the plugin manager can also be autowired. If you need to register another mapping you can simply add the following:

PluginManagerResolver::addMapping('MyInterfaceClass', 'MyPluginManager');

If your service needs the container as dependency this can also be autowired.

Add another resolver

If you like to add another resolver you can simply add one through the configuration.

'reinfi.dependencyInjection' => [
    'autowire_resolver' => [
        AnotherResolver::class,
    ],
]

It needs to implement the ResolverInterface.

Attributes

Attributes are activated if you are using a php version 8.0 or higher. To use attributes for your dependencies you need to specify the 'InjectionFactory' within the service manager configuration.

'service_manager' => [
    'factories' => [
        YourService::class => \Reinfi\DependencyInjection\Factory\InjectionFactory::class,
    ],
]

Following attributes are supported:

  • Inject (directly injects a service from the service locator)
  • InjectParent (must be used if you inject a service from a plugin manager)
  • InjectConfig (dot separated path to a config value, e.g. service_manager.factories)
  • InjectContainer (directly inject container interface)

Also in addition there a several annotations to inject from plugin managers.

  • InjectViewHelper
  • InjectFilter
  • InjectInputFilter
  • InjectValidator
  • InjectHydrator
  • InjectFormElement

You can either pass directly the required service name or if you need options you can pass them as following:

#[InjectFormElement(name="Service", options={"field": "value"}]

If you need a doctrine repository there is also an attribute.

  • InjectDoctrineRepository

It is only constructor injection supported, if you need di from setters you need to use delegator factories.

You can add the attributes at properties or at the __construct method.

#[Inject("Namespace\MyService")] 
private MyService $service;

public function __construct(MyService $service) 
{
    $this->service = $service;
}

or

private MyService $service;

#[Inject("Namespace\MyService")] 
public function __construct(MyService $service) 
{
    $this->service = $service;
}

The order is important and you should decide between constructor or property annotations.

Annotations

To use annotations for your dependencies you need to specify the 'InjectionFactory' within the service manager configuration.

'service_manager' => [
    'factories' => [
        YourService::class => \Reinfi\DependencyInjection\Factory\InjectionFactory::class,
    ],
]

Following annotations are supported:

  • Inject (directly injects a service from the service locator)
  • InjectParent (must be used if you inject a service from a plugin manager)
  • InjectConfig (dot separated path to a config value, e.g. service_manager.factories)
  • InjectContainer (directly inject container interface)

Also in addition there a several annotations to inject from plugin managers.

  • InjectViewHelper
  • InjectFilter
  • InjectInputFilter
  • InjectValidator
  • InjectHydrator
  • InjectFormElement

You can either pass directly the required service name or if you need options you can pass them as following:

@InjectFormElement(name="Service", options={"field": "value"})

If you need a doctrine repository there is also an annotation.

  • InjectDoctrineRepository

It is only constructor injection supported, if you need di from setters you need to use delegator factories.

You can add the annotations at properties or at the __construct method.

use Reinfi\DependencyInjection\Annotation\Inject;

/**
 * @Inject("Namespace\MyService")
 */
private MyService $service;

/**
 * @param MyService $service
 */
public function __construct(
    MyService $service,
) {
    $this->service = $service;
}

or

use Reinfi\DependencyInjection\Annotation\Inject;

/**
 * @Inject("Namespace\MyService")
 */
public function __construct(MyService $service) 
{
    $this->service = $service;
}

The order is important and you should decide between constructor or property annotations.

Adding own annotations

If you want to use your own annotation you just need to implement the AnnotationInterface.

YAML

You can specify your dependencies within a yaml file.

YourService:
  - {type: Inject, value: AnotherService}

To enable YAML usage you need to specify the following configuration

'reinfi.dependencyInjection' => [
    'extractor' => YamlExtractor::class,
    'extractor_options => [
        'file' => 'path_to_your_yaml_file.yml',
    ],
]

Caching

Parsing mapping sources is very heavy. You should enable the cache on production servers. You can set up caching easily with any custom or pre-existing PSR-16 cache adapter.

You can provide a string which will be resolved by the container. The factory must return a PSR-16 cache adapter.

'reinfi.dependencyInjection' => [
    'cache' => \Laminas\Cache\Storage\Adapter\Memory::class,
]

or you provide a factory for a cache adapter.

'reinfi.dependencyInjection' => [
    'cache' => function() {
       return new \Laminas\Cache\Psr\SimpleCache\SimpleCacheDecorator(
           new \Laminas\Cache\Storage\Adapter\Memory()
       );
    },
]

Console commands

  • Warmup script for Laminas: php bin/zf-dependency-injection-cache-warmup Fills the cache with every injection required by a class. This can either be via AutoWiringFactory or InjectionFactory.

FAQ

Feel free to ask any questions or open own pull requests.

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].