1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AMQPAL\Adapter; |
4
|
|
|
|
5
|
|
|
class AdapterFactory |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var array |
9
|
|
|
*/ |
10
|
|
|
protected $adapters = [ |
11
|
|
|
'amqp' => AMQP\AMQP::class, |
12
|
|
|
'phpamqplib' => PhpAmqpLib\PhpAmqpLib::class, |
13
|
|
|
]; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param array $options |
17
|
|
|
* @return AdapterInterface |
18
|
|
|
* @throws Exception\InvalidArgumentException |
19
|
|
|
* @throws Exception\OutOfBoundsException |
20
|
|
|
*/ |
21
|
4 |
|
public function createAdapter(array $options) |
22
|
|
|
{ |
23
|
4 |
|
if (!array_key_exists('name', $options)) { |
24
|
1 |
|
throw new Exception\InvalidArgumentException('Unable to find \'name\' key.'); |
25
|
|
|
} |
26
|
|
|
|
27
|
3 |
|
if (!array_key_exists('options', $options)) { |
28
|
1 |
|
throw new Exception\InvalidArgumentException('Unable to find \'options\' key.'); |
29
|
|
|
} |
30
|
|
|
|
31
|
2 |
|
$name = strtolower($options['name']); |
32
|
|
|
|
33
|
2 |
|
if (!array_key_exists($name, $this->adapters)) { |
34
|
1 |
|
throw new Exception\OutOfBoundsException(sprintf('Unable to find adapter \'%s\'.', $name)); |
35
|
|
|
} |
36
|
|
|
|
37
|
1 |
|
$className = $this->adapters[$name]; |
38
|
|
|
|
39
|
1 |
|
return new $className($options['options']); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
3 |
|
public function getAdapters() |
46
|
|
|
{ |
47
|
3 |
|
return $this->adapters; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param array $adapters |
52
|
|
|
* @return $this |
53
|
|
|
*/ |
54
|
6 |
|
public function setAdapters(array $adapters) |
55
|
|
|
{ |
56
|
6 |
|
$this->adapters = $adapters; |
57
|
|
|
|
58
|
6 |
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $name The adapter name |
63
|
|
|
* @param string $adapterClass The adapter class |
64
|
|
|
* @return $this |
65
|
|
|
*/ |
66
|
1 |
|
public function setAdapter($name, $adapterClass) |
67
|
|
|
{ |
68
|
1 |
|
$name = strtolower($name); |
69
|
1 |
|
$this->adapters[$name] = $adapterClass; |
70
|
|
|
|
71
|
1 |
|
return $this; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|