1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AMQPAL\Adapter\PhpAmqpLib\Factory; |
4
|
|
|
|
5
|
|
|
use AMQPAL\Adapter\Exception; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class ConnectionFactoryFactory |
9
|
|
|
* |
10
|
|
|
* @package AMQPAL\Adapter\PhpAmqpLib\Factory |
11
|
|
|
*/ |
12
|
|
|
class ConnectionFactoryFactory |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
protected $factoryMap = [ |
18
|
|
|
'lazy' => LazyConnectionFactory::class, |
19
|
|
|
'stream' => StreamConnectionFactory::class, |
20
|
|
|
'socket' => SocketConnectionFactory::class, |
21
|
|
|
'ssl' => SSLConnectionFactory::class, |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
protected $instances = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Create the connection factory |
28
|
|
|
* |
29
|
|
|
* @param string $type |
30
|
|
|
* |
31
|
|
|
* @return ConnectionFactoryInterface |
32
|
|
|
* @throws Exception\InvalidArgumentException |
33
|
|
|
* @throws Exception\RuntimeException |
34
|
|
|
*/ |
35
|
10 |
|
public function createFactory($type) |
36
|
|
|
{ |
37
|
10 |
|
$map = $this->getFactoryMap(); |
38
|
10 |
|
if (!array_key_exists($type, $map)) { |
39
|
1 |
|
throw new Exception\InvalidArgumentException(sprintf('Factory type "%s" is not in the map', $type)); |
40
|
|
|
} |
41
|
|
|
|
42
|
9 |
|
$className = $map[$type]; |
43
|
9 |
|
$factory = new $className; |
44
|
9 |
|
if (!$factory instanceof ConnectionFactoryInterface) { |
45
|
1 |
|
throw new Exception\RuntimeException( |
46
|
1 |
|
sprintf('Factory for type "%s" must be an instance of ConnectionFactoryInterface', $type) |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
8 |
|
return $factory; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
10 |
|
public function getFactoryMap() |
57
|
|
|
{ |
58
|
10 |
|
return $this->factoryMap; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param array $factoryMap |
63
|
|
|
* @return $this |
64
|
|
|
*/ |
65
|
3 |
|
public function setFactoryMap(array $factoryMap) |
66
|
|
|
{ |
67
|
3 |
|
$this->factoryMap = $factoryMap; |
68
|
3 |
|
return $this; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|