Skip to content

[FrameworkBundle] Sort tagged services #33128

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 1 commit into from
Sep 25, 2019
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 @@ -267,6 +267,25 @@ protected function sortServiceIds(array $serviceIds)
return $serviceIds;
}

protected function sortTaggedServicesByPriority(array $services): array
{
$maxPriority = [];
foreach ($services as $service => $tags) {
$maxPriority[$service] = 0;
foreach ($tags as $tag) {
$currentPriority = $tag['priority'] ?? 0;
if ($maxPriority[$service] < $currentPriority) {
$maxPriority[$service] = $currentPriority;
}
}
}
uasort($maxPriority, function ($a, $b) {
return $b <=> $a;
});

return array_keys($maxPriority);
}

/**
* Gets class description from a docblock.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ protected function describeContainerService($service, array $options = [], Conta
*/
protected function describeContainerServices(ContainerBuilder $builder, array $options = [])
{
$serviceIds = isset($options['tag']) && $options['tag'] ? array_keys($builder->findTaggedServiceIds($options['tag'])) : $builder->getServiceIds();
$serviceIds = isset($options['tag']) && $options['tag']
? $this->sortTaggedServicesByPriority($builder->findTaggedServiceIds($options['tag']))
: $this->sortServiceIds($builder->getServiceIds());
$showHidden = isset($options['show_hidden']) && $options['show_hidden'];
$omitTags = isset($options['omit_tags']) && $options['omit_tags'];
$showArguments = isset($options['show_arguments']) && $options['show_arguments'];
Expand All @@ -110,7 +112,7 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o
$serviceIds = array_filter($serviceIds, $options['filter']);
}

foreach ($this->sortServiceIds($serviceIds) as $serviceId) {
foreach ($serviceIds as $serviceId) {
$service = $this->resolveServiceDefinition($builder, $serviceId);

if ($showHidden xor '.' === ($serviceId[0] ?? null)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,17 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o
}
$this->write($title."\n".str_repeat('=', \strlen($title)));

$serviceIds = isset($options['tag']) && $options['tag'] ? array_keys($builder->findTaggedServiceIds($options['tag'])) : $builder->getServiceIds();
$serviceIds = isset($options['tag']) && $options['tag']
? $this->sortTaggedServicesByPriority($builder->findTaggedServiceIds($options['tag']))
: $this->sortServiceIds($builder->getServiceIds());
$showArguments = isset($options['show_arguments']) && $options['show_arguments'];
$services = ['definitions' => [], 'aliases' => [], 'services' => []];

if (isset($options['filter'])) {
$serviceIds = array_filter($serviceIds, $options['filter']);
}

foreach ($this->sortServiceIds($serviceIds) as $serviceId) {
foreach ($serviceIds as $serviceId) {
$service = $this->resolveServiceDefinition($builder, $serviceId);

if ($showHidden xor '.' === ($serviceId[0] ?? null)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o

$options['output']->title($title);

$serviceIds = isset($options['tag']) && $options['tag'] ? array_keys($builder->findTaggedServiceIds($options['tag'])) : $builder->getServiceIds();
$serviceIds = isset($options['tag']) && $options['tag']
? $this->sortTaggedServicesByPriority($builder->findTaggedServiceIds($options['tag']))
: $this->sortServiceIds($builder->getServiceIds());
$maxTags = [];

if (isset($options['filter'])) {
Expand Down Expand Up @@ -230,7 +232,7 @@ protected function describeContainerServices(ContainerBuilder $builder, array $o
$tableHeaders = array_merge(['Service ID'], $tagsNames, ['Class name']);
$tableRows = [];
$rawOutput = isset($options['raw_text']) && $options['raw_text'];
foreach ($this->sortServiceIds($serviceIds) as $serviceId) {
foreach ($serviceIds as $serviceId) {
$definition = $this->resolveServiceDefinition($builder, $serviceId);

$styledServiceId = $rawOutput ? $serviceId : sprintf('<fg=cyan>%s</fg=cyan>', OutputFormatter::escape($serviceId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,13 +289,14 @@ private function getContainerServicesDocument(ContainerBuilder $builder, string
$dom = new \DOMDocument('1.0', 'UTF-8');
$dom->appendChild($containerXML = $dom->createElement('container'));

$serviceIds = $tag ? array_keys($builder->findTaggedServiceIds($tag)) : $builder->getServiceIds();

$serviceIds = $tag
? $this->sortTaggedServicesByPriority($builder->findTaggedServiceIds($tag))
: $this->sortServiceIds($builder->getServiceIds());
if ($filter) {
$serviceIds = array_filter($serviceIds, $filter);
}

foreach ($this->sortServiceIds($serviceIds) as $serviceId) {
foreach ($serviceIds as $serviceId) {
$service = $this->resolveServiceDefinition($builder, $serviceId);

if ($showHidden xor '.' === ($serviceId[0] ?? null)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,4 +287,25 @@ private function getEventDispatcherDescriptionTestData(array $objects)

return $data;
}

/** @dataProvider getDescribeContainerBuilderWithPriorityTagsTestData */
public function testDescribeContainerBuilderWithPriorityTags(ContainerBuilder $builder, $expectedDescription, array $options): void
{
$this->assertDescription($expectedDescription, $builder, $options);
}

public function getDescribeContainerBuilderWithPriorityTagsTestData(): array
{
$variations = ['priority_tag' => ['tag' => 'tag1']];
$data = [];
foreach (ObjectsProvider::getContainerBuildersWithPriorityTags() as $name => $object) {
foreach ($variations as $suffix => $options) {
$file = sprintf('%s_%s.%s', trim($name, '.'), $suffix, $this->getFormat());
$description = file_get_contents(__DIR__.'/../../Fixtures/Descriptor/'.$file);
$data[] = [$object, $description, $options, $file];
}
}

return $data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,50 @@ public static function getContainerDefinitions()
];
}

public static function getContainerBuildersWithPriorityTags()
{
$builder = new ContainerBuilder();
$builder->setDefinitions(self::getContainerDefinitionsWithPriorityTags());

return ['builder' => $builder];
}

public static function getContainerDefinitionsWithPriorityTags()
{
$definition1 = new Definition('Full\\Qualified\\Class1');
$definition2 = new Definition('Full\\Qualified\\Class2');
$definition3 = new Definition('Full\\Qualified\\Class3');

return [
'definition_1' => $definition1
->setPublic(true)
->setSynthetic(true)
->setFile('/path/to/file')
->setLazy(false)
->setAbstract(false)
->addTag('tag1', ['attr1' => 'val1', 'priority' => 30])
->addTag('tag1', ['attr2' => 'val2'])
->addTag('tag2')
->addMethodCall('setMailer', [new Reference('mailer')])
->setFactory([new Reference('factory.service'), 'get']),
'definition_2' => $definition2
->setPublic(true)
->setSynthetic(true)
->setFile('/path/to/file')
->setLazy(false)
->setAbstract(false)
->addTag('tag1', ['attr1' => 'val1', 'attr2' => 'val2', 'priority' => -20]),
'definition_3' => $definition3
->setPublic(true)
->setSynthetic(true)
->setFile('/path/to/file')
->setLazy(false)
->setAbstract(false)
->addTag('tag1', ['attr1' => 'val1', 'attr2' => 'val2', 'priority' => 0])
->addTag('tag1', ['attr3' => 'val3', 'priority' => 40]),
];
}

public static function getContainerAliases()
{
return [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
{
"definitions": {
"definition_3": {
"class": "Full\\Qualified\\Class3",
"public": true,
"synthetic": true,
"lazy": false,
"shared": true,
"abstract": false,
"autowire": false,
"autoconfigure": false,
"file": "\/path\/to\/file",
"tags": [
{
"name": "tag1",
"parameters": {
"attr1": "val1",
"attr2": "val2",
"priority": 0
}
},
{
"name": "tag1",
"parameters": {
"attr3": "val3",
"priority": 40
}
}
]
},
"definition_1": {
"class": "Full\\Qualified\\Class1",
"public": true,
"synthetic": true,
"lazy": false,
"shared": true,
"abstract": false,
"autowire": false,
"autoconfigure": false,
"file": "\/path\/to\/file",
"factory_service": "factory.service",
"factory_method": "get",
"calls": [
"setMailer"
],
"tags": [
{
"name": "tag1",
"parameters": {
"attr1": "val1",
"priority": 30
}
},
{
"name": "tag1",
"parameters": {
"attr2": "val2"
}
},
{
"name": "tag2",
"parameters": []
}
]
},
"definition_2": {
"class": "Full\\Qualified\\Class2",
"public": true,
"synthetic": true,
"lazy": false,
"shared": true,
"abstract": false,
"autowire": false,
"autoconfigure": false,
"file": "\/path\/to\/file",
"tags": [
{
"name": "tag1",
"parameters": {
"attr1": "val1",
"attr2": "val2",
"priority": -20
}
}
]
}
},
"aliases": [],
"services": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
Services with tag `tag1`
========================

Definitions
-----------

### definition_3

- Class: `Full\Qualified\Class3`
- Public: yes
- Synthetic: yes
- Lazy: no
- Shared: yes
- Abstract: no
- Autowired: no
- Autoconfigured: no
- File: `/path/to/file`
- Tag: `tag1`
- Attr1: val1
- Attr2: val2
- Priority: 0
- Tag: `tag1`
- Attr3: val3
- Priority: 40

### definition_1

- Class: `Full\Qualified\Class1`
- Public: yes
- Synthetic: yes
- Lazy: no
- Shared: yes
- Abstract: no
- Autowired: no
- Autoconfigured: no
- File: `/path/to/file`
- Factory Service: `factory.service`
- Factory Method: `get`
- Call: `setMailer`
- Tag: `tag1`
- Attr1: val1
- Priority: 30
- Tag: `tag1`
- Attr2: val2
- Tag: `tag2`

### definition_2

- Class: `Full\Qualified\Class2`
- Public: yes
- Synthetic: yes
- Lazy: no
- Shared: yes
- Abstract: no
- Autowired: no
- Autoconfigured: no
- File: `/path/to/file`
- Tag: `tag1`
- Attr1: val1
- Attr2: val2
- Priority: -20
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

Symfony Container Services Tagged with "tag1" Tag
=================================================

-------------- ------- ------- ---------- ------- -----------------------
 Service ID   attr1   attr2   priority   attr3   Class name 
-------------- ------- ------- ---------- ------- -----------------------
definition_3 val1 val2 0 Full\Qualified\Class3
" 40 val3
definition_1 val1 30 Full\Qualified\Class1
" val2
definition_2 val1 val2 -20 Full\Qualified\Class2
-------------- ------- ------- ---------- ------- -----------------------

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<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="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">
<factory service="factory.service" method="get"/>
<calls>
<call method="setMailer"/>
</calls>
<tags>
<tag name="tag1">
<parameter name="attr1">val1</parameter>
<parameter name="priority">30</parameter>
</tag>
<tag name="tag1">
<parameter name="attr2">val2</parameter>
</tag>
<tag name="tag2"/>
</tags>
</definition>
<definition id="definition_2" class="Full\Qualified\Class2" public="true" synthetic="true" lazy="false" shared="true" abstract="false" autowired="false" autoconfigured="false" file="/path/to/file">
<tags>
<tag name="tag1">
<parameter name="attr1">val1</parameter>
<parameter name="attr2">val2</parameter>
<parameter name="priority">-20</parameter>
</tag>
</tags>
</definition>
</container>