定制 ganyariya/hako 二次开发

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

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

ganyariya/hako

最新稳定版本:v0.1.1

Composer 安装命令:

composer require ganyariya/hako

包简介

A super simple DI Container

README 文档

README

A super simple DI Container 📦 of PHP (based on PSR-11).

📦 How to Install

composer require ganyariya/hako

📦 Example

The detail example can be seen in tests.

Set / Get

<?php

use Ganyariya\Hako;
use Ganyariya\Hako\Container\Container;
use Psr\Container\ContainerInterface;

/**
 * Suppose that your some interface and class are already defined.
 * For example, Your UserRepositoryInterface, UserRepository, MasterRepositoryInterface, and MasterRepository are defined.
 */

$container = new Container();

$container->set("Hello", "world!");

// Pattern1: Hako\fetch can automatically resolve dependencies using builtin \Reflection.
$container->set(UserRepositoryInterface::class, Hako\fetch(UserRepository::class));
$container->set(MasterRepositoryInterface::class, Hako\fetch(MasterRepository::class));

// Pattern2: Your Self Injection
$container->set(UserRepositoryInterface::class, function(ContainerInterface $c) {
    /**
     * You can pre-set up objects at here.
     */
    return new UserRepository(
        $c->get(MasterRepositoryInterface::class);
    );
});

assert($container->get("Hello") === "world!");
assert($container->get(UserRepositoryInterface::class) instanceof UserRepository);

/**
 * After setting up the Container, pass the Container to a WebFramework such as Slim or Laravel.
 * https://www.slimframework.com/docs/v4/concepts/di.html
 * /

Container Builder

<?php

use Ganyariya\Hako;
use Ganyariya\Hako\Container\ContainerBuilder;

$builder = new ContainerBuilder();
$builder->addDefinitions([
    GetsInterface::class => Hako\fetch(GetsInteractor::class),
    MasterRepositoryInterface::class => Hako\fetch(MasterRepository::class),
]);
$builder->addDefinitions([
    UserRepositoryInterface::class => Hako\fetch(UserRepository::class)
]);

// From definitions, create container.
$container = $builder->build();

/** @var GetsController $controller */
$controller = $container->get(GetsController::class);

/**
 * After setting up the Container, pass the Container to a WebFramework such as Slim or Laravel.
 * https://www.slimframework.com/docs/v4/concepts/di.html
 */

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-08-07