Skip to content

[amqp] Configure by string DSN. #80

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

Merged
merged 7 commits into from
May 12, 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
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ services:
volumes:
- './:/mqdev'
environment:
- AMQP_DSN=amqp://rabbitmq
- SYMFONY__RABBITMQ__HOST=rabbitmq
- SYMFONY__RABBITMQ__USER=guest
- SYMFONY__RABBITMQ__PASSWORD=guest
Expand Down
12 changes: 6 additions & 6 deletions docs/bundle/config_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ enqueue:
# Port on the host.
port: 5672

# The login name to use. Note: Max 128 characters.
login: guest
# The user name to use. Note: Max 128 characters.
user: guest

# Password. Note: Max 128 characters.
password: guest
pass: guest

# The virtual host on the host. Note: Max 128 characters.
vhost: /
Expand All @@ -70,11 +70,11 @@ enqueue:
# Port on the host.
port: 5672

# The login name to use. Note: Max 128 characters.
login: guest
# The user name to use. Note: Max 128 characters.
user: guest

# Password. Note: Max 128 characters.
password: guest
pass: guest

# The virtual host on the host. Note: Max 128 characters.
vhost: /
Expand Down
7 changes: 1 addition & 6 deletions docs/bundle/quick_tour.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,7 @@ First, you have to configure a transport layer and set one to be default.
enqueue:
transport:
default: 'amqp'
amqp:
host: 'localhost'
port: 5672
login: 'guest'
password: 'guest'
vhost: '/'
amqp: "amqp://"
client: ~
```

Expand Down
8 changes: 1 addition & 7 deletions docs/client/quick_tour.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,7 @@ include __DIR__.'/vendor/autoload.php';
$client = new SimpleClient([
'transport' => [
'default' => 'amqp',
'amqp' => [
'host' => 'localhost',
'port' => 5672,
'vhost' => '/',
'login' => 'guest',
'password' => 'guest',
],
'amqp' => 'amqp://'
],
'client' => [
'app_name' => 'plain_php',
Expand Down
4 changes: 2 additions & 2 deletions docs/quick_tour.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,8 @@ $client = new SimpleClient([
'host' => 'localhost',
'port' => 5672,
'vhost' => '/',
'login' => 'guest',
'password' => 'guest',
'user' => 'guest',
'pass' => 'guest',
],
],
'client' => true,
Expand Down
21 changes: 17 additions & 4 deletions docs/transport/amqp.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,28 @@ $ composer require enqueue/amqp-ext
<?php
use Enqueue\AmqpExt\AmqpConnectionFactory;

// connects to localhost
$connectionFactory = new AmqpConnectionFactory();

// same as above
$connectionFactory = new AmqpConnectionFactory('amqp://');

// same as above
$connectionFactory = new AmqpConnectionFactory([]);

// connect to AMQP broker at example.com
$connectionFactory = new AmqpConnectionFactory([
'host' => '127.0.0.1',
'port' => 5672,
'host' => 'example.com',
'port' => 1000,
'vhost' => '/',
'login' => 'guest',
'password' => 'guest',
'user' => 'user',
'pass' => 'pass',
'persisted' => false,
]);

// same as above but given as DSN string
$connectionFactory = new AmqpConnectionFactory('amqp://user:pass@example.com:10000/%2f');

$psrContext = $connectionFactory->createContext();
```

Expand Down
117 changes: 96 additions & 21 deletions pkg/amqp-ext/AmqpConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,39 @@ class AmqpConnectionFactory implements PsrConnectionFactory
private $connection;

/**
* $config = [
* The config could be an array, string DSN or null. In case of null it will attempt to connect to localhost with default credentials.
*
* [
* 'host' => amqp.host The host to connect too. Note: Max 1024 characters.
* 'port' => amqp.port Port on the host.
* 'vhost' => amqp.vhost The virtual host on the host. Note: Max 128 characters.
* 'login' => amqp.login The login name to use. Note: Max 128 characters.
* 'password' => amqp.password Password. Note: Max 128 characters.
* 'user' => amqp.user The user name to use. Note: Max 128 characters.
* 'pass' => amqp.password Password. Note: Max 128 characters.
* 'read_timeout' => Timeout in for income activity. Note: 0 or greater seconds. May be fractional.
* 'write_timeout' => Timeout in for outcome activity. Note: 0 or greater seconds. May be fractional.
* 'connect_timeout' => Connection timeout. Note: 0 or greater seconds. May be fractional.
* 'persisted' => bool, Whether it use single persisted connection or open a new one for every context
* 'lazy' => the connection will be performed as later as possible, if the option set to true
* ].
* ]
*
* or
*
* amqp://user:pass@host:10000/vhost?lazy=true&persisted=false&read_timeout=2
*
* @param $config
* @param array|string $config
*/
public function __construct(array $config)
public function __construct($config = 'amqp://')
{
$this->config = array_replace([
'host' => null,
'port' => null,
'vhost' => null,
'login' => null,
'password' => null,
'read_timeout' => null,
'write_timeout' => null,
'connect_timeout' => null,
'persisted' => false,
'lazy' => true,
], $config);
if (empty($config)) {
$config = [];
} elseif (is_string($config)) {
$config = $this->parseDsn($config);
} elseif (is_array($config)) {
} else {
throw new \LogicException('The config must be eaither an array of options, a DSN string or null');
}

$this->config = array_replace($this->defaultConfig(), $config);
}

/**
Expand All @@ -64,18 +68,89 @@ public function createContext()
return new AmqpContext(new \AMQPChannel($this->establishConnection()));
}

/**
* @return \AMQPConnection
*/
private function establishConnection()
{
if (false == $this->connection) {
$this->connection = new \AMQPConnection($this->config);

$config = $this->config;
$config['login'] = $this->config['user'];
$config['password'] = $this->config['pass'];
$this->connection = new \AMQPConnection($config);
$this->config['persisted'] ? $this->connection->pconnect() : $this->connection->connect();
}

if (false == $this->connection->isConnected()) {
$this->config['persisted'] ? $this->connection->preconnect() : $this->connection->reconnect();
}

return $this->connection;
}

/**
* @param string $dsn
*
* @return array
*/
private function parseDsn($dsn)
{
if ('amqp://' == $dsn) {
return [];
}

$dsnConfig = parse_url($dsn);
if (false === $dsnConfig) {
throw new \LogicException(sprintf('Failed to parse DSN "%s"', $dsn));
}

$dsnConfig = array_replace([
'scheme' => null,
'host' => null,
'port' => null,
'user' => null,
'pass' => null,
'path' => null,
'query' => null,
], $dsnConfig);

if ('amqp' !== $dsnConfig['scheme']) {
throw new \LogicException('The given DSN scheme "%s" is not supported. Could be "amqp" only.');
}

if ($dsnConfig['query']) {
$query = [];
parse_str($dsnConfig['query'], $query);
$dsnConfig = array_replace($query, $dsnConfig);
}

$dsnConfig['vhost'] = ltrim($dsnConfig['path'], '/');

unset($dsnConfig['scheme'], $dsnConfig['query'], $dsnConfig['fragment'], $dsnConfig['path']);

$config = array_replace($this->defaultConfig(), $dsnConfig);
$config = array_map(function ($value) {
return urldecode($value);
}, $config);

return $config;
}

/**
* @return array
*/
private function defaultConfig()
{
return [
'host' => 'localhost',
'port' => 5672,
'vhost' => '/',
'user' => 'guest',
'pass' => 'guest',
'read_timeout' => null,
'write_timeout' => null,
'connect_timeout' => null,
'persisted' => false,
'lazy' => true,
];
}
}
17 changes: 13 additions & 4 deletions pkg/amqp-ext/Symfony/AmqpTransportFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,16 @@ public function __construct($name = 'amqp')
public function addConfiguration(ArrayNodeDefinition $builder)
{
$builder
->beforeNormalization()
->ifString()
->then(function ($v) {
return ['dsn' => $v];
})
->end()
->children()
->scalarNode('dsn')
->info('The connection to AMQP broker set as a string. Other parameters are ignored if set')
->end()
->scalarNode('host')
->defaultValue('localhost')
->cannotBeEmpty()
Expand All @@ -43,12 +52,12 @@ public function addConfiguration(ArrayNodeDefinition $builder)
->cannotBeEmpty()
->info('Port on the host.')
->end()
->scalarNode('login')
->scalarNode('user')
->defaultValue('guest')
->cannotBeEmpty()
->info('The login name to use. Note: Max 128 characters.')
->info('The user name to use. Note: Max 128 characters.')
->end()
->scalarNode('password')
->scalarNode('pass')
->defaultValue('guest')
->cannotBeEmpty()
->info('Password. Note: Max 128 characters.')
Expand Down Expand Up @@ -85,7 +94,7 @@ public function addConfiguration(ArrayNodeDefinition $builder)
public function createConnectionFactory(ContainerBuilder $container, array $config)
{
$factory = new Definition(AmqpConnectionFactory::class);
$factory->setArguments([$config]);
$factory->setArguments(isset($config['dsn']) ? [$config['dsn']] : [$config]);

$factoryId = sprintf('enqueue.transport.%s.connection_factory', $this->getName());
$container->setDefinition($factoryId, $factory);
Expand Down
Loading