Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #30255 [DependencyInjection] Invokable Factory Services (zanb…
…aldwin) This PR was squashed before being merged into the 4.3-dev branch (closes #30255). Discussion ---------- [DependencyInjection] Invokable Factory Services | Q | A | ------------- | --- | Branch? | `master` | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | symfony/symfony-docs#11014 > Failing test is in the Twig bridge, and outside of the the scope of this PR. Allow referencing invokable factory services, just as route definitions reference invokable controllers. This functionality was also added for service configurators for consistency. ## Example ```php <?php namespace App\Factory; class ServiceFactory { public function __invoke(bool $debug) { return new Service($debug); } } ``` ```yaml services: App\Service: # Prepend with "@" to differentiate between service and function. factory: '@app\Factory\ServiceFactory' arguments: [ '%kernel.debug%' ] ``` ```xml <?xml version="1.0" encoding="UTF-8" ?> <container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> <services> <!-- ... --> <service id="App\Service" class="App\Service"> <factory service="App\Factory\ServiceFactory" /> </service> </services> </container> ``` ```php <?php use App\Service; use App\Factory\ServiceFactory; use Symfony\Component\DependencyInjection\Reference; $container->register(Service::class, Service::class) ->setFactory(new Reference(ServiceFactory::class)); ``` Commits ------- 23cb83f [DependencyInjection] Invokable Factory Services
- Loading branch information
Showing
10 changed files
with
40 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/bad_factory_syntax.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
services: | ||
factory: | ||
class: Baz | ||
invalid_factory: | ||
class: FooBarClass | ||
factory: '@factory:method' |
1 change: 1 addition & 0 deletions
1
src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services14.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
services: | ||
factory: { class: FooBarClass, factory: baz:getClass} | ||
factory_with_static_call: { class: FooBarClass, factory: FooBacFactory::createFooBar} | ||
invokable_factory: { class: FooBarClass, factory: '@factory' } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters