-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[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
[Lock] add the DSN object #33200
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 = []) | ||||||||
{ | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure this is a good idea, by having this method the Why not throw an exception instead? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'], | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
$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 | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I am 👍 for no Except |
||||||||
{ | ||||||||
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; | ||||||||
} | ||||||||
} |
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'); | ||
} | ||
} |
There was a problem hiding this comment.
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