Skip to content

Commit cf4500c

Browse files
authoredMay 30, 2017
Merge pull request #107 from php-enqueue/symfony-incompatible-use-dynamic-env-var
[bundle] Fix "Incompatible use of dynamic environment variables "ENQUEUE_DSN" found in parameters."
2 parents c0eb38a + e7043a7 commit cf4500c

File tree

5 files changed

+64
-8
lines changed

5 files changed

+64
-8
lines changed
 

Diff for: ‎.travis.yml

+10-2
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,22 @@ matrix:
2323
- php: 7.0
2424
sudo: false
2525
env: SYMFONY_VERSION=3.0.* UNIT_TESTS=true SYMFONY_DEPRECATIONS_HELPER=weak
26-
- php: 7.0
26+
- php: 7.1
2727
sudo: required
2828
services: docker
2929
env: SYMFONY_VERSION=2.8.* FUNCTIONAL_TESTS=true
30-
- php: 7.0
30+
- php: 7.1
3131
sudo: required
3232
services: docker
3333
env: SYMFONY_VERSION=3.0.* FUNCTIONAL_TESTS=true
34+
- php: 7.1
35+
sudo: required
36+
services: docker
37+
env: SYMFONY_VERSION=3.2.* FUNCTIONAL_TESTS=true
38+
- php: 7.1
39+
sudo: required
40+
services: docker
41+
env: SYMFONY_VERSION=3.3.* FUNCTIONAL_TESTS=true
3442

3543
cache:
3644
directories:

Diff for: ‎pkg/enqueue-bundle/Tests/Functional/App/TestAsyncListener.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
namespace Enqueue\Bundle\Tests\Functional\App;
44

5+
use Symfony\Component\EventDispatcher\Event;
6+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
7+
58
class TestAsyncListener
69
{
710
public $calls = [];
811

9-
public function onEvent()
12+
public function onEvent(Event $event, $eventName, EventDispatcherInterface $dispatcher)
1013
{
1114
$this->calls[] = func_get_args();
1215
}

Diff for: ‎pkg/enqueue-bundle/Tests/Functional/App/TestAsyncSubscriber.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
namespace Enqueue\Bundle\Tests\Functional\App;
44

5+
use Symfony\Component\EventDispatcher\Event;
6+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
57
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
68

79
class TestAsyncSubscriber implements EventSubscriberInterface
810
{
911
public $calls = [];
1012

11-
public function onEvent()
13+
public function onEvent(Event $event, $eventName, EventDispatcherInterface $dispatcher)
1214
{
1315
$this->calls[] = func_get_args();
1416
}

Diff for: ‎pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Enqueue\Psr\PsrContext;
99
use Enqueue\Psr\PsrMessage;
1010
use Symfony\Component\Console\Tester\CommandTester;
11+
use Symfony\Component\HttpKernel\Kernel;
1112

1213
/**
1314
* @group functional
@@ -49,6 +50,15 @@ public function provideEnqueueConfigs()
4950
],
5051
]];
5152

53+
// Symfony 2.x does not such env syntax
54+
if (version_compare(Kernel::VERSION, '3.2', '>=')) {
55+
yield 'default_dsn_as_env' => [[
56+
'transport' => [
57+
'default' => '%env(AMQP_DSN)%',
58+
],
59+
]];
60+
}
61+
5262
yield 'default_dbal_as_dsn' => [[
5363
'transport' => [
5464
'default' => getenv('DOCTINE_DSN'),

Diff for: ‎pkg/enqueue/Symfony/DefaultTransportFactory.php

+37-4
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function addConfiguration(ArrayNodeDefinition $builder)
5050
}
5151

5252
if (is_string($v)) {
53-
return false !== strpos($v, '://') ?
53+
return false !== strpos($v, '://') || false !== strpos($v, 'env_') ?
5454
['dsn' => $v] :
5555
['alias' => $v];
5656
}
@@ -69,7 +69,9 @@ public function createConnectionFactory(ContainerBuilder $container, array $conf
6969
if (isset($config['alias'])) {
7070
$aliasId = sprintf('enqueue.transport.%s.connection_factory', $config['alias']);
7171
} else {
72-
$aliasId = $this->findFactory($config['dsn'])->createConnectionFactory($container, $config);
72+
$dsn = $this->resolveDSN($container, $config['dsn']);
73+
74+
$aliasId = $this->findFactory($dsn)->createConnectionFactory($container, $config);
7375
}
7476

7577
$factoryId = sprintf('enqueue.transport.%s.connection_factory', $this->getName());
@@ -88,7 +90,9 @@ public function createContext(ContainerBuilder $container, array $config)
8890
if (isset($config['alias'])) {
8991
$aliasId = sprintf('enqueue.transport.%s.context', $config['alias']);
9092
} else {
91-
$aliasId = $this->findFactory($config['dsn'])->createContext($container, $config);
93+
$dsn = $this->resolveDSN($container, $config['dsn']);
94+
95+
$aliasId = $this->findFactory($dsn)->createContext($container, $config);
9296
}
9397

9498
$contextId = sprintf('enqueue.transport.%s.context', $this->getName());
@@ -107,7 +111,9 @@ public function createDriver(ContainerBuilder $container, array $config)
107111
if (isset($config['alias'])) {
108112
$aliasId = sprintf('enqueue.client.%s.driver', $config['alias']);
109113
} else {
110-
$aliasId = $this->findFactory($config['dsn'])->createDriver($container, $config);
114+
$dsn = $this->resolveDSN($container, $config['dsn']);
115+
116+
$aliasId = $this->findFactory($dsn)->createDriver($container, $config);
111117
}
112118

113119
$driverId = sprintf('enqueue.client.%s.driver', $this->getName());
@@ -126,6 +132,33 @@ public function getName()
126132
return $this->name;
127133
}
128134

135+
/**
136+
* This is a quick fix to the exception "Incompatible use of dynamic environment variables "ENQUEUE_DSN" found in parameters."
137+
* TODO: We'll have to come up with a better solution.
138+
*
139+
* @param ContainerBuilder $container
140+
* @param $dsn
141+
*
142+
* @return array|false|string
143+
*/
144+
private function resolveDSN(ContainerBuilder $container, $dsn)
145+
{
146+
if (method_exists($container, 'resolveEnvPlaceholders')) {
147+
$dsn = $container->resolveEnvPlaceholders($dsn);
148+
149+
$matches = [];
150+
if (preg_match('/%env\((.*?)\)/', $dsn, $matches)) {
151+
if (false === $realDsn = getenv($matches[1])) {
152+
throw new \LogicException(sprintf('The env "%s" var is not defined', $matches[1]));
153+
}
154+
155+
return $realDsn;
156+
}
157+
}
158+
159+
return $dsn;
160+
}
161+
129162
/**
130163
* @param string
131164
* @param mixed $dsn

0 commit comments

Comments
 (0)