Skip to content

Conversation

@GromNaN
Copy link
Member

@GromNaN GromNaN commented Sep 5, 2025

Q A
Branch? 7.4
Bug fix? no
New feature? yes
Deprecations? no
Issues -
License MIT

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')])

@carsonbot carsonbot added this to the 7.4 milestone Sep 5, 2025
@carsonbot carsonbot changed the title [DependencyInjection Allow Class::function(...) and global_function(...) closures in PHP DSL for factories [DependencyInjection Allow Class::function(...) and global_function(...) closures in PHP DSL for factories Sep 5, 2025
@GromNaN GromNaN changed the title [DependencyInjection Allow Class::function(...) and global_function(...) closures in PHP DSL for factories [DependencyInjection] Allow Class::function(...) and global_function(...) closures in PHP DSL for factories Sep 5, 2025
@GromNaN GromNaN force-pushed the factory-closure branch 2 times, most recently from 3d2d343 to b1c7a25 Compare September 5, 2025 09:08
@GromNaN GromNaN requested a review from Copilot September 5, 2025 09:08
Copy link

Copilot AI left a 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 \Closure parameters
  • 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.

Copy link
Member

@nicolas-grekas nicolas-grekas left a 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 ;)

Copy link
Member Author

@GromNaN GromNaN left a 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.

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Sep 5, 2025

src/Symfony/Component/DependencyInjection/Loader/Configurator/Traits/ConfiguratorTrait.php
src/Symfony/Component/DependencyInjection/Loader/Configurator/Traits/FromCallableTrait.php
src/Symfony/Component/DependencyInjection/Loader/Configurator/ContainerConfigurator.php (the closure function)

I'd go with a new fixture yes - to cover all these cases

@carsonbot carsonbot changed the title [DependencyInjection] Allow Class::function(...) and global_function(...) closures in PHP DSL for factories Allow Class::function(...) and global_function(...) closures in PHP DSL for factories Sep 5, 2025
@GromNaN GromNaN changed the title Allow Class::function(...) and global_function(...) closures in PHP DSL for factories [DependencyInjection] Allow Class::function(...) and global_function(...) closures in PHP DSL for factories Sep 6, 2025
}

/**
* Convert a named closure to dumpable callable.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* Convert a named closure to dumpable callable.
* Converts a named closure to dumpable callable.

@fabpot
Copy link
Member

fabpot commented Sep 6, 2025

Thank you @GromNaN.

@fabpot fabpot merged commit 75935f8 into symfony:7.4 Sep 6, 2025
9 of 12 checks passed
@GromNaN GromNaN deleted the factory-closure branch September 6, 2025 12:18
@fabpot fabpot mentioned this pull request Oct 27, 2025
@fabpot fabpot mentioned this pull request Oct 27, 2025
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.

5 participants