|
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
|
|
|
foreach ($container->findTaggedServiceIds('ezpublish_rest.output.visitor') as $id => $attributes) { |
|
36
|
|
|
foreach ($attributes as $attribute) { |
|
37
|
|
|
if (!isset($attribute['regexps'])) { |
|
38
|
|
|
throw new \LogicException('ezpublish_rest.output.visitor service tag needs a "regexps" attribute to identify the Accept header. None given.'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
if (is_array($attribute['regexps'])) { |
|
42
|
|
|
$regexps = $attribute['regexps']; |
|
43
|
|
|
} elseif (is_string($attribute['regexps'])) { |
|
44
|
|
|
try { |
|
45
|
|
|
$regexps = $container->getParameter($attribute['regexps']); |
|
46
|
|
|
} catch (InvalidArgumentException $e) { |
|
47
|
|
|
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."); |
|
48
|
|
|
} |
|
49
|
|
|
} else { |
|
50
|
|
|
throw new \LogicException('ezpublish_rest.output.visitor service tag needs a "regexps" attribute, either as an array or a string. Invalid value.'); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
foreach ($regexps as $regexp) { |
|
54
|
|
|
$definition->addMethodCall( |
|
55
|
|
|
'addVisitor', |
|
56
|
|
|
array($regexp, new Reference($id)) |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|