Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[DependencyInjection] Allow array attributes for service tags#38540

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed

Conversation

@aschempp
Copy link
Contributor

QA
Branch?5.x
Bug fix?no
New feature?yes
Deprecations?no
TicketsFix#38339
LicenseMIT
Doc PRsymfony/symfony-docs#...

This adds array attribute support for container service tags as described in#38339. I'm not sure I catched all places this would apply. The most notable change is the additions to the XML schema for services, because that's the current limitation for not using array.

I created this as draft PR because I'm very well aware the docs and tests need to be update. But please let me know if this sounds like a valid feature to add and if the approach seems reasonable or if I missed something 🙃


With this PR, you can now define service tags like this:

PHP:

$container->services()        ->set('foo', BarClass::class)            ->public()            ->tag('foo_tag',                 ['some_option' =>'cat','array_option' => ['key1' =>'lorem','key2' =>'ipsum'                    ]                ]            );

XML:

<?xml version="1.0" ?><containerxmlns="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>    <serviceid="foo"class="BarClass"public="true">        <tagname="foo_tag"some-option="cat">            <attributename="array_option">                <attributename="key1">lorem</attribute>                <attributename="key2">ipsum</attribute>            </attribute>        </tag>    </service>  </services></container>

YAML:

services:foo:class:BarClasstags:            -{ name: 'foo_tag', 'some-option': 'cat', array_option: [key1: lorem, key2: ipsum] }

As described in#38339, our service definition might look like this now:

services:Vendor\Bundle\Controller\FooController:public:truetags:            -{ name: 'contao.page', path: 'foo/{id}', requirements: [ id: '\d+' ] }

ju1ius reacted with heart emoji
@aschempp
Copy link
ContributorAuthor

any update/feedback/opinions on this PR? 🙃

Copy link
Member

@nicolas-grekasnicolas-grekas left a comment

Choose a reason for hiding this comment

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

I like it. What's missing:

  • corresponding updates to the dumpers
  • tests

}

$parameters =$this->getRecursiveTagAttributes($tag,sprintf('The attribute name of tag "%s" for service "%s" in %s must be a non-empty string.',$tagName, (string)$service->getAttribute('id'),$file));
foreach ($tag->attributesas$name =>$node) {

Choose a reason for hiding this comment

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

I think something is missing here, that would allow removing this foreach. Right now, this looks like duplication with the new method.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

The foreach loop is to handle attributes on the tag itself, the method handles nested arguments.

Example in the initial comment:

<?xml version="1.0" ?><containerxmlns="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>    <serviceid="foo"class="BarClass"public="true">        <tagname="foo_tag"some-option="cat">            <attributename="array_option">                <attributename="key1">lorem</attribute>                <attributename="key2">ipsum</attribute>            </attribute>        </tag>    </service>  </services></container>

The foreach would readsome-option while the method recursively reads<atttribute …>

@nicolas-grekas
Copy link
Member

Friendly ping@aschempp

@aschempp
Copy link
ContributorAuthor

Sorry I didn't have time yet to update this. I'll give it a try in the coming weeks.

@aschemppaschemppforce-pushed thefeature/tag-array-attributes branch from24eae57 toaeb2ce1CompareJuly 15, 2021 07:26
@aschemppaschempp marked this pull request as ready for reviewJuly 15, 2021 07:36
@aschempp
Copy link
ContributorAuthor

I've updated the PR, added some text and fixed major bugs 🙈
The failing PHP8 tests seem unrelated to me.

@aschempp
Copy link
ContributorAuthor

Friendly ping@nicolas-grekas 😅

@carsonbotcarsonbot changed the titleAllow array attributes for service tags[DependencyInjection] Allow array attributes for service tagsOct 14, 2021
@nicolas-grekas
Copy link
Member

How do the dumpers behave with array as tag attributes? Can you please add some tests for that?

@aschempp
Copy link
ContributorAuthor

I'm sorry I might not be familiar with that concept. What "Dumpers" are you referring to?

@ruudk
Copy link
Contributor

ruudk commentedNov 6, 2021
edited
Loading

First of all, thank you for working on this feature. It's something that I really struggle with myself and I hope this will be possible one day 🙏

To answer your question,@nicolas-grekas talks about dumpers likeSymfony\Component\DependencyInjection\Dumper\XmlDumper. They are used to dump the compiled container to thevar/cache directory. (If you would try this on a real application/project you would see an error too)

As far as I can see your PR only makes it possible to have array tags in configurations (read side). But the PR should also fix the write side.

To solve that, add a test case toSymfony\Component\DependencyInjection\Tests\Dumper\XmlDumperTest:

publicfunctiontestNonScalarTags()    {$container =includeself::$fixturesPath.'/containers/container_non_scalar_tags.php';$dumper =newXmlDumper($container);$this->assertEquals(file_get_contents(self::$fixturesPath.'/xml/services_with_array_tags.xml'),$dumper->dump());    }

and create a fixture like this:

<?phprequire_once__DIR__.'/../includes/classes.php';require_once__DIR__.'/../includes/foo.php';useBar\FooClass;useSymfony\Component\DependencyInjection\ContainerBuilder;$container =newContainerBuilder();$container    ->register('foo', FooClass::class)    ->addTag('foo_tag', ['foo' =>'bar','bar' => ['foo' =>'bar','baz' =>'foo'        ]]);return$container;

When you run it, you'll see that the Dumper breaks:

1) Symfony\Component\DependencyInjection\Tests\Dumper\XmlDumperTest::testNonScalarTagsTypeError: DOMElement::setAttribute(): Argument #2 ($value) must be of type string, array given/Users/Ruud/opensource/symfony/src/Symfony/Component/DependencyInjection/Dumper/XmlDumper.php:147/Users/Ruud/opensource/symfony/src/Symfony/Component/DependencyInjection/Dumper/XmlDumper.php:272

That foreach should be altered to support arrays and write the XML in the new format.

Hope it helps 😊

aschempp and nicolas-grekas reacted with thumbs up emoji

@nicolas-grekasnicolas-grekas modified the milestones:5.4,6.1Nov 12, 2021
@nicolas-grekas
Copy link
Member

@aschempp up to finish this PR?

@fabpotfabpot modified the milestones:6.1,6.2May 20, 2022
@nicolas-grekas
Copy link
Member

I'm closing here because this staled and also because on second though I'm not sure it will work smoothly. Also because re-reading the original issue, turning tags into annotations doesn't feel like a good idea to me, at least not without using the autoconfiguration mechanism.registerAttributeForAutoconfiguration() provides a way to do that natively. This provides a hook that would allow serializing complex structures found in the attribute into eg json, so that the description can fit a tag.

Closing as such, thanks for proposing!

@aschempp
Copy link
ContributorAuthor

Sorry for being stale, I was just about to update that (started yesterday after finishing#42355). I just updated the PR but it seems GH does not update here if the PR is closed.

Not exactly sure what you mean byturning tags into annotations. The change simply allows for recursive tag attributes. A compiler pass can then use this information to do whatever they want.
In our case, we have tags to define a service as a controller, and the route configuration has a name, path and an array of requirements etc., hence the requirement for an array of tag attributes.

@nicolas-grekas
Copy link
Member

I meant turning annotations into tags sorry (withthis).
How do you do right now? Wouldn't my suggestion above work for you?

@aschempp
Copy link
ContributorAuthor

I meant turning annotations into tags sorry (withthis).

Yeah that's basically a predecessor of the PHP8 attributes we've had for a few years now 😎 (although it is obviously deprecated now that we have attributes)

How do you do right now? Wouldn't my suggestion above work for you?

If I remember correctly, both using our annotations as well as our newly added attributes works fine. Even though attributes/annotations are converted to tags, there is no validation on them not being arrays. It just does not work when trying to define the same in YAML or XML.

@nicolas-grekas
Copy link
Member

Even though attributes/annotations are converted to tags, there is no validation on them not being arrays. It just does not work when trying to define the same in YAML or XML.

Oh, and how does the XML dump look like?

I can't reopen but please resubmit if you want.

@aschempp
Copy link
ContributorAuthor

XML will look like this 😊
https://github.com/symfony/symfony/pull/38540/files#diff-16371727bd7f17a541f80dec3603214facfb14f0848930e8567c54c0a06eae8b

ruudk reacted with thumbs up emoji

@aschempp
Copy link
ContributorAuthor

see#47364 for the reopened PR

fabpot added a commit that referenced this pull requestOct 24, 2022
…ce tags (aschempp)This PR was merged into the 6.2 branch.Discussion----------[DependencyInjection] Allow array attributes for service tags| Q             | A| ------------- | ---| Branch?       | 6.2| Bug fix?      | no| New feature?  | yes| Deprecations? | no| Tickets       |Fix#38339,#38540| License       | MIT| Doc PR        | symfony/symfony-docs#... <!-- required for new features -->This adds array attribute support for container service tags as described in#38339. The most notable change is the additions to the XML schema for services, because that's the current limitation for not using array.This is reopening#38540 as new PR since the other one was ("accidentially") closed 😊/cc `@nicolas`-grekasCommits-------edd8d77 [DependencyInjection] Allow array attributes for service tags
symfony-splitter pushed a commit to symfony/dependency-injection that referenced this pull requestOct 24, 2022
…ce tags (aschempp)This PR was merged into the 6.2 branch.Discussion----------[DependencyInjection] Allow array attributes for service tags| Q             | A| ------------- | ---| Branch?       | 6.2| Bug fix?      | no| New feature?  | yes| Deprecations? | no| Tickets       |Fixsymfony/symfony#38339,symfony/symfony#38540| License       | MIT| Doc PR        | symfony/symfony-docs#... <!-- required for new features -->This adds array attribute support for container service tags as described insymfony/symfony#38339. The most notable change is the additions to the XML schema for services, because that's the current limitation for not using array.This is reopeningsymfony/symfony#38540 as new PR since the other one was ("accidentially") closed 😊/cc `@nicolas`-grekasCommits-------edd8d776a0 [DependencyInjection] Allow array attributes for service tags
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@nicolas-grekasnicolas-grekasnicolas-grekas left review comments

Assignees

No one assigned

Projects

None yet

Milestone

6.2

Development

Successfully merging this pull request may close these issues.

Support for non-scalar tag attributes

6 participants

@aschempp@nicolas-grekas@ruudk@fabpot@derrabus@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp