rukavishnikov/psr-container 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

rukavishnikov/psr-container

最新稳定版本:v0.2.2

Composer 安装命令:

composer require rukavishnikov/psr-container

包简介

A PSR-11 container interface implementation

README 文档

README

Features

  • PSR-11 compatible
  • Accepts array definitions
  • Supports constructor injection, property injection and method injection
  • Supports autoload of classes not specified in the container configuration
  • Supports auto-wiring
  • Supports closure
  • Detects circular references
  • Checks property types (default disable)
  • Checks method parameter types (default disable)

Requirements

  • PHP 8.0 or higher
  • PSR container 2.x

Installation

composer require rukavishnikov/psr-container

Using the container

index.php

$config = require 'config.php'; // Load config for container (see below)

$container = new Container($config); // Default used
//$container = new Container($config, true); // Strict mode used (see below)

if ($container->has(InterfaceOrClass::class)) {
    $instance = $container->get(InterfaceOrClass::class);
}

config.php

return [
    ServerRequestInterface::class => ServerRequest::class, // Simple definition
    RouterInterface::class => [ // Full definition
        'class' => Router::class, // Required

        // Constructor injection
        '__construct()' => [
            [
                '/test' => TestController::class,
            ],
        ],
    ],
    TestController::class => [ // Full definition
        'class' => TestController::class, // Required

        // Constructor injection
        '__construct()' => [
            true, // Bool
            123, // Int
            5.0, // Float
            'qwerty', // String
            [1, 2, 3], // Array
            StdClass::class, // Class
            new DateTime(), // Instance
            fn () => function (Container $container) {
                return $container->get(StdClass::class);
            }, // Closure
        ],

        // Property injection
        '$public_a' => true, // Bool
        '$public_b' => 123, // Int
        '$public_c' => 5.0, // Float
        '$public_d' => 'qwerty', // String
        '$public_e' => [1, 2, 3], // Array
        '$public_f' => StdClass::class, // Class
        '$public_g' => new DateTime(), // Instance
        '$public_h' => fn () => function (Container $container) {
            return $container->get(StdClass::class);
        }, // Closure

        // Method injection
        'setA()' => [true], // Bool
        'setB()' => [123], // Int
        'setC()' => [5.0], // Float
        'setD()' => ['qwerty'], // String
        'setE()' => [[1, 2, 3]], // Array
        'setF()' => [StdClass::class], // Class
        'setG()' => [new DateTime()], // Instance
        'setH()' => [
            fn () => function (Container $container) {
                return $container->get(StdClass::class);
            }
        ], // Closure
    ],
    ResponseInterface::class => Response::class, // Simple definition
    EmitterInterface::class => Emitter::class, // Simple definition
];

Strict mode

For use strict mode to property type checking and method parameter type checking you can enable it on container create (second param). Default value is false.

统计信息

  • 总下载量: 26
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-03-26