8000 Draft DSN · symfony/symfony@9d40c78 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9d40c78

Browse files
committed
Draft DSN
1 parent f9411ab commit 9d40c78

30 files changed

+1058
-126
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/Tests export-ignore
2+
/phpunit.xml.dist export-ignore
3+
/.gitattributes export-ignore
4+
/.gitignore export-ignore

src/Symfony/Component/Dsn/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor/
2+
composer.lock
3+
phpunit.xml
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CHANGELOG
2+
=========
3+
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
/*
3+
* This file is part of the Symfony package.
4+
*
5+
* (c) Fabien Potencier <fabien@symfony.com>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace Symfony\Component\Dsn\Configuration;
12+
13+
/**
14+
* Base DSN object.
15+
*
16+
* Example:
17+
* - null://
18+
* - redis:?host[h1]&host[h2]&host[/foo:]
19+
*
20+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
21+
*/
22+
class Dsn
23+
{
24+
/**
25+
* @var string|null
26+
*/
27+
private $scheme;
28+
29+
/**
30+
* @var array
31+
*/
32+
private $parameters = [];
33+
34+
public function __construct(?string $scheme, array $parameters = [])
35+
{
36+
$this->scheme = $scheme;
37+
$this->parameters = $parameters;
38+
}
39+
40+
public function getScheme(): ?string
41+
{
42+
return $this->scheme;
43+
}
44+
45+
public function getParameters(): array
46+
{
47+
return $this->parameters;
48+
}
49+
50+
public function getParameter(string $key, $default = null)
51+
{
52+
return array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default;
53+
}
54+
55+
public function getHost(): ?string
56+
{
57+
return null;
58+
}
59+
60+
public function getPort(): ?int
61+
{
62+
return null;
63+
}
64+
65+
public function getPath(): ?string
66+
{
67+
return null;
68+
}
69+
70+
public function getUsername(): ?string
71+
{
72+
return null;
73+
}
74+
75+
public function getPassword(): ?string
76+
{
77+
return null;
78+
}
79+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symfony\Component\Dsn\Configuration;
6+
7+
/**
8+
* A function with one or more arguments. The default function is called "dsn".
9+
* Other function may be "failover" or "roundrobin".
10+
*
11+
* Examples:
12+
* - failover(redis://localhost memcached://example.com)
13+
* - dsn(amqp://guest:password@localhost:1234)
14+
* - foobar(amqp://guest:password@localhost:1234 amqp://localhost)?delay=10
15+
*
16+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
17+
*/
18+
class DsnFunction
19+
{
20+
/**
21+
* @var string
22+
*/
23+
private $name;
24+
25+
/**
26+
* @var array
27+
*/
28+
private $arguments;
29+
30+
/**
31+
* @var array
32+
*/
33+
private $parameters;
34+
35+
public function __construct(string $name, array $arguments, array $parameters = [])
36+
{
37+
$this->name = $name;
38+
$this->arguments = $arguments;
39+
$this->parameters = $parameters;
40+
}
41+
42+
public function getName(): string
43+
{
44+
return $this->name;
45+
}
46+
47+
public function getArguments(): array
48+
{
49+
return $this->arguments;
50+
}
51+
52+
public function getParameters(): array
53+
{
54+
return $this->parameters;
55+
}
56+
57+
public function getParameter(string $key, $default = null)
58+
{
59+
return array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default;
60+
}
61+
62+
/**
63+
* @return DsnFunction|Dsn
64+
*/
65+
public function first()
66+
{
67+
return reset($this->arguments);
68+
}
69+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symfony\Component\Dsn\Configuration;
6+
7+
/**
8+
* A "path like" DSN string.
9+
*
10+
* Example:
11+
* - redis:///var/run/redis/redis.sock
12+
* - memcached://user:password@/var/local/run/memcached.socket?weight=25
13+
*
14+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
15+
*/
16+
class Path extends Dsn
17+
{
18+
use UserPasswordTrait;
19+
/**
20+
* @var string
21+
*/
22+
private $path;
23+
24+
public function __construct(?string $scheme, string $path, array $parameters = [], array $authentication = [])
25+
{
26+
$this->path = $path;
27+
$this->setAuthentication($authentication);
28+
parent::__construct($scheme, $parameters);
29+
}
30+
31+
public function getPath(): string
32+
{
33+
return $this->path;
34+
}
35+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symfony\Component\Dsn\Configuration;
6+
7+
/**
8+
* A "URL like" DSN string.
9+
*
10+
* Example:
11+
* - memcached://user:password@127.0.0.1?weight=50
12+
*
13+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
14+
*/
15+
class Url extends Dsn
16+
{
17+
use UserPasswordTrait;
18+
19+
/**
20+
* @var string
21+
*/
22+
private $host;
23+
24+
/**
25+
* @var int|null
26+
*/
27+
private $port;
28+
29+
/**
30+
* @var string|null
31+
*/
32+
private $path;
33+
34+
public function __construct(?string $scheme, string $host, ?int $port = null, ?string $path = null, array $parameters = [], array $authentication = [])
35+
{
36+
$this->host = $host;
37+
$this->port = $port;
38+
$this->path = $path;
39+
$this->setAuthentication($authentication);
40+
parent::__construct($scheme, $parameters);
41+
}
42+
43+
public function getHost(): string
44+
{
45+
return $this->host;
46+
}
47+
48+
public function getPort(): ?int
49+
{
50+
return $this->port;
51+
}
52+
53+
public function getPath(): ?string
54+
{
55+
return $this->path;
56+
}
57+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symfony\Component\Dsn\Configuration;
6+
7+
trait UserPasswordTrait
8+
{
9+
/**
10+
* @var array{
11+
* username: string|null,
12+
* password: string|null,
13+
* }
14+
*/
15+
private $authentication = ['username' => null, 'password' => null];
16+
17+
/**
18+
* @return array
19+
*/
20+
public function getAuthentication()
21+
{
22+
return $this->authentication;
23+
}
24+
25+
private function setAuthentication(array $authentication): void
26+
{
27+
if (!empty($authentication)) {
28+
$this->authentication = $authentication;
29+
}
30+
}
31+
32+
public function getUsername(): ?string
33+
{
34+
return $this->authentication['username'] ?? null;
35+
}
36+
37+
public function getPassword(): ?string
38+
{
39+
return $this->authentication['password'] ?? null;
40+
}
41+
}

0 commit comments

Comments
 (0)
0