fabpot/Pimple — pull request #29 by davedevelopment (2012-03-31 23:20:00+00:00)
Title: Adds extend method, allowing users to extend/customise object Closures

Kind of like an after filter for objects.

Use case:

Third party code is used to register an object, user wishes to add method calls to the definition, without loading it before it is actually required.

e.g. Add a custom loader to the Twig Service when using Silex

``` php
register(new Silex\Provider\TwigServiceProvider(), array(
'twig.path' => __DIR__.'/views',
'twig.class_path' => __DIR__.'/vendor/twig/lib',
));

$app->extend('twig', function($twig, $c) {
$twig->addLoader($c['my.custom.loader']);
return $twig;
});

```

I thought about returning a Closure and the user having to reassign it to maintain consistency with the existing API, like:

``` php
extend('twig', function($twig, $c) {
$twig->addLoader($c['my.custom.loader']);
return $twig;
});
```

But I can't think of a use case where you wouldn't want to assign it, so made the assignment part of the method.

Another option I considered would be to not require the original object to be returned in the extending Closure, but that would prevent people using decorators and such.