-
Notifications
You must be signed in to change notification settings - Fork 8
/
SimpleRegistry.php
77 lines (64 loc) · 2.05 KB
/
SimpleRegistry.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
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
namespace Enqueue\AsyncEventDispatcher;
class SimpleRegistry implements Registry
{
/**
* @var string[]
*/
private $eventsMap;
/**
* @var string[]
*/
private $transformersMap;
/**
* @param string[] $eventsMap [eventName => transformerName]
* @param string[] $transformersMap [transformerName => transformerObject]
*/
public function __construct(array $eventsMap, array $transformersMap)
{
$this->eventsMap = $eventsMap;
$this->transformersMap = $transformersMap;
}
/**
* {@inheritdoc}
*/
public function getTransformerNameForEvent($eventName)
{
$transformerName = null;
if (array_key_exists($eventName, $this->eventsMap)) {
$transformerName = $this->eventsMap[$eventName];
} else {
foreach ($this->eventsMap as $eventNamePattern => $name) {
if ('/' != $eventNamePattern[0]) {
continue;
}
if (preg_match($eventNamePattern, $eventName)) {
$transformerName = $name;
break;
}
}
}
if (empty($transformerName)) {
throw new \LogicException(sprintf('There is no transformer registered for the given event %s', $eventName));
}
return $transformerName;
}
/**
* {@inheritdoc}
*/
public function getTransformer($name)
{
if (false == array_key_exists($name, $this->transformersMap)) {
throw new \LogicException(sprintf('There is no transformer named %s', $name));
}
$transformer = $this->transformersMap[$name];
if (false == $transformer instanceof EventTransformer) {
throw new \LogicException(sprintf(
'The container must return instance of %s but got %s',
EventTransformer::class,
is_object($transformer) ? get_class($transformer) : gettype($transformer)
));
}
return $transformer;
}
}