承接 franco2911/avresticontainer 相关项目开发

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

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

franco2911/avresticontainer

最新稳定版本:1.0.1

Composer 安装命令:

composer require franco2911/avresticontainer

包简介

Lightweight and flexible Inversion of Control (IoC) container for PHP

README 文档

README

PHP Composer

AvrestiContainer is a lightweight and flexible dependency injection (DI) container for PHP. It allows you to manage class dependencies and perform dependency injection in a simple and intuitive way.

Features

  • Simple Binding: Bind classes and interfaces to concrete implementations.
  • Singletons: Register and resolve singletons easily.
  • Method Injection: Inject dependencies into class methods.
  • Aliases: Create aliases for bindings to simplify dependency resolution.
  • Dependency Resolution: Automatically resolve and inject class dependencies.
  • Configurable Definitions: Add multiple definitions in a single call using addDefinitions.
  • Autowiring: Automatically resolve class dependencies without explicit bindings.

Installation

You can install AvrestiContainer via Composer:

composer require franco2911/avresticontainer

Usage

Basic Usage

Here's a basic example of how to use AvrestiContainer:

require __DIR__ . '/vendor/autoload.php';

use Avresticontainer\Container;

$container = Container::getInstance();

$container->bind('config', function() {
    return new class {
        public function get($key) {
            return $key;
        }
    };
});

$config = $container->make('config');
echo $config->get('example_key');  // Outputs: example_key

Using Singletons

Register and resolve singletons:

$container->singleton('config', function() {
    return new class {
        public $value = 'singleton_value';
    };
});

$config1 = $container->make('config');
$config2 = $container->make('config');

$config1->value = 'new_value';

echo $config2->value;  // Outputs: new_value

Adding Definitions

You can add multiple definitions using addDefinitions:

$container->addDefinitions([
    'config' => function() {
        return new Config(['db_host' => '127.0.0.1', 'db_name' => 'my_database']);
    },
    'database' => function($container) {
        return new DatabaseConnection($container->make('config'));
    },
    'simple.value' => 'This is a simple value'
]);

$config = $container->make('config');
$database = $container->make('database');
$simpleValue = $container->make('simple.value');

Using Aliases

Create and use aliases for bindings:

$container->bind('original', function() {
    return new class {
        public function getValue() {
            return 'original value';
        }
    };
});

$container->alias('original', 'alias');

$original = $container->make('alias');
echo $original->getValue();  // Outputs: original value

Autowiring

AvrestiContainer can automatically resolve class dependencies:

class DependencyA {
    public function getValue() {
        return 'valueA';
    }
}

class DependencyB {
    protected $dependencyA;

    public function __construct(DependencyA $dependencyA) {
        $this->dependencyA = $dependencyA;
    }

    public function getDependencyValue() {
        return $this->dependencyA->getValue();
    }
}

$container->bind(DependencyA::class);
$container->bind(DependencyB::class);

$dependencyB = $container->make(DependencyB::class);
echo $dependencyB->getDependencyValue();  // Outputs: valueA

License

This library is licensed under the MIT License. See the LICENSE file for details.

Contributing

We welcome contributions! Please feel free to submit a pull request or open an issue on GitHub.

Acknowledgements

Inspired by various PHP DI containers, AvrestiContainer aims to be a simple yet powerful tool for managing dependencies in PHP applications.

Contact

For any questions or inquiries, please create an issue on GitHub.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-06-25

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固