symfony/symfony — pull request #9003 by fabpot (2013-09-12 10:19:25+00:00)
Title: [DependencyInjection] added a simple way to replace a service by keeping a reference to the old one

| Q | A
| ------------- | ---
| Bug fix? | no
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | #5920
| License | MIT
| Doc PR | not yet

TODO:

- [ ] add a cookbook entry for this new feature
- [ ] add support in all loaders/dumpers
- [ ] add unit tests
- [ ] find the best name for this feature

When overriding an existing definition, the old service is lost. Most of the
time, that's exactly what you want to do, but sometimes, you might want to
decorate the old one instead:

```php
$c->set('foo', 'stdClass');

// this is going to remove the old definition with the new one
// old definition is lost
$c->set('foo', 'stdClass');
```

In this case, the old service should be kept around to be able to reference it
in the new one. This PR implements this new feature:

```php
$c->set('foo', 'stdClass');

// I want to replace foo with a new one, but by decorating the old one
$c->set('foo1', 'stdClass')->addArgument('foo1.parent')->setSquashedService('foo');
```

Here is what's going on here: the `setSquashedService()` method tells the
container that the `foo1` service should replace the `foo` service. By
convention, the old `foo` service is going to be renamed `foo1.parent`, so I
can inject it into my new service.

You can change the parent service name if you want to:

```php
$c->set('foo1', 'stdClass')->addArgument('foo1.wooz')->setSquashedService('foo', 'foo1.wooz');
```

Also, several service can decorate a single one and everything should work fine:

```php
$c->set('foo', 'stdClass');

$c->set('foo1', 'stdClass')->addArgument('foo1.parent')->setSquashedService('foo');
$c->set('foo2', 'stdClass')->addArgument('foo2.parent')->setSquashedService('foo');
```

Note that this is different from the existing `parent` definition attribute,
where a service inherit/extends another one (as the two services co-exist
independently).

In XML, that would work like this:

```xml

```