承接 starbug/di 相关项目开发

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

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

starbug/di

最新稳定版本:v0.8

Composer 安装命令:

composer require starbug/di

包简介

A dependency injection container initialization library.

README 文档

README

A dependecy injection meta package and container initialization library for PHP-DI.

What's included

  • PHP-DI/PHP-DI The actual dependency injection library.
  • ContainerFactory - A configurable factory for initializing containers and running post initialization steps.
  • DefaultConfigurationHandler - A default handler for the factory that loads container definitions from etc/di.php in the root package and overrides from etc/di.php in modules that provide it. It can also set the default timezone and register an error handler after the container is initialized.

Usage

Application bootstrapping

The simplest way to use this library is to bootstrap your application with it and let it use the default configuration handler.

include("vendor/starbug/di/bootstrap/init.php");

// You now have a Psr\Container\ContainerInterface instance
$application = $container->make("MyApp");

Direct initialization

To do the same thing directly.

use Starbug\DI\ContainerFactory;

include("vendor/autoload.php");

$container = ContainerFactory::withDefaultHandler()
  ->create();

// You now have a Psr\Container\ContainerInterface instance
$application = $container->make("MyApp");

Definitions

The default handler will look for a file at etc/di.php to load definitions from. To customize MyApp our file might look like this.

return [
  "MyApp" => function () {
    $app = new MyApp();
    $app->addMiddleware(new MyMiddleware());
    return $app;
  }
];

See the PHP-DI documentation on PHP definitions.

Modules

If you are using starbug/composer-modules-plugin, the default handler will also check each module for an etc/di.php file to load additional definitions from.

Custom Handlers

To use custom handlers, call withHandlers instead of withDefaultHandler and pass your handlers.

use Starbug\DI\ContainerFactory;

$container = ContainerFactory::withHandlers(
  new MyHandler(),
  new SecondHandler()
)->create();

Alternatively, if you don't want to use the static helper method you can easily do it manually.

use Starbug\DI\ContainerFactory;

$factory = new ContainerFactory();
$factory->addHandler(new MyHandler());
$container = $factory->create();

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: GPL-3.0-or-later
  • 更新时间: 2021-01-17