[DependencyInjection] Document ServiceSubscriberTrait#9809
Merged
Conversation
Member
|
What about adding a sentence to tell why this is nice? (Autocompletion mostly isn't it?) |
yceruto
reviewed
May 27, 2018
| provided that takes the class name and method name as arguments:: | ||
|
|
||
| // src/AppBundle/MyService.php | ||
| namespace AppBundle; |
Member
There was a problem hiding this comment.
bundle-less up to 4.0, should be:
// src/MyService.php
namespace App;2b9e556 to
0de578b
Compare
0de578b to
b0ac3a4
Compare
Member
Author
|
I added an example showing how to create composable traits and a note about not using |
symfony-splitter
pushed a commit
to symfony/dependency-injection
that referenced
this pull request
Jun 4, 2018
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#9809 This allows you to easily configure Service Subscribers with the following convention: ```php class 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.). ```php trait LoggerAware { private function logger(): LoggerInterface { return $this->container->get(__CLASS__.'::'.__FUNCTION__); } } ``` ```php trait RouterAware { private function router(): RouterInterface { return $this->container->get(__CLASS__.'::'.__FUNCTION__); } } ``` ```php class MyService implements ServiceSubscriberInterface { use ServiceSubscriberTrait, LoggerAware, RouterAware; public function doSomething() { // $this->router() ... // $this->logger() ... } } ``` Commits ------- 238e793431 [DependencyInjection] add ServiceSubscriberTrait
nicolas-grekas
added a commit
to symfony/symfony
that referenced
this pull request
Jun 4, 2018
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#9809 This allows you to easily configure Service Subscribers with the following convention: ```php class 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.). ```php trait LoggerAware { private function logger(): LoggerInterface { return $this->container->get(__CLASS__.'::'.__FUNCTION__); } } ``` ```php trait RouterAware { private function router(): RouterInterface { return $this->container->get(__CLASS__.'::'.__FUNCTION__); } } ``` ```php class MyService implements ServiceSubscriberInterface { use ServiceSubscriberTrait, LoggerAware, RouterAware; public function doSomething() { // $this->router() ... // $this->logger() ... } } ``` Commits ------- 238e793 [DependencyInjection] add ServiceSubscriberTrait
tgalopin
approved these changes
Jun 5, 2018
HeahDude
approved these changes
Jun 10, 2018
Member
|
@kbond thanks for contributing these docs (and the original feature too). I love it because it explains things perfectly! |
javiereguiluz
added a commit
that referenced
this pull request
Jun 11, 2018
…ond) This PR was merged into the master branch. Discussion ---------- [DependencyInjection] Document ServiceSubscriberTrait Documentation for symfony/symfony#27077 Commits ------- b0ac3a4 document ServiceSubscriberTrait
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Documentation for symfony/symfony#27077