Skip to content

Commit

Permalink
[FrameworkBundle] Handle tags array attributes in descriptors
Browse files Browse the repository at this point in the history
  • Loading branch information
fancyweb committed Sep 15, 2023
1 parent f822f54 commit d864ddb
Show file tree
Hide file tree
Showing 25 changed files with 248 additions and 66 deletions.
2 changes: 1 addition & 1 deletion Console/Descriptor/MarkdownDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ protected function describeContainerDefinition(Definition $definition, array $op
foreach ($tagData as $parameters) {
$output .= "\n".'- Tag: `'.$tagName.'`';
foreach ($parameters as $name => $value) {
$output .= "\n".' - '.ucfirst($name).': '.$value;
$output .= "\n".' - '.ucfirst($name).': '.(\is_array($value) ? $this->formatParameter($value) : $value);
}
}
}
Expand Down
12 changes: 10 additions & 2 deletions Console/Descriptor/TextDescriptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ protected function describeContainerServices(ContainerBuilder $container, array
if (!isset($maxTags[$key])) {
$maxTags[$key] = \strlen($key);
}
if (\is_array($value)) {
$value = $this->formatParameter($value);
}

if (\strlen($value) > $maxTags[$key]) {
$maxTags[$key] = \strlen($value);
}
Expand All @@ -233,7 +237,11 @@ protected function describeContainerServices(ContainerBuilder $container, array
foreach ($this->sortByPriority($definition->getTag($showTag)) as $key => $tag) {
$tagValues = [];
foreach ($tagsNames as $tagName) {
$tagValues[] = $tag[$tagName] ?? '';
if (\is_array($tagValue = $tag[$tagName] ?? '')) {
$tagValue = $this->formatParameter($tagValue);
}

$tagValues[] = $tagValue;
}
if (0 === $key) {
$tableRows[] = array_merge([$serviceId], $tagValues, [$definition->getClass()]);
Expand Down Expand Up @@ -275,7 +283,7 @@ protected function describeContainerDefinition(Definition $definition, array $op
$tagInformation = [];
foreach ($tags as $tagName => $tagData) {
foreach ($tagData as $tagParameters) {
$parameters = array_map(fn ($key, $value) => sprintf('<info>%s</info>: %s', $key, $value), array_keys($tagParameters), array_values($tagParameters));
$parameters = array_map(fn ($key, $value) => sprintf('<info>%s</info>: %s', $key, \is_array($value) ? $this->formatParameter($value) : $value), array_keys($tagParameters), array_values($tagParameters));
$parameters = implode(', ', $parameters);

if ('' === $parameters) {
Expand Down
1 change: 1 addition & 0 deletions Tests/Console/Descriptor/ObjectsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ public static function getContainerDefinitions()
->addTag('tag1', ['attr1' => 'val1', 'attr2' => 'val2'])
->addTag('tag1', ['attr3' => 'val3'])
->addTag('tag2')
->addTag('tag3', ['array_attr' => ['foo', 'bar', [[[['ccc']]]]]])
->addMethodCall('setMailer', [new Reference('mailer')])
->setFactory([new Reference('factory.service'), 'get']),
'.definition_3' => $definition3
Expand Down
18 changes: 18 additions & 0 deletions Tests/Fixtures/Descriptor/alias_with_definition_2.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@
{
"name": "tag2",
"parameters": []
},
{
"name": "tag3",
"parameters": {
"array_attr": [
"foo",
"bar",
[
[
[
[
"ccc"
]
]
]
]
]
}
}
],
"usages": [
Expand Down
2 changes: 2 additions & 0 deletions Tests/Fixtures/Descriptor/alias_with_definition_2.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@
- Tag: `tag1`
- Attr3: val3
- Tag: `tag2`
- Tag: `tag3`
- Array_attr: ["foo","bar",[[[["ccc"]]]]]
- Usages: .alias_2
44 changes: 23 additions & 21 deletions Tests/Fixtures/Descriptor/alias_with_definition_2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,26 @@
Information for Service ".service_2"
====================================

----------------- ---------------------------------
 Option   Value 
----------------- ---------------------------------
Service ID .service_2
Class Full\Qualified\Class2
Tags tag1 (attr1: val1, attr2: val2)
tag1 (attr3: val3)
tag2
Calls setMailer
Public no
Synthetic yes
Lazy no
Shared yes
Abstract no
Autowired no
Autoconfigured no
Required File /path/to/file
Factory Service factory.service
Factory Method get
Usages .alias_2
----------------- ---------------------------------
----------------- ------------------------------------------------
 Option   Value 
----------------- ------------------------------------------------
Service ID .service_2
Class Full\Qualified\Class2
Tags tag1 (attr1: val1, attr2: val2)
tag1 (attr3: val3)
tag2
tag3 (array_attr: ["foo","bar",[[[["ccc"]]]]])
Calls setMailer
Public no
Synthetic yes
Lazy no
Shared yes
Abstract no
Autowired no
Autoconfigured no
Required File /path/to/file
Factory Service factory.service
Factory Method get
Usages .alias_2
----------------- ------------------------------------------------

3 changes: 3 additions & 0 deletions Tests/Fixtures/Descriptor/alias_with_definition_2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
<parameter name="attr3">val3</parameter>
</tag>
<tag name="tag2"/>
<tag name="tag3">
<parameter name="array_attr">["foo","bar",[[[["ccc"]]]]]</parameter>
</tag>
</tags>
<usages>
<usage>.alias_2</usage>
Expand Down
18 changes: 18 additions & 0 deletions Tests/Fixtures/Descriptor/builder_1_services.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@
{
"name": "tag2",
"parameters": []
},
{
"name": "tag3",
"parameters": {
"array_attr": [
"foo",
"bar",
[
[
[
[
"ccc"
]
]
]
]
]
}
}
],
"usages": []
Expand Down
2 changes: 2 additions & 0 deletions Tests/Fixtures/Descriptor/builder_1_services.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Definitions
- Tag: `tag1`
- Attr3: val3
- Tag: `tag2`
- Tag: `tag3`
- Array_attr: ["foo","bar",[[[["ccc"]]]]]
- Usages: none

### .definition_3
Expand Down
3 changes: 3 additions & 0 deletions Tests/Fixtures/Descriptor/builder_1_services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
<parameter name="attr3">val3</parameter>
</tag>
<tag name="tag2"/>
<tag name="tag3">
<parameter name="array_attr">["foo","bar",[[[["ccc"]]]]]</parameter>
</tag>
</tags>
</definition>
<definition id=".definition_3" class="Full\Qualified\Class3" public="false" synthetic="false" lazy="false" shared="true" abstract="false" autowired="false" autoconfigured="false" deprecated="false" file="/path/to/file">
Expand Down
18 changes: 18 additions & 0 deletions Tests/Fixtures/Descriptor/builder_1_tag1.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@
{
"name": "tag2",
"parameters": []
},
{
"name": "tag3",
"parameters": {
"array_attr": [
"foo",
"bar",
[
[
[
[
"ccc"
]
]
]
]
]
}
}
],
"usages": []
Expand Down
2 changes: 2 additions & 0 deletions Tests/Fixtures/Descriptor/builder_1_tag1.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ Definitions
- Tag: `tag1`
- Attr3: val3
- Tag: `tag2`
- Tag: `tag3`
- Array_attr: ["foo","bar",[[[["ccc"]]]]]
- Usages: none
3 changes: 3 additions & 0 deletions Tests/Fixtures/Descriptor/builder_1_tag1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
<parameter name="attr3">val3</parameter>
</tag>
<tag name="tag2"/>
<tag name="tag3">
<parameter name="array_attr">["foo","bar",[[[["ccc"]]]]]</parameter>
</tag>
</tags>
</definition>
</container>
20 changes: 20 additions & 0 deletions Tests/Fixtures/Descriptor/builder_1_tags.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,25 @@
],
"usages": []
}
],
"tag3": [
{
"class": "Full\\Qualified\\Class2",
"public": false,
"synthetic": true,
"lazy": false,
"shared": true,
"abstract": false,
"autowire": false,
"autoconfigure": false,
"deprecated": false,
"file": "\/path\/to\/file",
"factory_service": "factory.service",
"factory_method": "get",
"calls": [
"setMailer"
],
"usages": []
}
]
}
21 changes: 21 additions & 0 deletions Tests/Fixtures/Descriptor/builder_1_tags.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,24 @@ tag2
- Factory Method: `get`
- Call: `setMailer`
- Usages: none


tag3
----

### .definition_2

- Class: `Full\Qualified\Class2`
- Public: no
- Synthetic: yes
- Lazy: no
- Shared: yes
- Abstract: no
- Autowired: no
- Autoconfigured: no
- Deprecated: no
- File: `/path/to/file`
- Factory Service: `factory.service`
- Factory Method: `get`
- Call: `setMailer`
- Usages: none
5 changes: 5 additions & 0 deletions Tests/Fixtures/Descriptor/builder_1_tags.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@

* .definition_2

"tag3" tag
----------

* .definition_2

8 changes: 8 additions & 0 deletions Tests/Fixtures/Descriptor/builder_1_tags.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,12 @@
</calls>
</definition>
</tag>
<tag name="tag3">
<definition id=".definition_2" class="Full\Qualified\Class2" public="false" synthetic="true" lazy="false" shared="true" abstract="false" autowired="false" autoconfigured="false" deprecated="false" file="/path/to/file">
<factory service="factory.service" method="get"/>
<calls>
<call method="setMailer"/>
</calls>
</definition>
</tag>
</container>
18 changes: 18 additions & 0 deletions Tests/Fixtures/Descriptor/definition_2.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@
{
"name": "tag2",
"parameters": []
},
{
"name": "tag3",
"parameters": {
"array_attr": [
"foo",
"bar",
[
[
[
[
"ccc"
]
]
]
]
]
}
}
],
"usages": []
Expand Down
2 changes: 2 additions & 0 deletions Tests/Fixtures/Descriptor/definition_2.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@
- Tag: `tag1`
- Attr3: val3
- Tag: `tag2`
- Tag: `tag3`
- Array_attr: ["foo","bar",[[[["ccc"]]]]]
- Usages: none
43 changes: 22 additions & 21 deletions Tests/Fixtures/Descriptor/definition_2.txt
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
----------------- ---------------------------------
 Option   Value 
----------------- ---------------------------------
Service ID -
Class Full\Qualified\Class2
Tags tag1 (attr1: val1, attr2: val2)
tag1 (attr3: val3)
tag2
Calls setMailer
Public no
Synthetic yes
Lazy no
Shared yes
Abstract no
Autowired no
Autoconfigured no
Required File /path/to/file
Factory Service factory.service
Factory Method get
Usages none
----------------- ---------------------------------
----------------- ------------------------------------------------
 Option   Value 
----------------- ------------------------------------------------
Service ID -
Class Full\Qualified\Class2
Tags tag1 (attr1: val1, attr2: val2)
tag1 (attr3: val3)
tag2
tag3 (array_attr: ["foo","bar",[[[["ccc"]]]]])
Calls setMailer
Public no
Synthetic yes
Lazy no
Shared yes
Abstract no
Autowired no
Autoconfigured no
Required File /path/to/file
Factory Service factory.service
Factory Method get
Usages none
----------------- ------------------------------------------------

3 changes: 3 additions & 0 deletions Tests/Fixtures/Descriptor/definition_2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@
<parameter name="attr3">val3</parameter>
</tag>
<tag name="tag2"/>
<tag name="tag3">
<parameter name="array_attr">["foo","bar",[[[["ccc"]]]]]</parameter>
</tag>
</tags>
</definition>
Loading

0 comments on commit d864ddb

Please sign in to comment.