forked from ezsystems/ezpublish-kernel
/
XmlTextConverterPass.php
64 lines (55 loc) · 2.17 KB
/
XmlTextConverterPass.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
/**
* File containing the XmlTextConverterPass class.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
* @version //autogentag//
*/
namespace eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
/**
* Compiler pass adding pre-converters to HTML5 converter.
* Useful for manipulating internal XML before XSLT rendering.
*/
class XmlTextConverterPass implements CompilerPassInterface
{
public function process( ContainerBuilder $container )
{
if ( !$container->hasDefinition( 'ezpublish.fieldType.ezxmltext.converter.html5' ) )
{
return;
}
$html5ConverterDef = $container->getDefinition( 'ezpublish.fieldType.ezxmltext.converter.html5' );
$taggedServiceIds = $container->findTaggedServiceIds( 'ezpublish.ezxml.converter' );
$converterIdsByPriority = array();
foreach ( $taggedServiceIds as $id => $tags )
{
foreach ( $tags as $tag )
{
$priority = isset( $tag['priority'] ) ? (int)$tag['priority'] : 0;
$converterIdsByPriority[$priority][] = $id;
}
}
$converterIdsByPriority = $this->sortConverterIds( $converterIdsByPriority );
foreach ( $converterIdsByPriority as $referenceId )
{
$html5ConverterDef->addMethodCall( 'addPreConverter', array( new Reference( $referenceId ) ) );
}
}
/**
* Transforms a two-dimensional array of converters, indexed by priority,
* into a flat array of Reference objects.
*
* @param array $converterIdsByPriority
*
* @return \Symfony\Component\DependencyInjection\Reference[]
*/
protected function sortConverterIds( array $converterIdsByPriority )
{
krsort( $converterIdsByPriority, SORT_NUMERIC );
return call_user_func_array( 'array_merge', $converterIdsByPriority );
}
}