Skip to content

[FrameworkBundle] Improve the sorting of tagged services #33966

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,25 @@ protected function sortTaggedServicesByPriority(array $services): array
return array_keys($maxPriority);
}

protected function sortTagsByPriority(array $tags): array
{
$sortedTags = [];
foreach ($tags as $tagName => $tag) {
$sortedTags[$tagName] = $this->sortByPriority($tag);
}

return $sortedTags;
}

protected function sortByPriority(array $tag): array
{
usort($tag, function ($a, $b) {
return ($b['priority'] ?? 0) <=> ($a['priority'] ?? 0);
});

return $tag;
}

/**
* Gets class description from a docblock.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ private function getContainerDefinitionData(Definition $definition, bool $omitTa

if (!$omitTags) {
$data['tags'] = [];
foreach ($definition->getTags() as $tagName => $tagData) {
foreach ($this->sortTagsByPriority($definition->getTags()) as $tagName => $tagData) {
foreach ($tagData as $parameters) {
$data['tags'][] = ['name' => $tagName, 'parameters' => $parameters];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ protected function describeContainerDefinition(Definition $definition, array $op
}

if (!(isset($options['omit_tags']) && $options['omit_tags'])) {
foreach ($definition->getTags() as $tagName => $tagData) {
foreach ($this->sortTagsByPriority($definition->getTags()) as $tagName => $tagData) {
foreach ($tagData as $parameters) {
$output .= "\n".'- Tag: `'.$tagName.'`';
foreach ($parameters as $name => $value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o
$styledServiceId = $rawOutput ? $serviceId : sprintf('<fg=cyan>%s</fg=cyan>', OutputFormatter::escape($serviceId));
if ($definition instanceof Definition) {
if ($showTag) {
foreach ($definition->getTag($showTag) as $key => $tag) {
foreach ($this->sortByPriority($definition->getTag($showTag)) as $key => $tag) {
$tagValues = [];
foreach ($tagsNames as $tagName) {
$tagValues[] = isset($tag[$tagName]) ? $tag[$tagName] : '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ private function getContainerDefinitionDocument(Definition $definition, string $
}

if (!$omitTags) {
if ($tags = $definition->getTags()) {
if ($tags = $this->sortTagsByPriority($definition->getTags())) {
$serviceXML->appendChild($tagsXML = $dom->createElement('tags'));
foreach ($tags as $tagName => $tagData) {
foreach ($tagData as $parameters) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
{
"name": "tag1",
"parameters": {
"attr1": "val1",
"attr2": "val2",
"priority": 0
"attr3": "val3",
"priority": 40
}
},
{
"name": "tag1",
"parameters": {
"attr3": "val3",
"priority": 40
"attr1": "val1",
"attr2": "val2",
"priority": 0
}
}
]
Expand Down Expand Up @@ -87,4 +87,4 @@
},
"aliases": [],
"services": []
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ Definitions
- Autowired: no
- Autoconfigured: no
- File: `/path/to/file`
- Tag: `tag1`
- Attr3: val3
- Priority: 40
- Tag: `tag1`
- Attr1: val1
- Attr2: val2
- Priority: 0
- Tag: `tag1`
- Attr3: val3
- Priority: 40

### definition_1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
-------------- ------- ------- ---------- ------- -----------------------
 Service ID   attr1   attr2   priority   attr3   Class name 
-------------- ------- ------- ---------- ------- -----------------------
definition_3 val1 val2 0 Full\Qualified\Class3
" 40 val3
definition_3 40 val3 Full\Qualified\Class3
" val1 val2 0
definition_1 val1 30 Full\Qualified\Class1
" val2
definition_2 val1 val2 -20 Full\Qualified\Class2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
<container>
<definition id="definition_3" class="Full\Qualified\Class3" public="true" synthetic="true" lazy="false" shared="true" abstract="false" autowired="false" autoconfigured="false" file="/path/to/file">
<tags>
<tag name="tag1">
<parameter name="attr3">val3</parameter>
<parameter name="priority">40</parameter>
</tag>
<tag name="tag1">
<parameter name="attr1">val1</parameter>
<parameter name="attr2">val2</parameter>
<parameter name="priority">0</parameter>
</tag>
<tag name="tag1">
<parameter name="attr3">val3</parameter>
<parameter name="priority">40</parameter>
</tag>
</tags>
</definition>
<definition id="definition_1" class="Full\Qualified\Class1" public="true" synthetic="true" lazy="false" shared="true" abstract="false" autowired="false" autoconfigured="false" file="/path/to/file">
Expand Down