Skip to content

[DI] PSR4 loader suggestion #22397

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
kbond opened this issue Apr 12, 2017 · 7 comments
Closed

[DI] PSR4 loader suggestion #22397

kbond opened this issue Apr 12, 2017 · 7 comments

Comments

@kbond
Copy link
Member

kbond commented Apr 12, 2017

Q A
Bug report? no
Feature request? yes
BC Break report? no
RFC? yes
Symfony version 3.3.0 (master)

I'm really enjoying the new PSR4 loader but have a suggestion. It would be nice if I could declare multiple definitions with the same prefix. Example:

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

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

I can't use instanceof here because my handlers/subscribers don't have interfaces.

Ref: #22016 (comment)

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Jul 28, 2017

Adding new syntax adds complexity so better not if possible.
Yet, if someone wants to implement this, what about this alternative:

services:
    App\Domain\*\CommandHandler\:
        resource: ../../src/Domain/*/CommandHandler
        tags: [command_handler]

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

@nicolas-grekas
Copy link
Member

@kbond interested in trying a PR doing what I described just above?

@kbond
Copy link
Member Author

kbond commented Aug 25, 2017

@nicolas-grekas sure - I will give it a shot

@stof
Copy link
Member

stof commented Aug 25, 2017

@nicolas-grekas having globs inside the key is not simpler IMO, as it is not a prefix anymore either.
I'm fine with adding an optional setting for the namespace prefix, as long as the key is still used in case you omit this setting (i.e. the behavior we have in 3.3)
However, instead of psr4_prefix, I would use namespace, to be consistent with the XML format.

On a side note, it is already possible to use the same prefix multiple time in XML files, as the prefix is not used as a PHP array key there 😄

@kbond
Copy link
Member Author

kbond commented Aug 25, 2017

Yeah, having a glob in 2 places would be annoying...

@nicolas-grekas
Copy link
Member

having globs inside the key is not simpler IMO

simpler than what? there has been no other proposal that really work (the proposed one doesn't, nothing personal :) )

@nicolas-grekas
Copy link
Member

Rereading your yesterday's comment @stof, it looks like I didn't get it then, sorry about that.
Good idea in fact!

nicolas-grekas added a commit that referenced this issue 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants