|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the PHP Translation package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) PHP Translation team <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Translation\Bundle\DependencyInjection\CompilerPass; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
16
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
17
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @author Tobias Nyholm <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
class ExtractorPass implements CompilerPassInterface |
|
23
|
|
|
{ |
|
24
|
1 |
|
public function process(ContainerBuilder $container) |
|
25
|
|
|
{ |
|
26
|
1 |
|
$extractors = $this->getExtractors($container); |
|
27
|
1 |
|
$this->addVisitors($container, $extractors); |
|
28
|
1 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param ContainerBuilder $container |
|
32
|
|
|
* |
|
33
|
|
|
* @return array type => Definition[] |
|
34
|
|
|
*/ |
|
35
|
1 |
|
private function getExtractors(ContainerBuilder $container) |
|
36
|
|
|
{ |
|
37
|
|
|
/** @var Definition $def */ |
|
38
|
1 |
|
$def = $container->getDefinition('php_translation.extractor'); |
|
39
|
1 |
|
$services = $container->findTaggedServiceIds('php_translation.extractor'); |
|
40
|
1 |
|
$extractors = []; |
|
41
|
1 |
|
foreach ($services as $id => $tags) { |
|
42
|
1 |
|
foreach ($tags as $tag) { |
|
43
|
1 |
|
if (!isset($tag['type'])) { |
|
44
|
|
|
throw new \LogicException('The tag "php_translation.extractor" must have a "type".'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
1 |
|
$extractors[$tag['type']][] = $container->getDefinition($id); |
|
48
|
1 |
|
$def->addMethodCall('addFileExtractor', [new Reference($id)]); |
|
49
|
1 |
|
} |
|
50
|
1 |
|
} |
|
51
|
|
|
|
|
52
|
1 |
|
return $extractors; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param ContainerBuilder $container |
|
57
|
|
|
* @param $extractors |
|
58
|
|
|
*/ |
|
59
|
1 |
|
private function addVisitors(ContainerBuilder $container, $extractors) |
|
60
|
|
|
{ |
|
61
|
1 |
|
$services = $container->findTaggedServiceIds('php_translation.visitor'); |
|
62
|
1 |
|
foreach ($services as $id => $tags) { |
|
63
|
1 |
|
foreach ($tags as $tag) { |
|
64
|
1 |
|
if (!isset($tag['type'])) { |
|
65
|
|
|
throw new \LogicException('The tag "php_translation.visitor" must have a "type".'); |
|
66
|
|
|
} |
|
67
|
1 |
|
if (!isset($extractors[$tag['type']])) { |
|
68
|
|
|
continue; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
1 |
|
foreach ($extractors[$tag['type']] as $extractor) { |
|
72
|
1 |
|
$extractor->addMethodCall('addVisitor', [new Reference($id)]); |
|
73
|
1 |
|
} |
|
74
|
1 |
|
} |
|
75
|
1 |
|
} |
|
76
|
1 |
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|