1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* File containing the OutputVisitorPass 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
|
|
|
* @version //autogentag// |
10
|
|
|
*/ |
11
|
|
|
namespace eZ\Bundle\EzPublishRestBundle\DependencyInjection\Compiler; |
12
|
|
|
|
13
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
14
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
15
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
16
|
|
|
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Compiler pass for the ezpublish_rest.output.visitor tag. |
20
|
|
|
* |
21
|
|
|
* Maps an output visitor (json, xml...) to an accept-header |
22
|
|
|
* |
23
|
|
|
* @todo The tag is much more limited in scope than the name shows. Refactor. More ways to map ? |
24
|
|
|
*/ |
25
|
|
|
class OutputVisitorPass implements CompilerPassInterface |
26
|
|
|
{ |
27
|
|
|
public function process(ContainerBuilder $container) |
28
|
|
|
{ |
29
|
|
|
if (!$container->hasDefinition('ezpublish_rest.output.visitor.dispatcher')) { |
30
|
|
|
return; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$definition = $container->getDefinition('ezpublish_rest.output.visitor.dispatcher'); |
34
|
|
|
|
35
|
|
|
$visitors = []; |
36
|
|
|
|
37
|
|
|
foreach ($container->findTaggedServiceIds('ezpublish_rest.output.visitor') as $id => $attributes) { |
38
|
|
|
foreach ($attributes as $attribute) { |
39
|
|
|
$priority = isset($attribute['priority']) ? $attribute['priority'] : 0; |
40
|
|
|
|
41
|
|
|
if (!isset($attribute['regexps'])) { |
42
|
|
|
throw new \LogicException('ezpublish_rest.output.visitor service tag needs a "regexps" attribute to identify the Accept header. None given.'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if (is_array($attribute['regexps'])) { |
46
|
|
|
$regexps = $attribute['regexps']; |
47
|
|
|
} elseif (is_string($attribute['regexps'])) { |
48
|
|
|
try { |
49
|
|
|
$regexps = $container->getParameter($attribute['regexps']); |
50
|
|
|
} catch (InvalidArgumentException $e) { |
51
|
|
|
throw new \LogicException("The regexps attribute of the ezpublish_rest.output.visitor service tag can be a string matching a container parameter name. No parameter {$attribute['regexps']} could be found."); |
52
|
|
|
} |
53
|
|
|
} else { |
54
|
|
|
throw new \LogicException('ezpublish_rest.output.visitor service tag needs a "regexps" attribute, either as an array or a string. Invalid value.'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$visitors[$priority][] = [ |
58
|
|
|
'regexps' => $regexps, |
59
|
|
|
'reference' => new Reference($id), |
60
|
|
|
]; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// sort by priority and flatten |
65
|
|
|
krsort($visitors); |
66
|
|
|
$visitors = call_user_func_array('array_merge', $visitors); |
67
|
|
|
|
68
|
|
|
foreach ($visitors as $visitor) { |
69
|
|
|
foreach ($visitor['regexps'] as $regexp) { |
70
|
|
|
$definition->addMethodCall( |
71
|
|
|
'addVisitor', [ |
72
|
|
|
$regexp, |
73
|
|
|
$visitor['reference'], |
74
|
|
|
] |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|