Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.3k
[DependencyInjection] Document ServiceSubscriberTrait#9809
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
nicolas-grekas commentedMay 27, 2018
What about adding a sentence to tell why this is nice? (Autocompletion mostly isn't it?) |
| provided that takes the class name and method name as arguments:: | ||
| // src/AppBundle/MyService.php | ||
| namespace AppBundle; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
bundle-less up to 4.0, should be:
// src/MyService.phpnamespaceApp;
2b9e556 to0de578bComparekbond commentedMay 31, 2018
I added an example showing how to create composable traits and a note about not using |
This PR was squashed before being merged into the 4.2-dev branch (closes #27077).Discussion----------[DependencyInjection] add ServiceSubscriberTrait| Q | A| ------------- | ---| Branch? | master| Bug fix? | no| New feature? | yes| BC breaks? | no| Deprecations? | no| Tests pass? | yes| Fixed tickets | #23898| License | MIT| Doc PR |symfony/symfony-docs#9809This allows you to easily configure Service Subscribers with the following convention:```phpclass MyService implements ServiceSubscriberInterface{ use ServiceSubscriberTrait; public function doSomething() { // $this->router() ... } private function router(): RouterInterface { return $this->container->get(__METHOD__); }}```This also allows you to create helper traits like `RouterAware`, `LoggerAware` etc... and compose your services with them (*not* using `__METHOD__` in traits because it doesn't behave as expected.).```phptrait LoggerAware{ private function logger(): LoggerInterface { return $this->container->get(__CLASS__.'::'.__FUNCTION__); }}``````phptrait RouterAware{ private function router(): RouterInterface { return $this->container->get(__CLASS__.'::'.__FUNCTION__); }}``````phpclass MyService implements ServiceSubscriberInterface{ use ServiceSubscriberTrait, LoggerAware, RouterAware; public function doSomething() { // $this->router() ... // $this->logger() ... }}```Commits-------238e793431 [DependencyInjection] add ServiceSubscriberTrait
This PR was squashed before being merged into the 4.2-dev branch (closes#27077).Discussion----------[DependencyInjection] add ServiceSubscriberTrait| Q | A| ------------- | ---| Branch? | master| Bug fix? | no| New feature? | yes| BC breaks? | no| Deprecations? | no| Tests pass? | yes| Fixed tickets |#23898| License | MIT| Doc PR |symfony/symfony-docs#9809This allows you to easily configure Service Subscribers with the following convention:```phpclass MyService implements ServiceSubscriberInterface{ use ServiceSubscriberTrait; public function doSomething() { // $this->router() ... } private function router(): RouterInterface { return $this->container->get(__METHOD__); }}```This also allows you to create helper traits like `RouterAware`, `LoggerAware` etc... and compose your services with them (*not* using `__METHOD__` in traits because it doesn't behave as expected.).```phptrait LoggerAware{ private function logger(): LoggerInterface { return $this->container->get(__CLASS__.'::'.__FUNCTION__); }}``````phptrait RouterAware{ private function router(): RouterInterface { return $this->container->get(__CLASS__.'::'.__FUNCTION__); }}``````phpclass MyService implements ServiceSubscriberInterface{ use ServiceSubscriberTrait, LoggerAware, RouterAware; public function doSomething() { // $this->router() ... // $this->logger() ... }}```Commits-------238e793 [DependencyInjection] add ServiceSubscriberTrait
javiereguiluz commentedJun 11, 2018
@kbond thanks for contributing these docs (and the original feature too). I love it because it explains things perfectly! |
…ond)This PR was merged into the master branch.Discussion----------[DependencyInjection] Document ServiceSubscriberTraitDocumentation forsymfony/symfony#27077Commits-------b0ac3a4 document ServiceSubscriberTrait
Documentation forsymfony/symfony#27077