定制 phpbench/container 二次开发

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

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

phpbench/container

最新稳定版本:2.2.3

Composer 安装命令:

composer require phpbench/container

包简介

Simple, configurable, service container.

README 文档

README

Build Status

Simple, extensible dependency injection container with parameters and service tagging. Implements container interop.

Simple usage

$container = new Container();
$container->register('foobar', function (Container $container) {
    return new \stdClass();
});

Extending and Tagging

Extension classes should be passed as the first argument to the container (the user configuration is the second argumnet).

$container = new Container(
    [
        MyExtension::class
    ],
    [
        'foo.bar' => 'my_new_value',
    ]
);
$container->init(); // will trigger loading of the extensions.
class MyExtension implements ExtensionInterface
{
    public function load(Container $container)
    {
        $container->register('my_service', function (Container $container) {
            $service = new MyService(
                $container->getParameter('foo_bar'),
                $container->get('some_other_service')
            );

            foreach ($container->getServiceIdsForTag('tag') as $serviceId => $params) {
                $service->add($container->get($serviceId));
            }

            return $service;
        });

        $container->register('tagged_service', function (Container $container) {
            return new MyService(
                $container->getParameter('foo_bar'),
                $container->get('some_other_service')
            );
        }, [ 'tag' => [ 'param1' => 'foobar' ]);
    }

    /**
     * Return the default parameters for the container.
     *
     * @return array
     */
    public function getDefaultConfig()
    {
        return [
            'foo_bar' => 'this is foo'
        ];
    }
}

统计信息

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

GitHub 信息

  • Stars: 15
  • Watchers: 2
  • Forks: 7
  • 开发语言: PHP

其他信息

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