承接 takaram/nano-di 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

takaram/nano-di

最新稳定版本:0.0.1

Composer 安装命令:

composer require takaram/nano-di

包简介

Lightweight DI Container

README 文档

README

takaram/nano-di is a tiny PSR-11 dependency injection container for PHP.

It is intentionally small: no configuration DSL, no attributes, no service providers, and no compile step. In return, it gives you a lightweight container that can resolve ordinary constructor-injected objects with very little setup.

Installation

composer require takaram/nano-di

Requirements

  • PHP 8.3 or later
  • psr/container 2.0

Quick Start

<?php

declare(strict_types=1);

use App\Repository\UserRepository;
use Psr\Container\ContainerInterface;
use Takaram\NanoDi\Container;

final class UserService
{
    public function __construct(
        private UserRepository $users,
    ) {}
}

$container = new Container();

$service = $container->get(UserService::class);

var_dump($service instanceof UserService); // true
var_dump($container instanceof ContainerInterface); // true

When a constructor parameter has a concrete class type, or a mapped interface type, nano-di resolves that dependency recursively.

Interface Mappings

Pass a simple map to bind an interface or abstract service id to a concrete class:

<?php

declare(strict_types=1);

use App\Contract\Mailer;
use App\Service\SmtpMailer;
use App\Service\UserNotifier;
use Takaram\NanoDi\Container;

$container = new Container([
    Mailer::class => SmtpMailer::class,
]);

$notifier = $container->get(UserNotifier::class);

In this example, if UserNotifier depends on Mailer, nano-di will instantiate SmtpMailer.

What It Does

  • Implements Psr\Container\ContainerInterface
  • Resolves concrete classes by class name
  • Resolves constructor dependencies recursively
  • Supports interface-to-class mappings through a plain array
  • Reuses resolved objects for subsequent get() calls
  • Uses default values for builtin constructor parameters when available
  • Throws PSR-11 compatible exceptions for missing entries and container failures

What It Leaves Out

nano-di is built for small applications, tests, CLIs, examples, and libraries that only need a minimal container. The current API deliberately leaves out larger container features such as:

  • Factory callbacks
  • Runtime parameters
  • Attributes or annotations
  • Autowiring rules beyond constructor type hints
  • Service scopes
  • Lazy proxies
  • Compilation or caching layers

If your project needs those features, a full-featured DI container may be a better fit. If you only need constructor autowiring and a few interface mappings, nano-di keeps the moving parts low.

Development

Install dependencies:

composer install

Run the test suite and checks:

composer test
composer phpstan
composer cs:check

License

MIT

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-12

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固