DI Container (PSR-11)

Overview

My DI Container

Latest Stable Version Total Downloads License PHP Version Require codecov

It's my own implementation PSR-11 Container Interface.

Installation

composer require scruwi/container

Init

$container = new Container([
    new ClassResolver(['App\\Namespace1', 'App\\Namespace2']),
    new ParameterResolver(['param1' => true, 'param2' => 'string']),
    new DefinitionResolver([Definition1:class, Definition2:class]),
]);

Resolvers

You can implement your resolvers in a project if you need them. Look at the Interfaces namespace.

ClassResolver

Only classes from defined namespaces can be autowired.

ParameterResolver

Typical usage:

$rootParameters = ['param1' => true, 'param2' => 'string'];
$classSpecificParameters = [SpecificClass::class => ['param1' => false]];
$resolver = ParameterResolver($rootParameters, $classSpecificParameters);

Expected behaviour:

class SomeClass
{
    public function __construct(bool $param1) { /* $param1 === true */ }
}
class SpecificClass
{
    public function __construct(bool $param1) { /* $param1 === false */ }
}

Parameter with default value does not have to be resolved by the config:

class SpecificClass
{
    public function __construct(bool $param0 = 'default') { /* $param0 === 'default' */ }
}

You can create a specific resolver in your way and use it in your project. Just implement ParameterResolverInterface and specify it during container construct.

DefinitionResolver

Attach definitions to container.

Definition, there is no more than a factory that creates an object in a specific way. There is one specific ReflectionClassDefinition that constructs classes by their reflections. All autowired classes use that factory to create their object.

You should create as many definitions in your project as you need and specify them during container construct or attach them later by $container->addDefinition() method. Any definition must implement DefinitionInterface.

Typical definition class looks like this:

class ExampleDefinition implements DefinitionInterface
{
    public static function getId(): string
    {
        return ExampleClass::class;
    }

    public function __invoke(Container $container, ParameterResolver $resolver, BuildContext $context): object
    {
        return new ExampleClass();
    }
}

You can fetch some default parameters from the ParameterResolver:

    public function __invoke(Container $container, ParameterResolver $resolver, BuildContext $context): object
    {
        $param1 = $resolver->resolve('param1', ExampleClass::class);
        return new ExampleClass($param1);
    }

Also, you can find out from which context it was called. It may be useful for autowire interfaces:

class InterfaceDefinition implements DefinitionInterface
{
    public function getId(): string
    {
        return SomeInterface::class;
    }

    public function __invoke(Container $container, ParameterResolver $resolver, BuildContext $context): object
    {
        if ($context->getTargetClass() === ClassWithInterfaceDependency::class) {
            return new SomeClassImplementsInterface2();
        }

        return new SomeClassImplementsInterface1();
    }
}

Exceptions

  • NotFoundExceptionInterface - for an unknown entry
  • ContainerExceptionInterface - for all common exceptions in the container
  • CircularReferencesException - there is a special exception for circular references in the container
You might also like...
A small, modern, PSR-7 compatible PSR-17 and PSR-18 network library for PHP, inspired by Go's net package.

Net A small, modern, PSR-7 compatible PSR-17 and PSR-18 network library for PHP, inspired by Go's net package. Features: No hard dependencies; Favours

PSR-11 compatible Dependency Injection Container for PHP.

bitexpert/disco This package provides a PSR-11 compatible, annotation-based dependency injection container. Have a look at the disco-demos project to

DI Container (PSR-11)

My DI Container It's my own implementation PSR-11 Container Interface. Installation composer require scruwi/container Init $container = new Container(

This repository holds all interfaces related to PSR-11 (Container Interface).

Container interface This repository holds all interfaces related to PSR-11 (Container Interface). Note that this is not a Container implementation of

Middleware for PHP built on top of PSR-7 and PSR-15

zend-stratigility Repository abandoned 2019-12-31 This repository has moved to laminas/laminas-stratigility. From "Strata", Latin for "layer", and "ag

Fast PSR-7 based routing and dispatch component including PSR-15 middleware, built on top of FastRoute.

Route This package is compliant with PSR-1, PSR-2, PSR-4, PSR-7, PSR-11 and PSR-15. If you notice compliance oversights, please send a patch via pull

:tada: Release 2.0 is released! Very fast HTTP router for PHP 7.1+ (incl. PHP8 with attributes) based on PSR-7 and PSR-15 with support for annotations and OpenApi (Swagger)

HTTP router for PHP 7.1+ (incl. PHP 8 with attributes) based on PSR-7 and PSR-15 with support for annotations and OpenApi (Swagger) Installation compo

PSR-6 cache implementation adapting a given PSR-16 instance

PSR-6 cache implementation adapting PSR-16 This package provides a PSR-6 cache instance when you only have a PSR-16 cache at hand. As PSR-6 is more fe

PSR Log - This repository holds all interfaces/classes/traits related to PSR-3.

PSR Log This repository holds all interfaces/classes/traits related to PSR-3. Note that this is not a logger of its own. It is merely an interface tha

PSR-7 and PSR-15 JWT Authentication Middleware

PSR-7 and PSR-15 JWT Authentication Middleware This middleware implements JSON Web Token Authentication. It was originally developed for Slim but can

PSR-7 and PSR-15 HTTP Basic Authentication Middleware

PSR-7 and PSR-15 Basic Auth Middleware This middleware implements HTTP Basic Authentication. It was originally developed for Slim but can be used with

Testing utilities for the psr/log package that backs the PSR-3 specification.

FIG - Log Test Testing utilities for the psr/log package that backs the PSR-3 specification. Psr\Log\Test\LoggerInterfaceTest provides a base test cla

A small PHP dependency injection container

Pimple Caution! Pimple is now closed for changes. No new features will be added and no cosmetic changes will be accepted either. The only accepted cha

The dependency injection container for humans
The dependency injection container for humans

layout home PHP-DI is a dependency injection container meant to be practical, powerful, and framework-agnostic. Read more on the website: php-di.org G

Small but powerful dependency injection container

Container (Dependency Injection) This package is compliant with PSR-1, PSR-2, PSR-4 and PSR-11. If you notice compliance oversights, please send a pat

💎 Flexible, compiled and full-featured Dependency Injection Container with perfectly usable autowiring and support for all new PHP 7 features.

Nette Dependency Injection (DI) Introduction Purpose of the Dependecy Injection (DI) is to free classes from the responsibility for obtaining objects

🚀 PHP Service Container with fast and cachable dependency injection.
🚀 PHP Service Container with fast and cachable dependency injection.

ClanCats Container A PHP Service Container featuring a simple meta-language with fast and compilable dependency injection. Requires PHP = 7.0 Pros: M

An example of Laravel app that runs in a docker container with RoadRunner
An example of Laravel app that runs in a docker container with RoadRunner

This repository contains an example of Laravel (PHP Framework) application that runs in a docker container with RoadRunner (high-performance PHP appli

Nuber is an open source container management platform it provides a front end to manage your own cloud infrastructure, using Linux Containers virtualization technology

Nuber is an open source container management platform it provides a front end to manage your own cloud infrastructure, using Linux Containers virtualization technology

Owner
null
This repository holds all interfaces related to PSR-11 (Container Interface).

Container interface This repository holds all interfaces related to PSR-11 (Container Interface). Note that this is not a Container implementation of

PHP-FIG 9.6k Jan 4, 2023
A small PHP dependency injection container

Pimple Caution! Pimple is now closed for changes. No new features will be added and no cosmetic changes will be accepted either. The only accepted cha

Silex 2.6k Dec 29, 2022
The dependency injection container for humans

layout home PHP-DI is a dependency injection container meant to be practical, powerful, and framework-agnostic. Read more on the website: php-di.org G

null 2.4k Jan 4, 2023
Small but powerful dependency injection container

Container (Dependency Injection) This package is compliant with PSR-1, PSR-2, PSR-4 and PSR-11. If you notice compliance oversights, please send a pat

The League of Extraordinary Packages 779 Dec 30, 2022
💎 Flexible, compiled and full-featured Dependency Injection Container with perfectly usable autowiring and support for all new PHP 7 features.

Nette Dependency Injection (DI) Introduction Purpose of the Dependecy Injection (DI) is to free classes from the responsibility for obtaining objects

Nette Foundation 781 Dec 15, 2022
🚀 PHP Service Container with fast and cachable dependency injection.

ClanCats Container A PHP Service Container featuring a simple meta-language with fast and compilable dependency injection. Requires PHP >= 7.0 Pros: M

ClanCats 28 Apr 13, 2022
Twittee is the smallest, and still useful, Dependency Injection Container in PHP

What is Twittee? Twittee is the smallest, and still useful, Dependency Injection Container in PHP; it is also probably one of the first public softwar

null 133 Dec 5, 2022
Extensible DI container for Symfony2

This package contains an implementation of the Symfony 2 DI container that can be extended using other DI containers (from other frameworks).

TheCodingMachine 1 Apr 2, 2014
Yii Dependency Injection PSR-11 compatible

Yii Dependency Injection PSR-11 compatible dependency injection container that is able to instantiate and configure classes resolving dependencies. Fe

Yii Software 161 Nov 10, 2022
A small, modern, PSR-7 compatible PSR-17 and PSR-18 network library for PHP, inspired by Go's net package.

Net A small, modern, PSR-7 compatible PSR-17 and PSR-18 network library for PHP, inspired by Go's net package. Features: No hard dependencies; Favours

Minibase 16 Jun 7, 2022