-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DependencyInjection] add factory
argument in the #[Autoconfigure]
attribute and also enabled the use of factory
within instanceof
conditionals
#60589
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
Open
santysisi
wants to merge
1
commit into
symfony:7.4
Choose a base branch
from
santysisi:feature/as-factory-attribute
base: 7.4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
22 changes: 22 additions & 0 deletions
22
...mfony/Component/DependencyInjection/Tests/Fixtures/AutoconfigureWithExpressionFactory.php
This file contains hidden or 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,22 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection\Tests\Fixtures; | ||
|
||
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure; | ||
|
||
#[Autoconfigure(factory: '@=service("factory_for_autoconfigure").create()')] | ||
class AutoconfigureWithExpressionFactory | ||
{ | ||
public function __construct(public readonly string $foo) | ||
{ | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...Component/DependencyInjection/Tests/Fixtures/AutoconfigureWithInstanceExternalFactory.php
This file contains hidden or 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,22 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection\Tests\Fixtures; | ||
|
||
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure; | ||
|
||
#[Autoconfigure(bind: ['$foo' => 'foo'], factory: ['@factory_for_autoconfigure', 'createStatic'])] | ||
class AutoconfigureWithInstanceExternalFactory | ||
{ | ||
public function __construct(public readonly string $foo) | ||
{ | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...ymfony/Component/DependencyInjection/Tests/Fixtures/AutoconfigureWithInvokableFactory.php
This file contains hidden or 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,22 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection\Tests\Fixtures; | ||
|
||
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure; | ||
|
||
#[Autoconfigure(bind: ['$foo' => 'foo'], factory: '@factory_for_autoconfigure')] | ||
class AutoconfigureWithInvokableFactory | ||
{ | ||
public function __construct(public readonly string $foo) | ||
{ | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...y/Component/DependencyInjection/Tests/Fixtures/AutoconfigureWithStaticExternalFactory.php
This file contains hidden or 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,22 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection\Tests\Fixtures; | ||
|
||
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure; | ||
|
||
#[Autoconfigure(bind: ['$foo' => 'foo'], factory: [FactoryDummy::class, 'create'])] | ||
class AutoconfigureWithStaticExternalFactory | ||
{ | ||
public function __construct(public readonly string $foo) | ||
{ | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...mfony/Component/DependencyInjection/Tests/Fixtures/AutoconfigureWithStaticSelfFactory.php
This file contains hidden or 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,27 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection\Tests\Fixtures; | ||
|
||
use Symfony\Component\DependencyInjection\Attribute\Autoconfigure; | ||
|
||
#[Autoconfigure(bind: ['$foo' => 'foo'], factory: [null, 'create'])] | ||
class AutoconfigureWithStaticSelfFactory | ||
{ | ||
public function __construct(public readonly string $foo) | ||
{ | ||
} | ||
|
||
public static function create(string $foo): static | ||
{ | ||
return new self($foo); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...mfony/Component/DependencyInjection/Tests/Fixtures/config/instanceof_factory.expected.yml
This file contains hidden or 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,16 @@ | ||
|
||
services: | ||
service_container: | ||
class: Symfony\Component\DependencyInjection\ContainerInterface | ||
public: true | ||
synthetic: true | ||
Symfony\Component\DependencyInjection\Tests\Fixtures\Bar: | ||
class: Symfony\Component\DependencyInjection\Tests\Fixtures\Bar | ||
public: true | ||
tags: | ||
- bar | ||
factory: ['@Symfony\Component\DependencyInjection\Tests\Fixtures\BarFactory', getDefaultBar] | ||
Symfony\Component\DependencyInjection\Tests\Fixtures\BarFactory: | ||
class: Symfony\Component\DependencyInjection\Tests\Fixtures\BarFactory | ||
public: true | ||
arguments: [!tagged_iterator bar] |
23 changes: 23 additions & 0 deletions
23
src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/instanceof_factory.php
This file contains hidden or 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,23 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\DependencyInjection\Loader\Configurator; | ||
|
||
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
use Symfony\Component\DependencyInjection\Tests\Fixtures\BarFactory; | ||
use Symfony\Component\DependencyInjection\Tests\Fixtures\Bar; | ||
use Symfony\Component\DependencyInjection\Tests\Fixtures\BarInterface; | ||
|
||
return function (ContainerConfigurator $container) { | ||
$services = $container->services()->defaults()->public(); | ||
|
||
$services->instanceof(BarInterface::class) | ||
->factory([new Reference(BarFactory::class), 'getDefaultBar']) | ||
->tag('bar'); | ||
|
||
$services->set(Bar::class) | ||
->public(); | ||
|
||
$services->set(BarFactory::class) | ||
->args([new TaggedIteratorArgument('bar')]); | ||
}; |
16 changes: 16 additions & 0 deletions
16
src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services_instanceof_factory.xml
This file contains hidden or 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,16 @@ | ||
<?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 https://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
<services> | ||
<instanceof id="Symfony\Component\DependencyInjection\Tests\Fixtures\BarInterface" lazy="true" autowire="true"> | ||
<factory service="Symfony\Component\DependencyInjection\Tests\Fixtures\BarFactory" method="getDefaultBar"/> | ||
<tag name="foo" /> | ||
<tag name="bar"/> | ||
</instanceof> | ||
|
||
<service id="Symfony\Component\DependencyInjection\Tests\Fixtures\Bar" public="true"/> | ||
|
||
<service id="Symfony\Component\DependencyInjection\Tests\Fixtures\BarFactory"> | ||
<argument type="tagged_iterator" tag="bar"/> | ||
</service> | ||
</services> | ||
</container> |
12 changes: 12 additions & 0 deletions
12
...Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/service_instanceof_factory2.yml
This file contains hidden or 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,12 @@ | ||
services: | ||
_instanceof: | ||
Symfony\Component\DependencyInjection\Tests\Fixtures\BarInterface: | ||
factory: ['@Symfony\Component\DependencyInjection\Tests\Fixtures\BarFactory', 'getDefaultBar'] | ||
tags: | ||
- { name: bar } | ||
|
||
Symfony\Component\DependencyInjection\Tests\Fixtures\Bar: | ||
public: true | ||
|
||
Symfony\Component\DependencyInjection\Tests\Fixtures\BarFactory: | ||
arguments: [!tagged_iterator 'bar'] |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why does this change the YamlFileLoader when the PR description talks only about attributes ?
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.
the code internally uses the YamlFileLoader to read values found in attributes
this makes me wonder about the yaml + xml + php config formats: they should also support this factory entry
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.
then we should indeed support it in all formats, and make the changelog entry (and the PR title and description) mention that explicitly.
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.
and we should also have tests preventing regressions for those other formats.
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.
Hi @stof @nicolas-grekas 👋 I hope you're both doing well! 😊
I've made the requested changes, please let me know if there's anything else you'd like me to adjust. 😊
Thanks! ❤️