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 Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
16
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Description of ProxyCompilerPass. |
20
|
|
|
* |
21
|
|
|
* @author Thomas Tourlourat <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class ProxyCompilerPass implements CompilerPassInterface |
24
|
|
|
{ |
25
|
11 |
|
protected function reservedName() |
26
|
|
|
{ |
27
|
|
|
return [ |
28
|
11 |
|
'pheanstalks', |
29
|
|
|
'pheanstalk_locator', |
30
|
|
|
'proxy', |
31
|
|
|
'data_collector', |
32
|
|
|
'listener', |
33
|
|
|
'event', |
34
|
|
|
]; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @inheritdoc |
39
|
|
|
*/ |
40
|
11 |
|
public function process(ContainerBuilder $container) |
41
|
|
|
{ |
42
|
11 |
|
$defaultPheanstalkName = null; |
43
|
11 |
|
$pheanstalks = $container->getParameter('pyrowman.pheanstalk.pheanstalks'); |
44
|
|
|
|
45
|
11 |
|
$pheanstalkLocatorDef = $container->getDefinition('pyrowman.pheanstalk.pheanstalk_locator'); |
46
|
|
|
|
47
|
|
|
// For each connection in the configuration file |
48
|
11 |
|
foreach ($pheanstalks as $name => $pheanstalk) { |
49
|
11 |
|
if (in_array($name, $this->reservedName())) { |
50
|
1 |
|
throw new \RuntimeException('Reserved pheanstalk name: '.$name); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$pheanstalkConfig = [ |
54
|
10 |
|
$pheanstalk['server'], |
55
|
10 |
|
$pheanstalk['user'] ?? null, |
56
|
10 |
|
$pheanstalk['password'] ?? null, |
57
|
10 |
|
$pheanstalk['port'], |
58
|
10 |
|
$pheanstalk['timeout'] |
59
|
|
|
]; |
60
|
10 |
|
$isDefault = $pheanstalk['default']; |
61
|
|
|
|
62
|
|
|
# @see https://github.com/armetiz/pyrowmanPheanstalkBundle/issues/61 |
63
|
10 |
|
$pheanstalkDef = clone $container->getDefinition($pheanstalk['proxy']); |
64
|
|
|
|
65
|
9 |
|
$pheanstalkDef->addMethodCall('setPheanstalk', [new Definition(Pheanstalk::class, $pheanstalkConfig)]); |
66
|
9 |
|
$pheanstalkDef->addMethodCall('setName', [$name]); |
67
|
9 |
|
$pheanstalkDef->setPublic(true); |
68
|
|
|
|
69
|
9 |
|
$container->setDefinition('pyrowman.pheanstalk.'.$name, $pheanstalkDef); |
70
|
|
|
|
71
|
|
|
// Register the connection in the connection locator |
72
|
9 |
|
$pheanstalkLocatorDef->addMethodCall('addPheanstalk', [ |
73
|
9 |
|
$name, |
74
|
9 |
|
$container->getDefinition('pyrowman.pheanstalk.'.$name), |
75
|
9 |
|
$isDefault, |
76
|
|
|
]); |
77
|
|
|
|
78
|
9 |
|
if ($isDefault) { |
79
|
5 |
|
if (null !== $defaultPheanstalkName) { |
80
|
1 |
|
throw new PheanstalkException(sprintf('Default pheanstalk already defined. "%s" & "%s"', $defaultPheanstalkName, $name)); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
5 |
|
$defaultPheanstalkName = $name; |
84
|
5 |
|
$legacyAlias = $container->setAlias('pyrowman.pheanstalk', 'pyrowman.pheanstalk.'.$name); |
85
|
5 |
|
if ($legacyAlias) { |
86
|
5 |
|
$legacyAlias->setPublic(true); |
87
|
|
|
} |
88
|
|
|
|
89
|
5 |
|
$autoWiringAlias = $container->setAlias(PheanstalkInterface::class, 'pyrowman.pheanstalk'); |
90
|
5 |
|
if ($autoWiringAlias) { |
91
|
9 |
|
$autoWiringAlias->setPublic(true); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|