Skip to content

[fs] Use enqueue/dsn to parse DSN #612

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 2 commits into from
Nov 3, 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
4 changes: 2 additions & 2 deletions docs/transport/filesystem.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ $connectionFactory = new FsConnectionFactory('file:');
$connectionFactory = new FsConnectionFactory('/path/to/queue/dir');

// same as above
$connectionFactory = new FsConnectionFactory('file://path/to/queue/dir');
$connectionFactory = new FsConnectionFactory('file:///path/to/queue/dir');

// with options
$connectionFactory = new FsConnectionFactory('file://path/to/queue/dir?pre_fetch_count=1');
$connectionFactory = new FsConnectionFactory('file:///path/to/queue/dir?pre_fetch_count=1');

// as an array
$connectionFactory = new FsConnectionFactory([
Expand Down
14 changes: 14 additions & 0 deletions pkg/dsn/Dsn.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,20 @@ public function getInt(string $name, int $default = null): ?int
return (int) $value;
}

public function getOctal(string $name, int $default = null): ?int
{
$value = $this->getQueryParameter($name);
if (null === $value) {
return $default;
}

if (false == preg_match('/^0[\+\-]?[0-7]*$/', $value)) {
throw InvalidQueryParameterTypeException::create($name, 'integer');
}

return intval($value, 8);
}

public function getFloat(string $name, float $default = null): ?float
{
$value = $this->getQueryParameter($name);
Expand Down
44 changes: 44 additions & 0 deletions pkg/dsn/Tests/DsnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ public function testShouldParseQueryParameterAsInt(string $parameter, int $expec
$this->assertSame($expected, $dsn->getInt('aName'));
}

/**
* @dataProvider provideOctalQueryParameters
*/
public function testShouldParseQueryParameterAsOctalInt(string $parameter, int $expected)
{
$dsn = new Dsn('foo:?aName='.$parameter);

$this->assertSame($expected, $dsn->getOctal('aName'));
}

public function testShouldReturnDefaultIntIfNotSet()
{
$dsn = new Dsn('foo:');
Expand All @@ -124,6 +134,33 @@ public function testThrowIfQueryParameterNotInt()
$dsn->getInt('aName');
}

public function testThrowIfQueryParameterNotOctalButString()
{
$dsn = new Dsn('foo:?aName=notInt');

$this->expectException(InvalidQueryParameterTypeException::class);
$this->expectExceptionMessage('The query parameter "aName" has invalid type. It must be "integer"');
$dsn->getOctal('aName');
}

public function testThrowIfQueryParameterNotOctalButDecimal()
{
$dsn = new Dsn('foo:?aName=123');

$this->expectException(InvalidQueryParameterTypeException::class);
$this->expectExceptionMessage('The query parameter "aName" has invalid type. It must be "integer"');
$dsn->getOctal('aName');
}

public function testThrowIfQueryParameterInvalidOctal()
{
$dsn = new Dsn('foo:?aName=0128');

$this->expectException(InvalidQueryParameterTypeException::class);
$this->expectExceptionMessage('The query parameter "aName" has invalid type. It must be "integer"');
$dsn->getOctal('aName');
}

/**
* @dataProvider provideFloatQueryParameters
*/
Expand Down Expand Up @@ -204,6 +241,13 @@ public static function provideIntQueryParameters()
yield ['+123', 123];

yield ['-123', -123];

yield ['010', 10];
}

public static function provideOctalQueryParameters()
{
yield ['010', 8];
}

public static function provideFloatQueryParameters()
Expand Down
63 changes: 28 additions & 35 deletions pkg/fs/FsConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Enqueue\Fs;

use Enqueue\Dsn\Dsn;
use Interop\Queue\ConnectionFactory;
use Interop\Queue\Context;

Expand All @@ -27,23 +28,31 @@ class FsConnectionFactory implements ConnectionFactory
* or
*
* file: - create queue files in tmp dir.
* file://home/foo/enqueue
* file://home/foo/enqueue?pre_fetch_count=20&chmod=0777
* file:///home/foo/enqueue
* file:///home/foo/enqueue?pre_fetch_count=20&chmod=0777
*
* @param array|string|null $config
*/
public function __construct($config = 'file:')
{
if (empty($config) || 'file:' === $config) {
$config = ['path' => sys_get_temp_dir().'/enqueue'];
$config = $this->parseDsn('file://'.sys_get_temp_dir().'/enqueue');
} elseif (is_string($config)) {
$config = $this->parseDsn($config);
if ('/' === $config[0]) {
$config = $this->parseDsn('file://'.$config);
} else {
$config = $this->parseDsn($config);
}
} elseif (is_array($config)) {
} else {
throw new \LogicException('The config must be either an array of options, a DSN string or null');
}

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

if (empty($this->config['path'])) {
throw new \LogicException('The path option must be set.');
}
}

/**
Expand All @@ -61,39 +70,23 @@ public function createContext(): Context

private function parseDsn(string $dsn): array
{
if ($dsn && '/' === $dsn[0]) {
return ['path' => $dsn];
}

if (false === strpos($dsn, 'file:')) {
throw new \LogicException(sprintf('The given DSN "%s" is not supported. Must start with "file:".', $dsn));
}

$dsn = substr($dsn, 7);

$path = parse_url(https://github.com/$dsn,%20PHP_URL_PATH);
$query = parse_url(https://github.com/$dsn,%20PHP_URL_QUERY);

if ('/' != $path[0]) {
throw new \LogicException(sprintf('Failed to parse DSN path "%s". The path must start with "/"', $path));
$dsn = new Dsn($dsn);

$supportedSchemes = ['file'];
if (false == in_array($dsn->getSchemeProtocol(), $supportedSchemes, true)) {
throw new \LogicException(sprintf(
'The given scheme protocol "%s" is not supported. It must be one of "%s"',
$dsn->getSchemeProtocol(),
implode('", "', $supportedSchemes)
));
}

if ($query) {
$config = [];
parse_str($query, $config);
}

if (isset($config['pre_fetch_count'])) {
$config['pre_fetch_count'] = (int) $config['pre_fetch_count'];
}

if (isset($config['chmod'])) {
$config['chmod'] = intval($config['chmod'], 8);
}

$config['path'] = $path;

return $config;
return array_filter(array_replace($dsn->getQuery(), [
'path' => $dsn->getPath(),
'pre_fetch_count' => $dsn->getInt('pre_fetch_count'),
'chmod' => $dsn->getOctal('chmod'),
'polling_interval' => $dsn->getInt('polling_interval'),
]), function ($value) { return null !== $value; });
}

private function defaultConfig(): array
Expand Down
18 changes: 14 additions & 4 deletions pkg/fs/Tests/FsConnectionFactoryConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,30 @@ public function testThrowNeitherArrayStringNorNullGivenAsConfig()
new FsConnectionFactory(new \stdClass());
}

public function testThrowIfSchemeIsNotAmqp()
public function testThrowIfSchemeIsNotFileScheme()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('The given DSN "http://example.com" is not supported. Must start with "file:');
$this->expectExceptionMessage('The given scheme protocol "http" is not supported. It must be one of "file"');

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

public function testThrowIfDsnCouldNotBeParsed()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Failed to parse DSN path ":@/". The path must start with "/"');
$this->expectExceptionMessage('The DSN is invalid.');

new FsConnectionFactory('file://:@/');
new FsConnectionFactory('foo');
}

public function testThrowIfArrayConfigGivenWithEmptyPath()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('The path option must be set.');

new FsConnectionFactory([
'path' => null,
]);
}

/**
Expand Down
1 change: 1 addition & 0 deletions pkg/fs/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"require": {
"php": "^7.1.3",
"queue-interop/queue-interop": "0.7.x-dev",
"enqueue/dsn": "0.9.x-dev",
"symfony/filesystem": "^3.4|^4",
"makasim/temp-file": "^0.2@stable"
},
Expand Down