Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[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

Merged
merged 1 commit into from
Jun 11, 2018

Conversation

kbond
Copy link
Member

@kbond kbond commented May 23, 2018

Documentation for symfony/symfony#27077

@kbond kbond changed the title Document ServiceSubscriberTrait [DependencyInjection] Document ServiceSubscriberTrait May 23, 2018
@javiereguiluz javiereguiluz added DependencyInjection Waiting Code Merge Docs for features pending to be merged labels May 23, 2018
@javiereguiluz javiereguiluz modified the milestones: 4.0, 4.2 May 23, 2018
@nicolas-grekas
Copy link
Member

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;
Copy link
Member

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;

@kbond kbond force-pushed the service-subscriber-trait branch 2 times, most recently from 2b9e556 to 0de578b Compare May 31, 2018 13:17
@kbond kbond force-pushed the service-subscriber-trait branch from 0de578b to b0ac3a4 Compare May 31, 2018 13:29
@kbond
Copy link
Member Author

kbond commented May 31, 2018

I added an example showing how to create composable traits and a note about not using __METHOD__ in this case.

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
@HeahDude HeahDude removed the Waiting Code Merge Docs for features pending to be merged label Jun 10, 2018
@javiereguiluz
Copy link
Member

@kbond thanks for contributing these docs (and the original feature too). I love it because it explains things perfectly!

@javiereguiluz javiereguiluz merged commit b0ac3a4 into symfony:master Jun 11, 2018
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
@kbond kbond deleted the service-subscriber-trait branch June 12, 2018 19:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants