-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
any update/feedback/opinions on this PR? 🙃 |
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.
I like it. What's missing:
- corresponding updates to the dumpers
- tests
src/Symfony/Component/DependencyInjection/Loader/Configurator/DefaultsConfigurator.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php
Outdated
Show resolved
Hide resolved
throw new InvalidArgumentException(sprintf('The tag name for service "%s" in "%s" must be a non-empty string.', (string) $service->getAttribute('id'), $file)); | ||
} | ||
|
||
$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->attributes as $name => $node) { |
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.
I think something is missing here, that would allow removing this foreach. Right now, this looks like duplication with the new method.
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 foreach loop is to handle attributes on the tag itself, the method handles nested arguments.
Example in the initial comment:
<?xml version="1.0" ?>
<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>
<service id="foo" class="BarClass" public="true">
<tag name="foo_tag" some-option="cat">
<attribute name="array_option">
<attribute name="key1">lorem</attribute>
<attribute name="key2">ipsum</attribute>
</attribute>
</tag>
</service>
</services>
</container>
The foreach would read some-option
while the method recursively reads <atttribute …>
Friendly ping @aschempp |
Sorry I didn't have time yet to update this. I'll give it a try in the coming weeks. |
24eae57
to
aeb2ce1
Compare
I've updated the PR, added some text and fixed major bugs 🙈 |
Friendly ping @nicolas-grekas 😅 |
How do the dumpers behave with array as tag attributes? Can you please add some tests for that? |
I'm sorry I might not be familiar with that concept. What "Dumpers" are you referring to? |
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 like 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 to public function testNonScalarTags()
{
$container = include self::$fixturesPath.'/containers/container_non_scalar_tags.php';
$dumper = new XmlDumper($container);
$this->assertEquals(file_get_contents(self::$fixturesPath.'/xml/services_with_array_tags.xml'), $dumper->dump());
} and create a fixture like this: <?php
require_once __DIR__.'/../includes/classes.php';
require_once __DIR__.'/../includes/foo.php';
use Bar\FooClass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
$container = new ContainerBuilder();
$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:
That foreach should be altered to support arrays and write the XML in the new format. Hope it helps 😊 |
@aschempp up to finish this PR? |
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. Closing as such, thanks for proposing! |
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 by turning tags into annotations. The change simply allows for recursive tag attributes. A compiler pass can then use this information to do whatever they want. |
I meant turning annotations into tags sorry (with this). |
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)
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. |
Oh, and how does the XML dump look like? I can't reopen but please resubmit if you want. |
see #47364 for the reopened PR |
…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`-grekas Commits ------- edd8d77 [DependencyInjection] Allow array attributes for service tags
…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 symfony/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 in symfony/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 reopening symfony/symfony#38540 as new PR since the other one was ("accidentially") closed 😊 /cc `@nicolas`-grekas Commits ------- edd8d776a0 [DependencyInjection] Allow array attributes for service tags
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:
XML:
YAML:
As described in #38339, our service definition might look like this now: