Skip to content

New DSN component #36999

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
wants to merge 6 commits into from
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
4 changes: 4 additions & 0 deletions src/Symfony/Component/Dsn/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/Tests export-ignore
/phpunit.xml.dist export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
3 changes: 3 additions & 0 deletions src/Symfony/Component/Dsn/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor/
composer.lock
phpunit.xml
3 changes: 3 additions & 0 deletions src/Symfony/Component/Dsn/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CHANGELOG
=========

87 changes: 87 additions & 0 deletions src/Symfony/Component/Dsn/Configuration/Dsn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?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\Dsn\Configuration;

/**
* Base DSN object.
*
* Example:
* - null://
* - redis:?host[h1]&host[h2]&host[/foo:]
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class Dsn
{
/**
* @var string|null
*/
private $scheme;

/**
* @var array
*/
private $parameters = [];

public function __construct(?string $scheme, array $parameters = [])
{
$this->scheme = $scheme;
$this->parameters = $parameters;
}

public function getScheme(): ?string
{
return $this->scheme;
}

public function getParameters(): array
{
return $this->parameters;
}

public function getParameter(string $key, $default = null)
{
return \array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default;
}

public function getHost(): ?string
{
return null;
}

public function getPort(): ?int
{
return null;
}

public function getPath(): ?string
{
return null;
}

public function getUser(): ?string
{
return null;
}

public function getPassword(): ?string
{
return null;
}

/**
* @var string
*/
public function __toString()
{
return sprintf('%s://%s', $this->getScheme(), empty($this->parameters) ? '' : '?'.http_build_query($this->parameters));
}
}
89 changes: 89 additions & 0 deletions src/Symfony/Component/Dsn/Configuration/DsnFunction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?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.
*/

declare(strict_types=1);

namespace Symfony\Component\Dsn\Configuration;

/**
* A function with one or more arguments. The default function is called "dsn".
* Other function may be "failover" or "roundrobin".
*
* Examples:
* - failover(redis://localhost memcached://example.com)
* - dsn(amqp://guest:password@localhost:1234)
* - foobar(amqp://guest:password@localhost:1234 amqp://localhost)?delay=10
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class DsnFunction
{
/**
* @var string
*/
private $name;

/**
* @var array
*/
private $arguments;

/**
* @var array
*/
private $parameters;

public function __construct(string $name, array $arguments, array $parameters = [])
{
$this->name = $name;
$this->arguments = $arguments;
$this->parameters = $parameters;
}

public function getName(): string
{
return $this->name;
}

/**
* @return array<DsnFunction|Dsn>
*/
public function getArguments(): array
{
return $this->arguments;
}

public function getParameters(): array
{
return $this->parameters;
}

public function getParameter(string $key, $default = null)
{
return \array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default;
}

/**
* @return DsnFunction|Dsn
*/
public function first()
{
return reset($this->arguments);
}

/**
* @return string
*/
public function __toString()
{
return sprintf('%s(%s)%s', $this->getName(), implode(' ', $this->getArguments()), empty($this->parameters) ? '' : '?'.http_build_query($this->parameters));
}
}
63 changes: 63 additions & 0 deletions src/Symfony/Component/Dsn/Configuration/Path.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?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.
*/

declare(strict_types=1);

namespace Symfony\Component\Dsn\Configuration;

/**
* A "path like" DSN string.
*
* Example:
* - redis:///var/run/redis/redis.sock
* - memcached://user:password@/var/local/run/memcached.socket?weight=25
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class Path extends Dsn
{
use UserPasswordTrait;
/**
* @var string
*/
private $path;

public function __construct(string $scheme, string $path, array $parameters = [], array $authentication = [])
{
$this->path = $path;
$this->setAuthentication($authentication);
parent::__construct($scheme, $parameters);
}

public function getScheme(): string
{
return parent::getScheme();
}

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

/**
* @var string
*/
public function __toString()
{
$parameters = $this->getParameters();

return
$this->getScheme().'://'.
$this->getUserInfoString().
$this->getPath().
(empty($parameters) ? '' : '?'.http_build_query($parameters));
}
}
85 changes: 85 additions & 0 deletions src/Symfony/Component/Dsn/Configuration/Url.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?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.
*/

declare(strict_types=1);

namespace Symfony\Component\Dsn\Configuration;

/**
* A "URL like" DSN string.
*
* Example:
* - memcached://user:password@127.0.0.1?weight=50
* - 127.0.0.1:80
* - amqp://127.0.0.1/%2f/messages
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class Url extends Dsn
{
use UserPasswordTrait;

/**
* @var string
*/
private $host;

/**
* @var int|null
*/
private $port;

/**
* @var string|null
*/
private $path;

public function __construct(?string $scheme, string $host, ?int $port = null, ?string $path = null, array $parameters = [], array $authentication = [])
{
$this->host = $host;
$this->port = $port;
$this->path = $path;
$this->setAuthentication($authentication);
parent::__construct($scheme, $parameters);
}

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

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

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

/**
* @var string
*/
public function __toString()
{
$parameters = $this->getParameters();
$scheme = $this->getScheme();

return
(empty($scheme) ? '' : $scheme.'://').
$this->getUserInfoString().
$this->getHost().
(empty($this->port) ? '' : ':'.$this->port).
($this->getPath() ?? '').
(empty($parameters) ? '' : '?'.http_build_query($parameters));
}
}
Loading