Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Draft DSN
  • Loading branch information
Nyholm committed May 28, 2020
commit 4e705f1c8de0f4f7923600794b2d10b5c05972c8
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
=========

79 changes: 79 additions & 0 deletions src/Symfony/Component/Dsn/Configuration/Dsn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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 <[email protected]>
*/
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;
}
}
78 changes: 78 additions & 0 deletions src/Symfony/Component/Dsn/Configuration/DsnFunction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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 <[email protected]>
*/
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;
}

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

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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 <[email protected]>
*/
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 getPath(): string
{
return $this->path;
}
}
66 changes: 66 additions & 0 deletions src/Symfony/Component/Dsn/Configuration/Url.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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:[email protected]?weight=50
*
* @author Tobias Nyholm <[email protected]>
*/
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;
}
}
50 changes: 50 additions & 0 deletions src/Symfony/Component/Dsn/Configuration/UserPasswordTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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;

trait UserPasswordTrait
{
/**
* @var array{
* user: string|null,
* password: string|null,
* }
*/
private $authentication = ['user' => null, 'password' => null];

/**
* @return array
*/
public function getAuthentication()
{
return $this->authentication;
}

private function setAuthentication(array $authentication): void
{
if (!empty($authentication)) {
$this->authentication = $authentication;
}
}

public function getUser(): ?string
{
return $this->authentication['user'] ?? null;
}

public function getPassword(): ?string
{
return $this->authentication['password'] ?? null;
}
}
Loading