Dsn.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. namespace Symfony\Component\Notifier\Transport;
  11. use Symfony\Component\Notifier\Exception\InvalidArgumentException;
  12. /**
  13. * @author Fabien Potencier <fabien@symfony.com>
  14. *
  15. * @experimental in 5.1
  16. */
  17. final class Dsn
  18. {
  19. private $scheme;
  20. private $host;
  21. private $user;
  22. private $password;
  23. private $port;
  24. private $options;
  25. private $path;
  26. private $dsn;
  27. public function __construct(string $scheme, string $host, ?string $user = null, ?string $password = null, ?int $port = null, array $options = [], ?string $path = null)
  28. {
  29. $this->scheme = $scheme;
  30. $this->host = $host;
  31. $this->user = $user;
  32. $this->password = $password;
  33. $this->port = $port;
  34. $this->options = $options;
  35. $this->path = $path;
  36. }
  37. public static function fromString(string $dsn): self
  38. {
  39. if (false === $parsedDsn = parse_url($dsn)) {
  40. throw new InvalidArgumentException(sprintf('The "%s" notifier DSN is invalid.', $dsn));
  41. }
  42. if (!isset($parsedDsn['scheme'])) {
  43. throw new InvalidArgumentException(sprintf('The "%s" notifier DSN must contain a scheme.', $dsn));
  44. }
  45. if (!isset($parsedDsn['host'])) {
  46. throw new InvalidArgumentException(sprintf('The "%s" notifier DSN must contain a host (use "default" by default).', $dsn));
  47. }
  48. $user = isset($parsedDsn['user']) ? urldecode($parsedDsn['user']) : null;
  49. $password = isset($parsedDsn['pass']) ? urldecode($parsedDsn['pass']) : null;
  50. $port = $parsedDsn['port'] ?? null;
  51. $path = $parsedDsn['path'] ?? null;
  52. parse_str($parsedDsn['query'] ?? '', $query);
  53. $dsnObject = new self($parsedDsn['scheme'], $parsedDsn['host'], $user, $password, $port, $query, $path);
  54. $dsnObject->dsn = $dsn;
  55. return $dsnObject;
  56. }
  57. public function getScheme(): string
  58. {
  59. return $this->scheme;
  60. }
  61. public function getHost(): string
  62. {
  63. return $this->host;
  64. }
  65. public function getUser(): ?string
  66. {
  67. return $this->user;
  68. }
  69. public function getPassword(): ?string
  70. {
  71. return $this->password;
  72. }
  73. public function getPort(int $default = null): ?int
  74. {
  75. return $this->port ?? $default;
  76. }
  77. public function getOption(string $key, $default = null)
  78. {
  79. return $this->options[$key] ?? $default;
  80. }
  81. public function getPath(): ?string
  82. {
  83. return $this->path;
  84. }
  85. public function getOriginalDsn(): string
  86. {
  87. return $this->dsn;
  88. }
  89. }