Completed
Push — master ( 143d9a...4c3327 )
by André
121:21 queued 99:18
created

RichTextHtml5ConverterPass::sortConverters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the RichTextHtml5ConverterPass class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Compiler;
10
11
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
12
use Symfony\Component\DependencyInjection\ContainerBuilder;
13
use Symfony\Component\DependencyInjection\Reference;
14
use Symfony\Component\DependencyInjection\Definition;
15
16
/**
17
 * Compiler pass for the RichText Aggregate converter tags.
18
 *
19
 * @see \eZ\Publish\Core\FieldType\RichText\Converter\Aggregate
20
 */
21
class RichTextHtml5ConverterPass implements CompilerPassInterface
22
{
23
    public function process(ContainerBuilder $container)
24
    {
25
        if ($container->hasDefinition('ezpublish.fieldType.ezrichtext.converter.output.xhtml5')) {
26
            $html5OutputConverterDefinition = $container->getDefinition('ezpublish.fieldType.ezrichtext.converter.output.xhtml5');
27
            $taggedOutputServiceIds = $container->findTaggedServiceIds('ezpublish.ezrichtext.converter.output.xhtml5');
28
            $this->setConverterDefinitions($taggedOutputServiceIds, $html5OutputConverterDefinition);
29
        }
30
31
        if ($container->hasDefinition('ezpublish.fieldType.ezrichtext.converter.input.xhtml5')) {
32
            $html5InputConverterDefinition = $container->getDefinition('ezpublish.fieldType.ezrichtext.converter.input.xhtml5');
33
            $taggedInputServiceIds = $container->findTaggedServiceIds('ezpublish.ezrichtext.converter.input.xhtml5');
34
            $this->setConverterDefinitions($taggedInputServiceIds, $html5InputConverterDefinition);
35
        }
36
    }
37
38
    /**
39
     * @param array $taggedServiceIds
40
     * @param Definition $converterDefinition
41
     */
42
    protected function setConverterDefinitions(array $taggedServiceIds, Definition $converterDefinition)
43
    {
44
        $convertersByPriority = array();
45
        foreach ($taggedServiceIds as $id => $tags) {
46
            foreach ($tags as $tag) {
47
                $priority = isset($tag['priority']) ? (int)$tag['priority'] : 0;
48
                $convertersByPriority[$priority][] = new Reference($id);
49
            }
50
        }
51
52
        if (count($convertersByPriority) > 0) {
53
            $converterDefinition->setArguments(
54
                array($this->sortConverters($convertersByPriority))
55
            );
56
        }
57
    }
58
59
    /**
60
     * Transforms a two-dimensional array of converters, indexed by priority,
61
     * into a flat array of Reference objects.
62
     *
63
     * @param array $convertersByPriority
64
     *
65
     * @return \Symfony\Component\DependencyInjection\Reference[]
66
     */
67
    protected function sortConverters(array $convertersByPriority)
68
    {
69
        ksort($convertersByPriority);
70
71
        return array_merge(...$convertersByPriority);
72
    }
73
}
74