clippy/container 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

clippy/container

最新稳定版本:v1.4.2

Composer 安装命令:

composer require clippy/container

包简介

Dependency-injection container inspired by Pimple and PHP-DI Invoker

README 文档

README

This is a derivative of pimple with a few notable changes.

Function parameter injection

In standard Pimple, the service factories all receive Container $c as input, e.g.

$c['descendent'] = function($c) {
  return $c['parent']->generateDescendent()->withWhizBang();
};

In Clippy's container:

$c['descendent'] = function(MyParent $parent) {
  return $parent->generateDescendent()->withWhizBang();
};

This allows the consumers of services to (progressively) use type-hinting.

Service methods

A "service method" is a function which supports both service-injection and runtime data-passing. For example:

$c['getPassword()'] = function ($domain, SymfonyStyle $io) {
  if (getenv('PASSWORD')) return getenv('PASSWORD');
  return $io->askHidden("What is the password for <comment>$domain</comment>?");
}
$c['app']->main('', function($getPassword) {
  $pass = $getPassword('example.com');
});

The first parameter to getPassword is given at runtime ($getPassword('example.com')); the second parameter ($io) is injected automatically.

The service-method is denoted by including () in the declaration. Compare:

// Standard service
$c['foo'] = function($injectedService, ...) { ... }

// Service method
$c['foo()'] = function($inputtedData, ... , $injectedService, ...)  { ... }
};

Autowired objects / anonymous service classes

The following allows for using injection with improvised service classes.

$c['basicService'] = 'something';
$c['newService'] = $c->autowiredObject(new class() {

  protected $basicService;

  public function double() {
    return $this->basicService . $this->basicService;
  }

});

Properties (eg $basicService) will be pre-populated with the corresponding services.

In the default strict mode, unmatched properties will produce exceptions. This can be disabled, e.g.

$c['newService'] = $c->autowiredObject(['strict' => FALSE], new class() { ..});

Similarly, you may define a regular service function and use autowiring as part of the logic, e.g.

$c['basicService'] = 'something';
$c['newService'] = function() use ($c) {
  return $c->autowire(new MyClass());
};

Sigils

In standard Pimple, you may define alternative handling for a callback by using a wrapper method. Clippy supports wrappers as well as a sigil notation.

// Run a function every time one reads `$c['theJoneses]`, with mix of inputs and services
$c['getPassword'] = $c->method(function ($domain, SymfonyStyle $io) { ... });
$c['getPassword()'] = function ($domain, SymfonyStyle $io) { ... };
$c['getPassword']('localhost');
$c['getPassword']('example.com');

// Create a new instance every time one reads `$c['theJonses']`:
$c['theJoneses'] = $c->factory(function() { return new CoolThing(microtime(1)); });
$c['theJoneses++'] = function() { return new CoolThing(microtime(1)); };
print_r($c['theJoneses']);
print_r($c['theJoneses']);

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-12-08