Skip to content

pattern to extend a service definition and aliasing new definition to the old name #5920

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
lsmith77 opened this issue Nov 6, 2012 · 7 comments

Comments

@lsmith77
Copy link
Contributor

lsmith77 commented Nov 6, 2012

i keep stumbling over this use case:

https://github.com/liip/LiipFunctionalTestBundle/pull/70/files#L7R25
https://github.com/liip/LiipFunctionalTestBundle/pull/70/files#L1R13

what is the best practice here? is there one? is there something that we could change to make this easier? specifically i would love to avoid duplicating the definition. guess we could clone the original test.client definition in the extension?

@stof
Copy link
Member

stof commented Nov 6, 2012

I have a compiler pass that I copy-pasted in several projects for this: https://github.com/FriendsOfSymfony/FOSAdvancedEncoderBundle/blob/master/DependencyInjection/Compiler/SecurityEncoderFactoryPass.php

Maybe we could provide a compiler pass in the DI component (the same code but making the 3 service ids configurable through the constructor) so that bundles can simply use it ?

@stof
Copy link
Member

stof commented Nov 6, 2012

note that this compiler pass allows to compose the service several times if all bundles tryign to replace the service by composition are doing it cleanly (like in this example) instead of hardcoding the id of the old service

@schmittjoh
Copy link
Contributor

I'd rather add something like a CallbackCompilerPass that should make it relatively painless to perform minor operations during compile-time without implementing an extra class for it.

@stof
Copy link
Member

stof commented Nov 6, 2012

@schmittjoh A CallbackCompilerPass would still require every bundle owner trying to composer a service to know how this could be achieved (and handling the different cases). The advantage of this class would be that they have a working implementation and can simply use it (btw, this could even be used in FrameworkBundle IIRC)

@colinfrei
Copy link
Contributor

@stof yep, something like this seems as if it would be useful: https://gist.github.com/4030056

In the case Lukas referenced above (https://github.com/liip/LiipFunctionalTestBundle/pull/70/files#L1R12 ) I'd still need to extend the class and then just execute the parent based on the condition, but in a lot of cases you likely wouldn't need an additional class and could just add the compilerpass in the bundle class.

Not sure what it takes to have a baseclass like this, but an alternative may be a cookbook entry showing similiar code.

@schmittjoh A CallbackCompilerPass seems to be a good idea as well, but unrelated. I'm also not sure if it would work in this specific case, since you'd need access to the parameters of the bundle, which aren't set yet.

@colinfrei
Copy link
Contributor

I updated the pull request that was referenced above with Stof's template, this is what the compilerpass function looked like before: colinfrei/LiipFunctionalTestBundle@b401f87

@fabpot
Copy link
Member

fabpot commented Sep 12, 2013

Have a look at #9003 for a possible generic implementation.

fabpot added a commit that referenced this issue Apr 1, 2014
…service by keeping a reference to the old one (romainneutron)

This PR was merged into the 2.5-dev branch.

Discussion
----------

[DependencyInjection] added a simple way to replace a service by keeping a reference to the old one

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

This PR replaces #9003. Here's the todo list:
 - [x] add a cookbook entry for this new feature
 - [x] add support in all loaders/dumpers
 - [x] add unit tests
 - [x] see if there are use cases in Symfony that would benefit from this new feature
 - [x] find the best name for this feature

I've implemented YAML and XML Loader / Dumper.
From what I see, PhpDumper, PhpLoader, IniLoader, GraphvizDumper do not require an update, am I wrong?

Commits
-------

140f807 [DependencyInjection] Update dumpers and loaders, add unit tests
1eb1f4d [DependencyInjection] added a simple way to replace a service by keeping a reference to the old one
@fabpot fabpot closed this as completed Apr 1, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants