-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[DependencyInjection] Allow Class::function(...) and global_function(...) closures in PHP DSL for factories
#61656
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
Class::function(...) and global_function(...) closures in PHP DSL for factoriesClass::function(...) and global_function(...) closures in PHP DSL for factories
Class::function(...) and global_function(...) closures in PHP DSL for factoriesClass::function(...) and global_function(...) closures in PHP DSL for factories
3d2d343 to
b1c7a25
Compare
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.
Pull Request Overview
This PR adds support for PHP 8.1+ first-class callable syntax in the Dependency Injection component's PHP DSL for factories. It allows using Class::method(...) and global_function(...) closures instead of array/string notation for factory definitions.
- Extends the
factory()method to accept\Closureparameters - Adds validation to ensure closures are first-class callables (not anonymous or bound)
- Converts closures to appropriate factory formats internally
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/Symfony/Component/DependencyInjection/Loader/Configurator/Traits/FactoryTrait.php | Adds closure support with validation and conversion logic |
| src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/services9.php | Updates test fixture to use new first-class callable syntax |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
src/Symfony/Component/DependencyInjection/Loader/Configurator/Traits/FactoryTrait.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/DependencyInjection/Loader/Configurator/Traits/FactoryTrait.php
Outdated
Show resolved
Hide resolved
b1c7a25 to
e577498
Compare
src/Symfony/Component/DependencyInjection/Loader/Configurator/Traits/FactoryTrait.php
Outdated
Show resolved
Hide resolved
nicolas-grekas
left a comment
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.
Nice one! Don't forget about configurators ;)
src/Symfony/Component/DependencyInjection/Loader/Configurator/Traits/FactoryTrait.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/DependencyInjection/Loader/Configurator/Traits/FactoryTrait.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/DependencyInjection/Loader/Configurator/Traits/FactoryTrait.php
Outdated
Show resolved
Hide resolved
GromNaN
left a comment
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.
Don't forget about configurators ;)
Could you point me in the direction of what else needs to be changed?
For tests, would you add new fixture.php files? I think unit tests would be fine.
src/Symfony/Component/DependencyInjection/Loader/Configurator/Traits/FactoryTrait.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/DependencyInjection/Loader/Configurator/Traits/FactoryTrait.php
Outdated
Show resolved
Hide resolved
|
src/Symfony/Component/DependencyInjection/Loader/Configurator/Traits/ConfiguratorTrait.php I'd go with a new fixture yes - to cover all these cases |
d85b952 to
46b9395
Compare
Class::function(...) and global_function(...) closures in PHP DSL for factoriesClass::function(...) and global_function(...) closures in PHP DSL for factories
Class::function(...) and global_function(...) closures in PHP DSL for factoriesClass::function(...) and global_function(...) closures in PHP DSL for factories
| } | ||
|
|
||
| /** | ||
| * Convert a named closure to dumpable callable. |
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.
| * Convert a named closure to dumpable callable. | |
| * Converts a named closure to dumpable callable. |
…on(...)` closures in PHP DSL for factories
58526b6 to
cde55c3
Compare
|
Thank you @GromNaN. |
For static class methods:
$container->services() ->set('date', DateTime::class) - ->factory([DateTime::class, 'createFromFormat']) + ->factory(DateTime::createFromFormat(...)) ->args(['d/m/Y', env('DATE')])For global functions:
$container->services() ->set('date', DateTime::class) - ->factory('date_create') + ->factory(date_create(...)) ->args(['d/m/Y', env('DATE')])