Builder for generic and type safe decorators
OTHER License
A builder for generic and type safe PHP decorators.
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() ] );
} )