承接 gspataro/dependencyinjection 相关项目开发

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

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

gspataro/dependencyinjection

Composer 安装命令:

composer require gspataro/dependencyinjection

包简介

A dependency injection container component to handle the booting process of your application

README 文档

README

A dependency injection container component made to handle the booting process of your application easier.

Installation

Requires [PHP 8.0+](PHP: Releases)

To install this package you have to require it with Composer:

composer require gspataro/dependencyinjection

Quick start

The fastest way to use the container is to initialize it and provide the services that you need:

<?php

use GSpataro\DependencyInjection\Container;

$container = new Container();

/**
 * Register a new service
 * 
 * @param string   $tag                The name that will identify this service
 * @param callable $factory            The factory that will create the instance of the class
 * @param bool     $isSingleton = true If true, the service will be instanciated once the first time
 */

$container->add("example", function (Container $c, array $params) {
    return new stdClass();
});

/**
 * Get a service
 * Note: if the service is a singleton, the $params array will be used only the first time this method is called
 * 
 * @param string $tag        The identifier of the service
 * @param array $params = [] An array of arguments used by the factory to instanciate the class
 */

$container->get("example");

Container

The Container class is the heart of this component.

Service dependencies

When defining a service, the Container class and params array are passed to the factory to have access to the class dependencies.

/**
 * Let's assume the class of this service will need the "example" class created in the quick start guide
 * and some other informations
 */

$container->add("second_example", function (Container $c, array $params) {
    return new stdClass($c->get("example"), $params['someinfo']);
});

Variables

Sometimes some configurations or other variables will be needed globally across services. In this case, you can define variables accessible by the container.

/**
 * Set a variable
 * 
 * @param string $key          The name of the variable
 * @param mixed  $value = null The value of the variable (if null, the method will retrieve the value instead of setting it)
 */

$container->variable("foo", "bar");

/**
 * Get a variable
 * 
 * Output: bar
 */

$container->variable("foo");

Direct instance

If you need to instanciate a new class but you don't need to register it as a service, you can directly instanciate it. This is not recomended, but you'll still need it in some cases:

/**
 * Directly instanciate a class
 * 
 * @param callable $factory  The factory that will instanciate the class
 * @param array $params = [] Arguments needed by the factory
 */

Container::instanciate(function (Container $c, array $params) {
    return new stdClass();
});

Components

The components are another feature of the container. The issue is: if you define your services one by one, the risk of booting a service before its dependency is higher and you have to remember what you booted first.

To simplify this process, the components are "archives" of services that will be booted in the order you specify.

Defining a component

<?php

use GSpataro\DependencyInjection\Component;
use GSpataro\DependencyInjection\Container;

/**
 * A component must extend the Component abstract class
 */

final class MyComponent extends Component
{
    /**
     * Register the services that you need
     */

    public function register(): void
    {
        $this->container->add("myservice", function (Container $c, array $params) {
            return new stdClass();
        });
    }

    /**
     * If your services need to be booted with the application, call them inside this method
     * Here you can handle everything your service needs in order to boot
     */

    public function boot(): void
    {
        $this->container->get("myservice");
    }
}

Boot components

To boot your components you need to register them into the container and then call the boot method.

/**
 * Register components
 */

$container->loadComponents([
    MyComponent::class
]);

/**
 * Boot components
 */

$container->boot();

Let's assume you have two components: ComponentA and ComponentB. If ComponentA depends on some services provided by ComponentB, you can change the order of them in the loadComponents method:

$container->loadComponents([
    ComponentB::class,
    ComponentA::class
]);

gspataro/dependencyinjection 适用场景与选型建议

gspataro/dependencyinjection 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 25 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 03 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 gspataro/dependencyinjection 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 gspataro/dependencyinjection 我们能提供哪些服务?
定制开发 / 二次开发

基于 gspataro/dependencyinjection 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-03-09