-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
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.php
namespace App;
2b9e556
to
0de578b
Compare
0de578b
to
b0ac3a4
Compare
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#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
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
@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 ServiceSubscriberTrait Documentation for symfony/symfony#27077 Commits ------- b0ac3a4 document ServiceSubscriberTrait
Documentation for symfony/symfony#27077