承接 top-cat-software/tcsf-di 相关项目开发

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

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

top-cat-software/tcsf-di

最新稳定版本:0.1.1

Composer 安装命令:

composer require top-cat-software/tcsf-di

包简介

Top Cat Software Framework Dependency Injection module

README 文档

README

A lightweight, powerful dependency injection container for PHP 8.4+.

Features

  • Simple, intuitive API
  • Autowiring of dependencies
  • Support for singleton instances
  • Factory bindings
  • Interface to implementation binding
  • PSR-4 compliant

Installation

composer require top-cat-software/tcsf-di

Basic Usage

use TCSF\DI\Container;

// Get the container instance
$container = Container::instance();

// Create an instance with autowiring
$myService = $container->create(MyService::class);

Binding Interfaces to Implementations

use TCSF\DI\Container;

// Bind an interface to a concrete implementation
$container = Container::instance();
$container->bind(MyInterface::class, MyImplementation::class);

// Now you can create instances of the interface
$instance = $container->create(MyInterface::class);
// $instance will be an instance of MyImplementation

Singleton Bindings

use TCSF\DI\Container;

// Bind a class as a singleton
$container = Container::instance();
$container->singleton(MyService::class, MyService::class);

// Get the same instance each time
$instance1 = $container->create(MyService::class);
$instance2 = $container->create(MyService::class);
// $instance1 === $instance2

Factory Bindings

use TCSF\DI\Container;

// Bind a class to a factory function
$container = Container::instance();
$container->bind(MyService::class, function($container) {
    return new MyService('custom', 'parameters');
});

// Create an instance using the factory
$instance = $container->create(MyService::class);

Registering Existing Instances

use TCSF\DI\Container;

// Create an instance manually
$myService = new MyService('custom', 'parameters');

// Register it with the container
$container = Container::instance();
$container->registerInstance(MyService::class, $myService);

// Get the same instance back
$instance = $container->create(MyService::class);
// $instance === $myService

Autowiring

The container automatically resolves dependencies by type-hinting:

class UserRepository
{
    // Implementation...
}

class UserService
{
    private UserRepository $repository;

    public function __construct(UserRepository $repository)
    {
        $this->repository = $repository;
    }
}

// The container will automatically create a UserRepository when creating a UserService
$userService = $container->create(UserService::class);

License

MIT

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-06-08