kilahm/ioc-factory-container
最新稳定版本:v0.3.2
Composer 安装命令:
composer require kilahm/ioc-factory-container
包简介
Using attributes, compile your factories into a single class
README 文档
README
Compile a type safe IOC Container from user defined attributes.
Use
This library includes an executable that will scan your project directory for factories then construct a valid hack file that aliases all your factories as public instance methods of a single class. Your factory methods must accept only a single parameter which is the factory container.
The factory container should only be instantiated once in your application's bootstrap. You should then be able to use the container to instantiate your application class and run it.
Mark factories with attributes
Here is an example class with its factory function marked.
<?hh // strict final class A { <<provides('myA')>> public static function factory(FactoryContainer $c) : this { return new static(); } }
The attribute name is provides and requires one paramter, which is an alias for the factory method. The above definition would compile to:
... <<__Memoize>> public function getMyA() : /Foo { return $this->newMyA(); } public function newMyA() : /Foo { return $this->runner->make(class_meth('/A', 'factory')); } ...
Note that you are able to retrieve the same instance by calling $factory->getMyA() any number of times, but calling $factory->makeMyA() will always return a new instance.
Run the factory container compiler
After defining some factory methods, run the findfactories executable, which can be found in your vendor/bin directory.
The executable takes any number of arguments which will be interpreted as base directories to scan.
The executable will also accept any number of --exclude="..." long options which is interpreted as a directory to ignore.
vendor/bin/findfactories src/ other/path --exclude="src/ignore” --exclude=”other/path/to/ignore"
The command above (if run from your project directory) will recursively scan the src/ and other/path/ directories, except for the src/ignore, other/path/to/ignore directories and their children.
The resulting factory container file will be created in the project root and will be named FactoryContainer.php.
You may optionally provide the path to install FactoryContainer.php by using the --install-path="dir/to/put/file" option.
Namespaces
You may use namespaces and use as normal, and the compiler will expand the class names to include their namespace.
<?hh // strict namespace Foo\Baz; use Foo\Bar\IFoo; final class Foo implements IFoo { <<provides('realFoo')>> public static function fooFactory(FactoryContainer $c) : this { return new static(); } ... }
<?hh // strict namespace Bar; use Foo\Bar\IFoo; final class FooBar implements IFoo { <<provides('fooBar')>> public static function barFactory(FactoryContainer $c) : this { return new static(); } ... }
The files above would compile to container methods shown below.
... <<__Memoize>> public function getRealFoo() : \Foo\Baz\Foo { return $this->newRealFoo(); } public function newRealFoo() : \Foo\Baz\Foo { return $this->runner->make(class_meth('\Foo\Baz\Foo', 'fooFactory')); } <<__Memoize>> public function getFooBar() : \Bar\FooBar { return $this->newFooBar(); } public function newFooBar() : \Bar\FooBar { return $this->runner->make(class_meth('\Bar\FooBar', 'barFactory')); } ...
None of the examples above were particularly useful factories. This tool would be more useful when a particular class has many dependencies and you wish to encapsulate instantiation inside of a single method.
<?hh // strict namespace CoolStuff; use A; use Foo\Bar\IFoo; <<__ConsistentConstruct>> class Awesome { <<provides('awesomeWithFoo')>> public static function makeWithRealFoo(FactoryContainer $c) : this { return new static($c->getRealFoo(), $c->getA()); } <<provides('awesomeWithBar')>> public static function makeWithFooBar(FactoryContainer $c) : this { return new static($c->getFooBar(), $c->getA()); } public function __construct(private IFoo $foo, private A $a) { }
kilahm/ioc-factory-container 适用场景与选型建议
kilahm/ioc-factory-container 是一款 基于 Hack 开发的 Composer 扩展包,目前已累计 43 次下载、GitHub Stars 达 3, 最近一次更新时间为 2014 年 11 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「dependency injection」 「container」 「di」 「inversion of control」 「ioc」 「factory」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 kilahm/ioc-factory-container 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kilahm/ioc-factory-container 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 kilahm/ioc-factory-container 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A fast and intuitive dependency injection container.
Dependency injection container for the Monolith framework.
The PSR-11 container bridges
Http Microframework for Hack
PHP Dependency Injection Container
统计信息
- 总下载量: 43
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 27
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-11-21