1 | <?php |
||
28 | class ServiceMapPass implements CompilerPassInterface, \Serializable |
||
29 | { |
||
30 | private $tagName; |
||
31 | private $keyAttributeName; |
||
32 | private $callable; |
||
33 | |||
34 | 25 | public function __construct($tagName, $keyAttributeName, $callable) |
|
40 | |||
41 | 25 | public function process(ContainerBuilder $container) |
|
42 | { |
||
43 | 25 | if (!is_callable($this->callable)) { |
|
44 | throw new \RuntimeException('The callable is invalid. If you had serialized this pass, the original callable might not be available anymore.'); |
||
45 | } |
||
46 | |||
47 | 25 | $serviceMap = array(); |
|
48 | 25 | foreach ($container->findTaggedServiceIds($this->tagName) as $id => $tags) { |
|
49 | 25 | foreach ($tags as $tag) { |
|
50 | 25 | if (!isset($tag[$this->keyAttributeName])) { |
|
51 | throw new \RuntimeException(sprintf('The attribute "%s" must be set for service "%s" and tag "%s".', $this->keyAttributeName, $id, $this->tagName)); |
||
52 | } |
||
53 | |||
54 | 25 | $serviceMap[$tag[$this->keyAttributeName]] = new Reference($id); |
|
55 | 25 | } |
|
56 | 25 | } |
|
57 | |||
58 | 25 | $def = new Definition('PhpCollection\Map'); |
|
59 | 25 | $def->addArgument($serviceMap); |
|
60 | |||
61 | 25 | call_user_func($this->callable, $container, $def); |
|
62 | 25 | } |
|
63 | |||
64 | public function serialize() |
||
68 | |||
69 | public function unserialize($str) |
||
73 | } |
||
74 |