1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* (c) 2013 Wozbe |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Pyrowman\PheanstalkBundle\DependencyInjection\Compiler; |
10
|
|
|
|
11
|
|
|
use Pyrowman\PheanstalkBundle\Exceptions\PheanstalkException; |
12
|
|
|
use Pheanstalk\Pheanstalk; |
13
|
|
|
use Pheanstalk\PheanstalkInterface; |
14
|
|
|
use Pyrowman\PheanstalkBundle\PheanstalkLocator; |
15
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
17
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Description of ProxyCompilerPass. |
21
|
|
|
* |
22
|
|
|
* @author Thomas Tourlourat <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class ProxyCompilerPass implements CompilerPassInterface |
25
|
|
|
{ |
26
|
|
|
protected $defaultPheanstalkName = null; |
27
|
|
|
|
28
|
11 |
|
protected function reservedName() |
29
|
|
|
{ |
30
|
|
|
return [ |
31
|
11 |
|
'pheanstalks', |
32
|
|
|
'pheanstalk_locator', |
33
|
|
|
'proxy', |
34
|
|
|
'data_collector', |
35
|
|
|
'listener', |
36
|
|
|
'event', |
37
|
|
|
]; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param ContainerBuilder $container |
42
|
|
|
* |
43
|
|
|
* @throws PheanstalkException |
44
|
|
|
*/ |
45
|
11 |
|
public function process(ContainerBuilder $container) |
46
|
|
|
{ |
47
|
11 |
|
$pheanstalks = $container->getParameter('pheanstalk.pheanstalks'); |
48
|
|
|
|
49
|
|
|
// For each connection in the configuration file |
50
|
11 |
|
foreach ($pheanstalks as $name => $pheanstalk) { |
51
|
11 |
|
$this->addServer($name, $pheanstalk, $container); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $name |
57
|
|
|
* @param array $pheanstalk |
58
|
|
|
* @param ContainerBuilder $container |
59
|
|
|
* |
60
|
|
|
* @throws PheanstalkException |
61
|
|
|
*/ |
62
|
11 |
|
protected function addServer($name, array $pheanstalk, ContainerBuilder $container) |
63
|
|
|
{ |
64
|
11 |
|
$pheanstalkLocatorDef = $container->getDefinition('pheanstalk.pheanstalk_locator'); |
65
|
11 |
|
if (in_array($name, $this->reservedName())) { |
66
|
1 |
|
throw new \RuntimeException('Reserved pheanstalk name: ' . $name); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$pheanstalkConfig = [ |
70
|
10 |
|
$pheanstalk['server'], |
71
|
10 |
|
$pheanstalk['user'] ?? null, |
72
|
10 |
|
$pheanstalk['password'] ?? null, |
73
|
10 |
|
$pheanstalk['port'], |
74
|
10 |
|
$pheanstalk['timeout'] |
75
|
|
|
]; |
76
|
10 |
|
$isDefault = $pheanstalk['default']; |
77
|
|
|
|
78
|
|
|
# @see https://github.com/armetiz/pyrowmanPheanstalkBundle/issues/61 |
79
|
10 |
|
$pheanstalkDef = clone $container->getDefinition($pheanstalk['proxy']); |
80
|
|
|
|
81
|
9 |
|
$pheanstalkDef->addMethodCall('setPheanstalk', [new Definition(Pheanstalk::class, $pheanstalkConfig)]); |
82
|
9 |
|
$pheanstalkDef->addMethodCall('setName', [$name]); |
83
|
9 |
|
$pheanstalkDef->setPublic(true); |
84
|
|
|
|
85
|
9 |
|
$container->setDefinition('pheanstalk.' . $name, $pheanstalkDef); |
86
|
|
|
|
87
|
|
|
// Register the connection in the connection locator |
88
|
9 |
|
$pheanstalkLocatorDef->addMethodCall('addPheanstalk', [ |
89
|
9 |
|
$name, |
90
|
9 |
|
$container->getDefinition('pheanstalk.' . $name), |
91
|
9 |
|
$isDefault, |
92
|
|
|
]); |
93
|
|
|
|
94
|
9 |
|
if ($isDefault) { |
95
|
5 |
|
$this->autowireDefaultConfig($name, $container); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $name |
101
|
|
|
* @param ContainerBuilder $container |
102
|
|
|
* |
103
|
|
|
* @throws PheanstalkException |
104
|
|
|
*/ |
105
|
5 |
|
protected function autowireDefaultConfig($name, ContainerBuilder $container) |
106
|
|
|
{ |
107
|
5 |
|
if (null !== $this->defaultPheanstalkName) { |
108
|
1 |
|
throw new PheanstalkException(sprintf('Default pheanstalk already defined. "%s" & "%s"', $this->defaultPheanstalkName, $name)); |
109
|
|
|
} |
110
|
|
|
|
111
|
5 |
|
$this->defaultPheanstalkName = $name; |
112
|
5 |
|
$legacyAlias = $container->setAlias('pheanstalk', 'pheanstalk.' . $name); |
113
|
5 |
|
$legacyAlias->setPublic(true); |
114
|
|
|
|
115
|
5 |
|
$autoWiringAlias = $container->setAlias(PheanstalkInterface::class, 'pheanstalk'); |
116
|
5 |
|
$autoWiringAlias->setPublic(true); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|