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

Skip to content

[DependencyInjection] Allow attribute autoconfiguration on static methods #47066

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
Jan 26, 2023

Conversation

alex-dev
Copy link
Contributor

@alex-dev alex-dev commented Jul 26, 2022

Q A
Branch? 6.2
Bug fix? no
Tickets #47060
License MIT

A static method can be used to carry complex configuration payload for DI.
A static method can be used to generate a Closure that can be hooked in the container (a static message handler with no dependency).
Using simple attribute autoconfigurator to wire those case easily.

@nicolas-grekas
Copy link
Member

Can you please give a more concrete use case? I don't understand the point for now. This would need test cases of course before being accepted.

@alex-dev
Copy link
Contributor Author

alex-dev commented Jul 29, 2022

Use cases

  • A system that can be configured by static methods flagged as such by attributes
  • A generalized callable
    • Autowire a bunch of attribute classes with methods flagged by attributes
    • Convert the static method to simple closures
    • Introspecting a static closure is really easy
    • Convert the instance methods to a wrapper taking a ServiceClosureArgument and a method name
    • Said wrapper may have metadata so we don't have to instantiate the whole service just to introspect it
    • Have a generalized bunch of callable in a container (tagged_iterable or tagged_locator)

Tests

I have not seen any tests for complex attributes in the compiler pass unit test. If you can point me at some classes that may have relevant tests, I'd be happy to test my changes.

@nicolas-grekas
Copy link
Member

The concept of "static" and "service" look orthogonal to me. Static is a kind of global state while a service is an instance, aka it has a local scope only. For methods, if they're purely functional, why not, but for properties, what's the use case?
Also, can you really describe a real-world use case? All your examples are still generic.

@alex-dev
Copy link
Contributor Author

alex-dev commented Aug 1, 2022

I just removed checks for properties because it seemed purely arbitrary to support static methods and not static properties.

Currently I'm implementing a workflow abstraction based on attributes. Configuring a single workflow is through a static method. Transition guards/actions are mere callable so anything that can be registered in the container (static or instance) can be used.

Sure, everything could be forced to be instance methods, but the overhead of instantiating a service on something that can be a simple closure is kinda annoying.

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Aug 2, 2022

So, you are using a static method as a factory for a closure and you're adding attributes on that factory to configure how it is wired?

Works for me.

But that's no-go for properties. There is nothing arbitrary there: a static property is global state. These don't belong to service configuration.

@alex-dev
Copy link
Contributor Author

alex-dev commented Aug 2, 2022

That's fine for me. I will fix the PR, just let me know where I could find relevant tests so I can add to them.

@nicolas-grekas
Copy link
Member

There is only src/Symfony/Component/DependencyInjection/Tests/Compiler/AttributeAutoconfigurationPassTest.php :)

@nicolas-grekas
Copy link
Member

Friendly ping @alex-dev

@nicolas-grekas nicolas-grekas modified the milestones: 6.2, 6.3 Nov 5, 2022
@alex-dev
Copy link
Contributor Author

alex-dev commented Jan 19, 2023

Ok.... Work is always hectic around Christmas. I could secure time for Symfony work in the next few weeks... I plan to finish this PR, and work on a few other issues I've reported, will report or have already been reported that have been annoying my coworkers... Should see a bit more of me in the next few weeks.

@alex-dev alex-dev force-pushed the di-static-attributes branch from c7a7d2e to 89d9363 Compare January 19, 2023 14:28
@nicolas-grekas nicolas-grekas changed the title [DependencyInjection] Allow attribute autoconfiguration on static methods or properties [DependencyInjection] Allow attribute autoconfiguration on static methods Jan 19, 2023
@nicolas-grekas
Copy link
Member

Thank you @alex-dev.

@nicolas-grekas nicolas-grekas merged commit 7f37a0f into symfony:6.3 Jan 26, 2023
@fabpot fabpot mentioned this pull request May 1, 2023
reviewtypo3org pushed a commit to TYPO3/typo3 that referenced this pull request Aug 15, 2023
With #101596 event listener registrations were migrated to
PHP attributes. Service registrations may only be performed
on non-static methods with symfony/dependency-injection <6.3 [1].
This caused the 'non-composer-class-loader' events to be silently
skipped, which is why no class loading information was dumped.
ClassLoadingInformation event listeners are moved into a separate
service with an instance method to circumvent this bug in
symfony/dependency-injection.

Also stray and unneeded Services.yaml configuration is removed.
(public: false is the default and does not need to be set at all)

[1] symfony/symfony#47066

Resolves: #101679
Resolves: #101681
Related: #101596
Releases: main
Change-Id: Ie18b8f1123364073a5df7a07b6f8bf71d27cd150
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/80546
Reviewed-by: Stefan B�rk <[email protected]>
Tested-by: core-ci <[email protected]>
Tested-by: Stefan B�rk <[email protected]>
Reviewed-by: Benjamin Franzke <[email protected]>
Tested-by: Benjamin Franzke <[email protected]>
TYPO3IncTeam pushed a commit to TYPO3-CMS/core that referenced this pull request Aug 15, 2023
With #101596 event listener registrations were migrated to
PHP attributes. Service registrations may only be performed
on non-static methods with symfony/dependency-injection <6.3 [1].
This caused the 'non-composer-class-loader' events to be silently
skipped, which is why no class loading information was dumped.
ClassLoadingInformation event listeners are moved into a separate
service with an instance method to circumvent this bug in
symfony/dependency-injection.

Also stray and unneeded Services.yaml configuration is removed.
(public: false is the default and does not need to be set at all)

[1] symfony/symfony#47066

Resolves: #101679
Resolves: #101681
Related: #101596
Releases: main
Change-Id: Ie18b8f1123364073a5df7a07b6f8bf71d27cd150
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/80546
Reviewed-by: Stefan B�rk <[email protected]>
Tested-by: core-ci <[email protected]>
Tested-by: Stefan B�rk <[email protected]>
Reviewed-by: Benjamin Franzke <[email protected]>
Tested-by: Benjamin Franzke <[email protected]>
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.

3 participants