Skip to content

[Symfony] default factory should resolve DSN in runtime #510

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 6 commits into from
Aug 21, 2018
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
2 changes: 1 addition & 1 deletion bin/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
set -x
set -e

docker-compose run --workdir="/mqdev" --rm dev ./docker/bin/test.sh "$@"
docker-compose run --workdir="/mqdev" --rm dev ./docker/bin/test.sh $@
6 changes: 2 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"require-dev": {
"phpunit/phpunit": "^5.5",
"phpstan/phpstan": "^0.10",
"queue-interop/queue-spec": "^0.5.5@dev",
"queue-interop/queue-spec": "^0.5.9@dev",
"symfony/browser-kit": "4.0.*",
"symfony/config": "4.0.*",
"symfony/process": "4.0.*",
Expand Down Expand Up @@ -73,13 +73,11 @@
"Enqueue\\Sqs\\": "pkg/sqs/",
"Enqueue\\Stomp\\": "pkg/stomp/",
"Enqueue\\Test\\": "pkg/test/",
"Enqueue\\Dsn\\": "pkg/dsn/",
"Enqueue\\": "pkg/enqueue/"
},
"exclude-from-classmap": [
"/Tests/"
],
"files": [
"pkg/enqueue/functions_include.php"
]
},
"autoload-dev": {
Expand Down
6 changes: 3 additions & 3 deletions docs/transport/amqp.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ $factory = new AmqpConnectionFactory([

$psrContext = $factory->createContext();

// if you have enqueue/enqueue library installed you can use a function from there to create the context
$psrContext = \Enqueue\dsn_to_context('amqp:');
$psrContext = \Enqueue\dsn_to_context('amqp+ext:');
// if you have enqueue/enqueue library installed you can use a factory to build context from DSN
$psrContext = (new \Enqueue\ConnectionFactoryFactory())->create('amqp:')->createContext();
$psrContext = (new \Enqueue\ConnectionFactoryFactory())->create('amqp+ext:')->createContext();
```

## Declare topic.
Expand Down
6 changes: 3 additions & 3 deletions docs/transport/amqp_bunny.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ $factory = new AmqpConnectionFactory('amqp://user:pass@example.com:10000/%2f');

$psrContext = $factory->createContext();

// if you have enqueue/enqueue library installed you can use a function from there to create the context
$psrContext = \Enqueue\dsn_to_context('amqp:');
$psrContext = \Enqueue\dsn_to_context('amqp+bunny:');
// if you have enqueue/enqueue library installed you can use a factory to build context from DSN
$psrContext = (new \Enqueue\ConnectionFactoryFactory())->create('amqp:')->createContext();
$psrContext = (new \Enqueue\ConnectionFactoryFactory())->create('amqp+bunny:')->createContext();
```

## Declare topic.
Expand Down
6 changes: 3 additions & 3 deletions docs/transport/amqp_lib.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ $factory = new AmqpConnectionFactory([

$psrContext = $factory->createContext();

// if you have enqueue/enqueue library installed you can use a function from there to create the context
$psrContext = \Enqueue\dsn_to_context('amqp:');
$psrContext = \Enqueue\dsn_to_context('amqp+lib:');
// if you have enqueue/enqueue library installed you can use a factory to build context from DSN
$psrContext = (new \Enqueue\ConnectionFactoryFactory())->create('amqp:')->createContext();
$psrContext = (new \Enqueue\ConnectionFactoryFactory())->create('amqp+lib:')->createContext();
```

## Declare topic.
Expand Down
4 changes: 2 additions & 2 deletions docs/transport/dbal.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ $factory = new ManagerRegistryConnectionFactory($registry, [

$psrContext = $factory->createContext();

// if you have enqueue/enqueue library installed you can use a function from there to create the context
$psrContext = \Enqueue\dsn_to_context('mysql:');
// if you have enqueue/enqueue library installed you can use a factory to build context from DSN
$psrContext = (new \Enqueue\ConnectionFactoryFactory())->create('mysql:')->createContext();
```

## Init database
Expand Down
4 changes: 2 additions & 2 deletions docs/transport/filesystem.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ $connectionFactory = new FsConnectionFactory([

$psrContext = $connectionFactory->createContext();

// if you have enqueue/enqueue library installed you can use a function from there to create the context
$psrContext = \Enqueue\dsn_to_context('file:');
// if you have enqueue/enqueue library installed you can use a factory to build context from DSN
$psrContext = (new \Enqueue\ConnectionFactoryFactory())->create('file:')->createContext();
```

## Send message to topic
Expand Down
4 changes: 2 additions & 2 deletions docs/transport/gearman.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ $factory = new GearmanConnectionFactory([

$psrContext = $factory->createContext();

// if you have enqueue/enqueue library installed you can use a function from there to create the context
$psrContext = \Enqueue\dsn_to_context('gearman:');
// if you have enqueue/enqueue library installed you can use a factory to build context from DSN
$psrContext = (new \Enqueue\ConnectionFactoryFactory())->create('gearman:')->createContext();
```

## Send message to topic
Expand Down
4 changes: 2 additions & 2 deletions docs/transport/gps.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ $connectionFactory = new GpsConnectionFactory('gps:');

$psrContext = $connectionFactory->createContext();

// if you have enqueue/enqueue library installed you can use a function from there to create the context
$psrContext = \Enqueue\dsn_to_context('gps:');
// if you have enqueue/enqueue library installed you can use a factory to build context from DSN
$psrContext = (new \Enqueue\ConnectionFactoryFactory())->create('gps:')->createContext();
```

## Send message to topic
Expand Down
4 changes: 2 additions & 2 deletions docs/transport/kafka.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ $connectionFactory = new RdKafkaConnectionFactory([

$psrContext = $connectionFactory->createContext();

// if you have enqueue/enqueue library installed you can use a function from there to create the context
$psrContext = \Enqueue\dsn_to_context('kafka:');
// if you have enqueue/enqueue library installed you can use a factory to build context from DSN
$psrContext = (new \Enqueue\ConnectionFactoryFactory())->create('kafka:')->createContext();
```

## Send message to topic
Expand Down
4 changes: 2 additions & 2 deletions docs/transport/mongodb.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ $factory = new MongodbConnectionFactory([

$psrContext = $factory->createContext();

// if you have enqueue/enqueue library installed you can use a function from there to create the context
$psrContext = \Enqueue\dsn_to_context('mongodb:');
// if you have enqueue/enqueue library installed you can use a factory to build context from DSN
$psrContext = (new \Enqueue\ConnectionFactoryFactory())->create('mongodb:')->createContext();
```

## Send message to topic
Expand Down
4 changes: 2 additions & 2 deletions docs/transport/pheanstalk.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ $factory = new PheanstalkConnectionFactory([

$psrContext = $factory->createContext();

// if you have enqueue/enqueue library installed you can use a function from there to create the context
$psrContext = \Enqueue\dsn_to_context('beanstalk:');
// if you have enqueue/enqueue library installed you can use a factory to build context from DSN
$psrContext = (new \Enqueue\ConnectionFactoryFactory())->create('beanstalk:')->createContext();
```

## Send message to topic
Expand Down
4 changes: 2 additions & 2 deletions docs/transport/redis.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ $factory = new RedisConnectionFactory('redis://example.com:1000?vendor=phpredis'

$psrContext = $factory->createContext();

// if you have enqueue/enqueue library installed you can use a function from there to create the context
$psrContext = \Enqueue\dsn_to_context('redis:');
// if you have enqueue/enqueue library installed you can use a factory to build context from DSN
$psrContext = (new \Enqueue\ConnectionFactoryFactory())->create('redis:')->createContext();

// pass redis instance directly
$redis = new \Enqueue\Redis\PhpRedis([ /** redis connection options */ ]);
Expand Down
4 changes: 2 additions & 2 deletions docs/transport/sqs.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ $psrContext = $factory->createContext();
$client = new Aws\Sqs\SqsClient([ /* ... */ ]);
$factory = new SqsConnectionFactory($client);

// if you have enqueue/enqueue library installed you can use a function from there to create the context
$psrContext = \Enqueue\dsn_to_context('sqs:');
// if you have enqueue/enqueue library installed you can use a factory to build context from DSN
$psrContext = (new \Enqueue\ConnectionFactoryFactory())->create('sqs:')->createContext();
```

## Declare queue.
Expand Down
4 changes: 2 additions & 2 deletions docs/transport/stomp.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ $factory = new StompConnectionFactory('stomp://example.com:1000?login=theLogin')

$psrContext = $factory->createContext();

// if you have enqueue/enqueue library installed you can use a function from there to create the context
$psrContext = \Enqueue\dsn_to_context('stomp:');
// if you have enqueue/enqueue library installed you can use a factory to build context from DSN
$psrContext = (new \Enqueue\ConnectionFactoryFactory())->create('stomp:')->createContext();
```

## Send message to topic
Expand Down
4 changes: 4 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@
<testsuite name="async-command">
<directory>pkg/async-command/Tests</directory>
</testsuite>

<testsuite name="dsn">
<directory>pkg/dsn/Tests</directory>
</testsuite>
</testsuites>

<php>
Expand Down
56 changes: 30 additions & 26 deletions pkg/dbal/DbalConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,51 +92,55 @@ private function establishConnection()
return $this->connection;
}

/**
* @param string $dsn
*
* @return array
*/
private function parseDsn($dsn)
private function parseDsn(string $dsn): array
{
if (false === strpos($dsn, ':')) {
throw new \LogicException(sprintf('The DSN is invalid. It does not have scheme separator ":".'));
}

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

if (!preg_match('/^([0-9a-z_]+):(.+)?$/', $dsn, $matches)) {
throw new \LogicException('Schema is empty');
list($scheme) = explode(':', $dsn, 2);

$scheme = strtolower($scheme);
if (false == preg_match('/^[a-z\d+-.]*$/', $scheme)) {
throw new \LogicException('The DSN is invalid. Scheme contains illegal symbols.');
}
$schema = $matches[1];

$supported = [
'db2' => true,
'ibm_db2' => true,
'mssql' => true,
'pdo_sqlsrv' => true,
'mysql' => true,
'mysql2' => true,
'pdo_mysql' => true,
'pgsql' => true,
'postgres' => true,
'postgresql' => true,
'pdo_pgsql' => true,
'sqlite' => true,
'sqlite3' => true,
'pdo_sqlite' => true,
'db2' => 'db2',
'ibm-db2' => 'ibm-db2',
'mssql' => 'mssql',
'sqlsrv+pdo' => 'pdo_sqlsrv',
'mysql' => 'mysql',
'mysql2' => 'mysql2',
'mysql+pdo' => 'pdo_mysql',
'pgsql' => 'pgsql',
'postgres' => 'postgres',
'pgsql+pdo' => 'pdo_pgsql',
'sqlite' => 'sqlite',
'sqlite3' => 'sqlite3',
'sqlite+pdo' => 'pdo_sqlite',
];

if (false == isset($supported[$schema])) {
if (false == isset($supported[$scheme])) {
throw new \LogicException(sprintf(
'The given DSN schema "%s" is not supported. There are supported schemes: "%s".',
$schema,
$scheme,
implode('", "', array_keys($supported))
));
}

$doctrineScheme = $supported[$scheme];

return [
'lazy' => true,
'connection' => [
'url' => $schema.':' === $dsn ? $schema.'://root@localhost' : $dsn,
'url' => $scheme.':' === $dsn ?
$doctrineScheme.'://root@localhost' :
str_replace($scheme, $doctrineScheme, $dsn),
],
];
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/dbal/Tests/DbalConnectionFactoryConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ public function testThrowNeitherArrayStringNorNullGivenAsConfig()
public function testThrowIfSchemeIsNotSupported()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('The given DSN schema "http" is not supported. There are supported schemes: "db2", "ibm_db2", "mssql", "pdo_sqlsrv", "mysql", "mysql2", "pdo_mysql", "pgsql", "postgres", "postgresql", "pdo_pgsql", "sqlite", "sqlite3", "pdo_sqlite"');
$this->expectExceptionMessage('The given DSN schema "http" is not supported. There are supported schemes: "db2", "ibm-db2", "mssql", "sqlsrv+pdo", "mysql", "mysql2", "mysql+pdo", "pgsql", "postgres", "pgsql+pdo", "sqlite", "sqlite3", "sqlite+pdo".');

new DbalConnectionFactory('http://example.com');
}

public function testThrowIfDsnCouldNotBeParsed()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Schema is empty');
$this->expectExceptionMessage('The DSN is invalid. It does not have scheme separator ":".');

new DbalConnectionFactory('invalidDSN');
}
Expand Down Expand Up @@ -78,7 +78,7 @@ public static function provideConfigs()
];

yield [
'pdo_mysql:',
'mysql+pdo:',
[
'connection' => [
'url' => 'pdo_mysql://root@localhost',
Expand Down Expand Up @@ -114,7 +114,7 @@ public static function provideConfigs()
];

yield [
'pdo_mysql://user:pass@host:10001/db',
'mysql+pdo://user:pass@host:10001/db',
[
'connection' => [
'url' => 'pdo_mysql://user:pass@host:10001/db',
Expand Down
6 changes: 6 additions & 0 deletions pkg/dsn/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*~
/composer.lock
/composer.phar
/phpunit.xml
/vendor/
/.idea/
21 changes: 21 additions & 0 deletions pkg/dsn/.travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
sudo: false

git:
depth: 10

language: php

php:
- '7.1'
- '7.2'

cache:
directories:
- $HOME/.composer/cache

install:
- composer self-update
- composer install --prefer-source

script:
- vendor/bin/phpunit
Loading