jeroen/generic-decorator
Composer 安装命令:
composer require jeroen/generic-decorator
包简介
Builder for generic and type safe decorators
README 文档
README
A builder for generic and type safe PHP decorators.
Usage
This library provides the DecoratorBuilder class, which follows the
Builder pattern, and
thus is similar in use as PHPUnit's MockBuilder interface.
You construct a new builder by calling DecoratorBuilder::newBuilder and
passing in the object you want to decorate. Then you can call withBefore
and withAfter, to define the decorated behaviour. Finally you call
newDecorator and get the decorated instance.
public function __construct() { $this->repository = new DoctrineKittenRepository( /* ... */ ); $this->stopWatch = new Stopwatch(); } public function newProfilingKittenRepository(): KittenRepository { return DecoratorBuilder::newBuilder( $this->repository ) ->withBefore( function() { $this->stopWatch->start( 'KittenRepository' ); } ) ->withAfter( function() { $this->stopWatch->stop( 'KittenRepository' ); } ) ->newDecorator(); }
The callable provided to withBefore and withAfter receives all
arguments the decorated method receives.
->withBefore( function() { $this->logger->alert( 'KittenRepository', [ 'arguments' => func_get_args() ] ); } )
Missing features / roadmap
- Allow decorating generated decorators (this will fatal error at present)
- Support try-catch, ie for logging decorators
- Provide a way to get the name of the calling method (anyone knows how to do that without crazy debug_backtrace hacks?)
- Test presence of private and protected methods
- Test final class
- Test exceptions
- Test internal calls to public methods
Release notes
0.1.2 (2017-02-06)
- Made installable with PHPUnit Mock Objects 4.x
0.1.1 (2016-08-16)
- Decorating classes with required constructor parameters now works
0.1.0 (2016-07-29)
- Initial release
统计信息
- 总下载量: 8.58k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0
- 更新时间: 2016-07-20