承接 guide42/suda 相关项目开发

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

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

guide42/suda

Composer 安装命令:

composer require guide42/suda

包简介

Registry of values and dependency injector

README 文档

README

Suda is a lightweight container for your services.

Quick Start

Creating and configuring the registry is as simple as giving an associative array that map a class to a factory or a key to a parameter value.

$di = new suda\Registry([
    MovieFinder::class => function(callable $make) {
        return new MovieFinder;
    },
    MovieLister::class => function(callable $make, MovieFinder $finder) {
        return new MovieLister($finder);
    },
]);

All objects are created once, when first requested, using the factory, and the same instance will be returned at each lookup.

$finder0 = $di[MovieFinder::class];
$finder1 = $di[MovieFinder::class];

assert($finder0 === $finder1);

Factories

Any callable that accepts a make function as first argument is a factory, any other arguments are resolved as dependencies. It purpose is to create the object when the class or interface is requested.

Factories can be stacked and called in a middleware fashion. The given make function when called without arguments will:

  • Calls the previous factory if stacked,
  • Or tries to create the object of the concrete class key, resolving its dependencies.
$di[ConcreteHouse::class] = function(callable $make) {
    $house = $make(); // creates the object resolving dependencies
    return $house;
};
$di[ConcreteHouse::class] = function(callable $make) {
    $house = $make(); // calls the previous factory
    $house->build();
    return $house;
};

The make function also can be used to create any class with arguments:

$di[NumberValidatorInterface::class] = function(callable $make) {
    return $make(BCNumberValidator::class, ['min' => 5]);
};

API

$di = new Registry;                                    // creates empty registry
$di = new Registry(array $values);                     // ... with assoc-array containing values or factories
$di = new Registry(array $values, Registry $delegate); // ... with registry to delegate dependencies

$di[string $classOrInterface] = callable $factory;     // stores a factory for abstract
$di[string $key] = mixed $value;                       // stores a parameter

$di(callable $fn);                                     // call a function resolving it's parameters
$di(callable $fn, array $arguments);                   // ... with given arguments

$di->freeze();                                         // disallow to store values or factories
$di->freeze(string $key);                              // ... for this entry key

Badges

Latest Stable Version Build Status Coverage Status

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: ISC
  • 更新时间: 2014-10-03

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固