Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bundle] Fix "Incompatible use of dynamic environment variables "ENQUEUE_DSN" found in parameters." #107

Merged
merged 5 commits into from
May 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,22 @@ matrix:
- php: 7.0
sudo: false
env: SYMFONY_VERSION=3.0.* UNIT_TESTS=true SYMFONY_DEPRECATIONS_HELPER=weak
- php: 7.0
- php: 7.1
sudo: required
services: docker
env: SYMFONY_VERSION=2.8.* FUNCTIONAL_TESTS=true
- php: 7.0
- php: 7.1
sudo: required
services: docker
env: SYMFONY_VERSION=3.0.* FUNCTIONAL_TESTS=true
- php: 7.1
sudo: required
services: docker
env: SYMFONY_VERSION=3.2.* FUNCTIONAL_TESTS=true
- php: 7.1
sudo: required
services: docker
env: SYMFONY_VERSION=3.3.* FUNCTIONAL_TESTS=true

cache:
directories:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

namespace Enqueue\Bundle\Tests\Functional\App;

use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

class TestAsyncListener
{
public $calls = [];

public function onEvent()
public function onEvent(Event $event, $eventName, EventDispatcherInterface $dispatcher)
{
$this->calls[] = func_get_args();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace Enqueue\Bundle\Tests\Functional\App;

use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class TestAsyncSubscriber implements EventSubscriberInterface
{
public $calls = [];

public function onEvent()
public function onEvent(Event $event, $eventName, EventDispatcherInterface $dispatcher)
{
$this->calls[] = func_get_args();
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/enqueue-bundle/Tests/Functional/UseCasesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Enqueue\Psr\PsrContext;
use Enqueue\Psr\PsrMessage;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\HttpKernel\Kernel;

/**
* @group functional
Expand Down Expand Up @@ -49,6 +50,15 @@ public function provideEnqueueConfigs()
],
]];

// Symfony 2.x does not such env syntax
if (version_compare(Kernel::VERSION, '3.2', '>=')) {
yield 'default_dsn_as_env' => [[
'transport' => [
'default' => '%env(AMQP_DSN)%',
],
]];
}

yield 'default_dbal_as_dsn' => [[
'transport' => [
'default' => getenv('DOCTINE_DSN'),
Expand Down
41 changes: 37 additions & 4 deletions pkg/enqueue/Symfony/DefaultTransportFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function addConfiguration(ArrayNodeDefinition $builder)
}

if (is_string($v)) {
return false !== strpos($v, '://') ?
return false !== strpos($v, '://') || false !== strpos($v, 'env_') ?
['dsn' => $v] :
['alias' => $v];
}
Expand All @@ -69,7 +69,9 @@ public function createConnectionFactory(ContainerBuilder $container, array $conf
if (isset($config['alias'])) {
$aliasId = sprintf('enqueue.transport.%s.connection_factory', $config['alias']);
} else {
$aliasId = $this->findFactory($config['dsn'])->createConnectionFactory($container, $config);
$dsn = $this->resolveDSN($container, $config['dsn']);

$aliasId = $this->findFactory($dsn)->createConnectionFactory($container, $config);
}

$factoryId = sprintf('enqueue.transport.%s.connection_factory', $this->getName());
Expand All @@ -88,7 +90,9 @@ public function createContext(ContainerBuilder $container, array $config)
if (isset($config['alias'])) {
$aliasId = sprintf('enqueue.transport.%s.context', $config['alias']);
} else {
$aliasId = $this->findFactory($config['dsn'])->createContext($container, $config);
$dsn = $this->resolveDSN($container, $config['dsn']);

$aliasId = $this->findFactory($dsn)->createContext($container, $config);
}

$contextId = sprintf('enqueue.transport.%s.context', $this->getName());
Expand All @@ -107,7 +111,9 @@ public function createDriver(ContainerBuilder $container, array $config)
if (isset($config['alias'])) {
$aliasId = sprintf('enqueue.client.%s.driver', $config['alias']);
} else {
$aliasId = $this->findFactory($config['dsn'])->createDriver($container, $config);
$dsn = $this->resolveDSN($container, $config['dsn']);

$aliasId = $this->findFactory($dsn)->createDriver($container, $config);
}

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

/**
* This is a quick fix to the exception "Incompatible use of dynamic environment variables "ENQUEUE_DSN" found in parameters."
* TODO: We'll have to come up with a better solution.
*
* @param ContainerBuilder $container
* @param $dsn
*
* @return array|false|string
*/
private function resolveDSN(ContainerBuilder $container, $dsn)
{
if (method_exists($container, 'resolveEnvPlaceholders')) {
$dsn = $container->resolveEnvPlaceholders($dsn);

$matches = [];
if (preg_match('/%env\((.*?)\)/', $dsn, $matches)) {
if (false === $realDsn = getenv($matches[1])) {
throw new \LogicException(sprintf('The env "%s" var is not defined', $matches[1]));
}

return $realDsn;
}
}

return $dsn;
}

/**
* @param string
* @param mixed $dsn
Expand Down