Skip to content

[DI] add docs for new namespace option #8310

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
wants to merge 2 commits into from

Conversation

kbond
Copy link
Member

@kbond kbond commented Aug 28, 2017

Q A
Doc fix? no
New docs? yes
Applies to? 3.4
Fixed tickets? symfony/symfony#23991

nicolas-grekas added a commit to symfony/symfony that referenced this pull request Aug 29, 2017
… implementation) (kbond)

This PR was merged into the 3.4 branch.

Discussion
----------

[DI] Improve psr4-based service discovery (alternative implementation)

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

This is an alternative to #23986. It is simpler and doesn't require a second glob in the service id. This adds a `namespace` option. It is optional and if it isn't used, then it falls back to using the service id (current behaviour).

As stof mentions in #22397 (comment), it is consistent with the xml loader.

With this feature, you can define your services like this:

```yaml
services:
    command_handlers:
        namespace: App\Domain\
        resource: ../../src/Domain/*/CommandHandler
        tags: [command_handler]

    event_subscribers:
        namespace: App\Domain\
        resource: ../../src/Domain/*/EventSubscriber
        tags: [event_subscriber]
```

ping @stof, @nicolas-grekas

Commits
-------

00d7f6f [DI] improve psr4-based service discovery with namespace option
@HeahDude HeahDude changed the title [WCM][DI] add docs for new namespace option [DI] add docs for new namespace option Aug 31, 2017
@HeahDude HeahDude added this to the 3.4 milestone Aug 31, 2017
When using the ``resource`` option in YAML, the namespace prefix can only be used once
per file when defining it as the ``id``. In order to have multiple definitions in the
same file with the same namespace prefix, you can use the ``namespace`` option. When
this option is used, the ``id`` can be anything as long as it is unique. For example,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO this sentence is confusing. One may ask: Where is the id now taken from?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I remove the sentence?

this option is used, the ``id`` can be anything as long as it is unique. For example,
you can define your services like this:

.. configuration-block::
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can omit the configuration block and just start with the YAML code block.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -923,6 +923,34 @@ them will not cause the container to be rebuilt.
means that all classes are "available to be *used* as services" without needing
to be manually configured.

The namespace Option
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about enclosing the following section in a sidebar directive instead? IMO it's a nice addition for the YAML format, but not a totally new concept.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@kbond kbond force-pushed the prototype-namespace branch from 70ec5df to 16e0cc7 Compare September 5, 2017 19:44
@weaverryan
Copy link
Member

@kbond Can you tell me when this is a useful feature people (I didn't meant that to sound negative at all - I want to make sure I understand the precise use-case). Is it only when there are multiple classes in the same file? If not, I didn't quite understand the description of it.

I would also prefer to put this doc somewhere else (though I'm not sure where). I want to keep the main article very light and focused on the 90% use-cases.

@kbond
Copy link
Member Author

kbond commented Nov 30, 2017

Here is a use-case: SimpleBus/symfony-bridge#43 (comment)

It allows you to use the same namespace multiple times in the same yaml file.

@kbond
Copy link
Member Author

kbond commented Dec 1, 2017

FYI - XML has the namespace option already. I can find reference to it on this page: http://symfony.com/doc/current/service_container/3.3-di-changes.html. Don't know if it makes sense to move there as this is a 3.4 change.

@javiereguiluz
Copy link
Member

@kbond thanks for this contribution! I'm sorry we didn't merge it earlier. The doc your proposed was great ... but we try to not use sidebars in the docs (because you can't easily link to them directly) so I reworded the contents to be a normal section instead. Thanks!

javiereguiluz added a commit that referenced this pull request Feb 9, 2018
…luz)

This PR was merged into the 3.4 branch.

Discussion
----------

[DI] add docs for new namespace option

| Q             | A
| ------------- | ---
| Doc fix?       | no
| New docs?      | yes
| Applies to?  | 3.4
| Fixed tickets?    | symfony/symfony#23991

Commits
-------

f395dad Reworded and turned the sidebar into a section
16e0cc7 [DI] add docs for new namespace option
javiereguiluz added a commit to javiereguiluz/symfony-docs that referenced this pull request Feb 10, 2018
* 3.4:
  Fix missing trailling commas
  [Serializer] Added missing ObjectNormalizer
  [Serializer] By default the serializer do not convert to lower case properties
  Added a note about named form types
  Update translation.rst
  minor symfony#8310 [DI] add docs for new namespace option (kbond, javiereguiluz)
  Added a note about controller arguments
  Documented the new behavior of getGroupSequence() method
  Fix UrlMatcher::match() URL
  Updated a diagram to SVG format
javiereguiluz added a commit to javiereguiluz/symfony-docs that referenced this pull request Feb 10, 2018
* 4.0:
  Fix missing trailling commas
  [Serializer] Added missing ObjectNormalizer
  [Serializer] By default the serializer do not convert to lower case properties
  Added a note about named form types
  Update translation.rst
  minor symfony#8310 [DI] add docs for new namespace option (kbond, javiereguiluz)
  Added a note about controller arguments
  Documented the new behavior of getGroupSequence() method
  Fix UrlMatcher::match() URL
  Updated a diagram to SVG format
  Added a missing namespace in CacheKernel example
  fix creating a CacheKernel in HTTP cache documentation
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.

6 participants