定制 assodepicche/php-di-container 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

assodepicche/php-di-container

Composer 安装命令:

composer require assodepicche/php-di-container

包简介

A PHP Dependency Injection Container

README 文档

README

According to DotNetTricks, a DI Container is a framework to create dependencies and inject them automatically when required. It automatically creates objects based on the request and injects them when required. DI Container helps us to manage dependencies within the application in a simple and easy way.

The DI container creates an object of the defined class and also injects all the required dependencies as an object a constructor, a property, or a method that is triggered at runtime and disposes itself at the appropriate time. This process is completed so that we don't have to create and manage objects manually all the time.

These repository contains a DI container for PHP projects.

Table of Contents

  1. The Interface

  2. The Implementation

  3. Installation

  4. Getting Started

  5. Contributing

  6. Get in Touch

The Interface

The Dependency Injection Container interface defines four methods: autowire, get, set and singleton. All these methods expect a class or interface name as a string, but only the last two methods also expect a callable, which represents a definition of the class or interface.

<?php

declare(strict_types=1);

namespace Container\Adapter;

interface DependencyInjectionContainer
{
    public function autowire(string $className): object;

    /**
     * @template TClassName
     * @param class-string<TClassName> $className
     * @return TClassName
     */
    public function get(string $className): object;

    public function has(string $className): bool;

    public function set(string $className, callable $definition): self;

    public function singleton(string $className, callable $definition): self;
}

The Implementation

The implementation of the "DependencyInjectionContainer" interface is the "DependencyContainer" class and uses the PHP reflection API to manage the instantiation of registered classes.

<?php

declare(strict_types=1);

namespace Container\Infrastructure;

use Container\Adapter\DependencyInjectionContainer;
use ReflectionClass;
use ReflectionParameter;

final class DependencyContainer implements DependencyInjectionContainer
{
    private array $definitions = [];

    private array $singletons = [];

    public function autowire(string $className): object
    {
        $reflectionClass = new ReflectionClass($className);

        $constructorParameters = array_map(
            fn (ReflectionParameter $parameter) => $this->get($parameter->getType()->getName()),
            $reflectionClass->getConstructor()?->getParameters() ?? []
        );

        return new $className(...$constructorParameters);
    }

    public function get(string $className): object
    {
        if ($instance = $this->singletons[$className] ?? null) {
            return $instance;
        }

        $definition = $this->definitions[$className] ?? $this->autowire(...);

        return $definition($className);
    }

    public function has(string $className): bool
    {
        return isset($this->definitions[$className]) || isset($this->singletons[$className]);
    }

    public function set(string $className, callable $definition): self
    {
        $this->definitions[$className] = $definition;

        return $this;
    }

    public function singleton(string $className, callable $definition): self
    {
        $this->definitions[$className] = function () use ($className, $definition) {
            $this->singletons[$className] = $definition($this);

            return $this->singletons[$className];
        };

        return $this;
    }
}

Installation

First, make sure you have PHP installed (8.2 version or higher) and then clone this repository or install the library via composer.

  • Installation via Git
git clone git@github.com:AssoDePicche/php-di-container.git
  • Installation via Composer
composer require assodepicche/php-di-container

Getting Started

Instantiate the DependencyContainer class

<?php

use Container\Infrastructure\DependencyContainer;

$container = new DependencyContainer;

Use the set method to set the definition of a class

$container->set(Foo::class, fn () => new Foo);

Call the defined class with the get method whenever you want

$object = $container->get(Foo::class);

Use the has method to find out if an interface has been defined in the container

var_dump($container->has(Foo::class)); // true

Obs: to make a class a singleton, use the singleton method

$container->singleton(Singleton::class, fn () => new SingletonClass);

Contributing

To contribute to this project follow these steps.

Get in Touch

Samuel do Prado Rodrigues (AssoDePicche) - samuelprado730@gmail.com

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unlicense
  • 更新时间: 2023-07-23