Skip to content

[Lock] add the DSN object #33200

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

Closed
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
3 changes: 2 additions & 1 deletion src/Symfony/Component/Lock/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ CHANGELOG
* added InvalidTtlException
* deprecated `StoreInterface` in favor of `BlockingStoreInterface` and `PersistingStoreInterface`
* `Factory` is deprecated, use `LockFactory` instead

* add `DSN` object to parse dsn in the component

4.2.0
-----

Expand Down
101 changes: 101 additions & 0 deletions src/Symfony/Component/Lock/Dsn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Lock;

use Symfony\Component\Lock\Exception\InvalidArgumentException;

/**
* @author Konstantin Myakshin <molodchick@gmail.com>
* @author Hamza Amrouche <hamza.simperfit@gmail.com>
*/
final class Dsn
{
private $scheme;
private $host;
private $user;
private $password;
private $port;
private $path;
private $options;

public function __construct(string $scheme, string $host, ?string $user = null, ?string $password = null, ?int $port = null, ?string $path = null, array $options = [])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be private to be immutable

{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should assert, that the string values are not empty IMO

$this->scheme = $scheme;
$this->host = $host;
$this->user = $user;
$this->password = $password;
$this->port = $port;
$this->path = $path;
$this->options = $options;
}

public static function isValid(string $dsn)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure this is a good idea, by having this method the Dsn object could be instantiated and be in an invalid state.

Why not throw an exception instead?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I saw this is using a parameter and to the objects state... Then why using it? You can wrap your code in try-catch-block to achieve the same goal, right?

{
return false !== parse_url($dsn);
}

public static function fromString(string $dsn, array $options): self
{
if (false === $parsedDsn = parse_url($dsn)) {
throw new InvalidArgumentException(sprintf('The "%s" DSN is invalid.', $dsn));
}

parse_str($parsedDsn['query'] ?? '', $options);

return new self($parsedDsn['scheme'],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return new self($parsedDsn['scheme'],
return new self(
$parsedDsn['scheme'],

$parsedDsn['host'],
isset($parsedDsn['user']) ? urldecode($parsedDsn['user']) : null,
isset($parsedDsn['pass']) ? urldecode($parsedDsn['pass']) : null,
$parsedDsn['port'] ?? null, $parsedDsn['path'] ?? null,
$options);
}

public function getScheme(): string
Copy link
Contributor

@OskarStark OskarStark Aug 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function getScheme(): string
public function scheme(): string

I am 👍 for no get-prefix. Same for the others

Except getOption(...)

{
return $this->scheme;
}

public function getHost(string $default = null): ?string
{
return $this->host ?? $default;
}

public function getUser(): ?string
{
return $this->user;
}

public function getPassword(): ?string
{
return $this->password;
}

public function getPort(int $default = null): ?int
{
return $this->port ?? $default;
}

public function getPath(string $default = null): ?string
{
return $this->path ?? $default;
}

public function getOption(string $key, $default = null)
{
return $this->options[$key] ?? $default;
}

public function getOptions(): array
{
return $this->options;
}
}
9 changes: 5 additions & 4 deletions src/Symfony/Component/Lock/Store/StoreFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Cache\Adapter\AbstractAdapter;
use Symfony\Component\Cache\Traits\RedisClusterProxy;
use Symfony\Component\Cache\Traits\RedisProxy;
use Symfony\Component\Lock\Dsn;
use Symfony\Component\Lock\Exception\InvalidArgumentException;
use Symfony\Component\Lock\PersistingStoreInterface;

Expand Down Expand Up @@ -50,15 +51,15 @@ public static function createStore($connection)
if (!\is_string($connection)) {
throw new InvalidArgumentException(sprintf('Unsupported Connection: %s.', \get_class($connection)));
}

$validDsn = Dsn::isValid($connection);
switch (true) {
case 'flock' === $connection:
return new FlockStore();
case 0 === strpos($connection, 'flock://'):
return new FlockStore(substr($connection, 8));
case $validDsn && 'flock' === ($parsedDsn = Dsn::fromString($connection, []))->getScheme():
return new FlockStore($parsedDsn->getPath());
case 'semaphore' === $connection:
return new SemaphoreStore();
case class_exists(AbstractAdapter::class) && preg_match('#^[a-z]++://#', $connection):
case class_exists(AbstractAdapter::class) && $validDsn:
return static::createStore(AbstractAdapter::createConnection($connection));
default:
throw new InvalidArgumentException(sprintf('Unsupported Connection: %s.', $connection));
Expand Down
43 changes: 43 additions & 0 deletions src/Symfony/Component/Lock/Tests/DsnTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Lock\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Lock\Dsn;

/**
* @author Hamza Amrouche <hamza.simperfit@gmail.com>
*/
class DsnTest extends TestCase
{
public function testIsValid()
{
$this->assertTrue(Dsn::isValid('redis://elsa:secret@localhost:6321/1?test=1'));
}

public function testIsNotValid()
{
$this->assertFalse(Dsn::isValid('gerard:////'));
}

public function testFromString()
{
$parsedDsn = Dsn::fromString('redis://elsa:secret@localhost:6321/1?test=1', []);
$this->assertSame($parsedDsn->getScheme(), 'redis');
$this->assertSame($parsedDsn->getHost(), 'localhost');
$this->assertSame($parsedDsn->getUser(), 'elsa');
$this->assertSame($parsedDsn->getPassword(), 'secret');
$this->assertSame($parsedDsn->getPort(), 6321);
$this->assertSame($parsedDsn->getPath(), '/1');
$this->assertSame($parsedDsn->getOption('test'), '1');
}
}