PHPackages                             jeroen/generic-decorator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. jeroen/generic-decorator

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

jeroen/generic-decorator
========================

Builder for generic and type safe decorators

0.1.2(9y ago)18.2k[1 PRs](https://github.com/JeroenDeDauw/GenericDecorator/pulls)GPL-2.0+PHPPHP &gt;=7.0

Since Jul 29Pushed 6y ago4 watchersCompare

[ Source](https://github.com/JeroenDeDauw/GenericDecorator)[ Packagist](https://packagist.org/packages/jeroen/generic-decorator)[ Docs](https://github.com/JeroenDeDauw/GenericDecorator)[ RSS](/packages/jeroen-generic-decorator/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependencies (3)Versions (5)Used By (0)

Generic Decorator
=================

[](#generic-decorator)

[![Build Status](https://camo.githubusercontent.com/f7a6eb77fd689a95139314193591de30201e5e53741b946092edbc47b3edb2dc/68747470733a2f2f7365637572652e7472617669732d63692e6f72672f4a65726f656e4465446175772f47656e657269634465636f7261746f722e706e673f6272616e63683d6d6173746572)](http://travis-ci.org/JeroenDeDauw/GenericDecorator)[![Latest Stable Version](https://camo.githubusercontent.com/d9ea99609e6b73dcb43edb28915e6ea65945f5a0f9e8e54ab24db83c9b689903/68747470733a2f2f706f7365722e707567782e6f72672f6a65726f656e2f67656e657269632d6465636f7261746f722f76657273696f6e2e706e67)](https://packagist.org/packages/jeroen/generic-decorator)[![Download count](https://camo.githubusercontent.com/f5363f1ea58550b39b02ce3d799e282c80d1489eede5db6c041725d65174e7a0/68747470733a2f2f706f7365722e707567782e6f72672f6a65726f656e2f67656e657269632d6465636f7261746f722f642f746f74616c2e706e67)](https://packagist.org/packages/jeroen/generic-decorator)

A builder for generic and type safe PHP decorators.

Usage
-----

[](#usage)

This library provides the `DecoratorBuilder` class, which follows the [Builder pattern](https://en.wikipedia.org/wiki/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
--------------------------

[](#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
-------------

[](#release-notes)

### 0.1.2 (2017-02-06)

[](#012-2017-02-06)

- Made installable with PHPUnit Mock Objects 4.x

### 0.1.1 (2016-08-16)

[](#011-2016-08-16)

- Decorating classes with required constructor parameters now works

### 0.1.0 (2016-07-29)

[](#010-2016-07-29)

- Initial release

###  Health Score

30

—

LowBetter than 62% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 94.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~96 days

Total

3

Last Release

3425d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/451bd4039d530fed8f9c3da91bfa519233a397d2182cdfdcad700f6cfea19b7f?d=identicon)[Jeroen De Dauw](/maintainers/Jeroen%20De%20Dauw)

---

Top Contributors

[![JeroenDeDauw](https://avatars.githubusercontent.com/u/146040?v=4)](https://github.com/JeroenDeDauw "JeroenDeDauw (18 commits)")[![gbirke](https://avatars.githubusercontent.com/u/223326?v=4)](https://github.com/gbirke "gbirke (1 commits)")

###  Code Quality

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/jeroen-generic-decorator/health.svg)

```
[![Health](https://phpackages.com/badges/jeroen-generic-decorator/health.svg)](https://phpackages.com/packages/jeroen-generic-decorator)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
