Skip to content

Commit 59f93f4

Browse files
committed
Better work with DSN provided as env vars, build factory in runtime.
0 parents  commit 59f93f4

File tree

8 files changed

+367
-0
lines changed

8 files changed

+367
-0
lines changed

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*~
2+
/composer.lock
3+
/composer.phar
4+
/phpunit.xml
5+
/vendor/
6+
/.idea/

.travis.yml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
sudo: false
2+
3+
git:
4+
depth: 10
5+
6+
language: php
7+
8+
php:
9+
- '7.1'
10+
- '7.2'
11+
12+
cache:
13+
directories:
14+
- $HOME/.composer/cache
15+
16+
install:
17+
- composer self-update
18+
- composer install --prefer-source
19+
20+
script:
21+
- vendor/bin/phpunit

Dsn.php

+228
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
<?php
2+
3+
namespace Enqueue\Dsn;
4+
5+
class Dsn
6+
{
7+
/**
8+
* @var string
9+
*/
10+
private $dsn;
11+
12+
/**
13+
* @var string
14+
*/
15+
private $scheme;
16+
17+
/**
18+
* @var string|null
19+
*/
20+
private $user;
21+
22+
/**
23+
* @var string|null
24+
*/
25+
private $password;
26+
27+
/**
28+
* @var string|null
29+
*/
30+
private $host;
31+
32+
/**
33+
* @var int|null
34+
*/
35+
private $port;
36+
37+
/**
38+
* @var string|null
39+
*/
40+
private $path;
41+
42+
/**
43+
* @var string|null
44+
*/
45+
private $queryString;
46+
47+
/**
48+
* @var array
49+
*/
50+
private $query;
51+
52+
/**
53+
* @var string
54+
*/
55+
private $schemeProtocol;
56+
57+
/**
58+
* @var string[]
59+
*/
60+
private $schemeExtensions;
61+
62+
public function __construct(string $dsn)
63+
{
64+
$this->dsn = $dsn;
65+
$this->query = [];
66+
67+
$this->parse($dsn);
68+
}
69+
70+
public function __toString(): string
71+
{
72+
return $this->dsn;
73+
}
74+
75+
public function getDsn(): string
76+
{
77+
return $this->dsn;
78+
}
79+
80+
public function getScheme(): string
81+
{
82+
return $this->scheme;
83+
}
84+
85+
public function getSchemeProtocol(): string
86+
{
87+
return $this->schemeProtocol;
88+
}
89+
90+
/**
91+
* @return string[]
92+
*/
93+
public function getSchemeExtensions(): array
94+
{
95+
return $this->schemeExtensions;
96+
}
97+
98+
public function hasSchemeExtension(string $extension): bool
99+
{
100+
return in_array($extension, $this->schemeExtensions, true);
101+
}
102+
103+
/**
104+
* @return null|string
105+
*/
106+
public function getUser(): ?string
107+
{
108+
return $this->user;
109+
}
110+
111+
/**
112+
* @return null|string
113+
*/
114+
public function getPassword(): ?string
115+
{
116+
return $this->password;
117+
}
118+
119+
/**
120+
* @return null|string
121+
*/
122+
public function getHost(): ?string
123+
{
124+
return $this->host;
125+
}
126+
127+
/**
128+
* @return int|null
129+
*/
130+
public function getPort(): ?int
131+
{
132+
return $this->port;
133+
}
134+
135+
/**
136+
* @return null|string
137+
*/
138+
public function getPath(): ?string
139+
{
140+
return $this->path;
141+
}
142+
143+
/**
144+
* @return null|string
145+
*/
146+
public function getQueryString(): ?string
147+
{
148+
return $this->queryString;
149+
}
150+
151+
/**
152+
* @return array
153+
*/
154+
public function getQuery(): array
155+
{
156+
return $this->query;
157+
}
158+
159+
public function getQueryParameter(string $name, $default = null)
160+
{
161+
return array_key_exists($name, $this->query) ? $this->query[$name] : $default;
162+
}
163+
164+
public function toArray()
165+
{
166+
return [
167+
'scheme' => $this->scheme,
168+
'schemeProtocol' => $this->schemeProtocol,
169+
'schemeExtensions' => $this->schemeExtensions,
170+
'user' => $this->user,
171+
'password' => $this->password,
172+
'host' => $this->host,
173+
'port' => $this->port,
174+
'path' => $this->path,
175+
'queryString' => $this->queryString,
176+
'query' => $this->query,
177+
];
178+
}
179+
180+
private function parse(string $dsn): void
181+
{
182+
if (false === strpos($dsn, ':')) {
183+
throw new \LogicException(sprintf('The DSN is invalid. It does not have scheme separator ":".'));
184+
}
185+
186+
list($scheme, $dsnWithoutScheme) = explode(':', $dsn, 2);
187+
if (false == preg_match('/[\w\d+-.]/', $scheme)) {
188+
throw new \LogicException('The DSN is invalid. Scheme contains illegal symbols.');
189+
}
190+
191+
$scheme = strtolower($scheme);
192+
193+
$schemeParts = explode('+', $scheme);
194+
$this->scheme = $scheme;
195+
$this->schemeProtocol = $schemeParts[0];
196+
197+
unset($schemeParts[0]);
198+
$this->schemeExtensions = $schemeParts;
199+
200+
if ($host = parse_url($dsn, PHP_URL_HOST)) {
201+
$this->host = $host;
202+
}
203+
204+
if ($port = parse_url($dsn, PHP_URL_PORT)) {
205+
$this->port = (int) $port;
206+
}
207+
208+
if ($user = parse_url($dsn, PHP_URL_USER)) {
209+
$this->user = $user;
210+
}
211+
212+
if ($password = parse_url($dsn, PHP_URL_PASS)) {
213+
$this->password = $password;
214+
}
215+
216+
if ($path = parse_url($dsn, PHP_URL_PATH)) {
217+
$this->path = $path;
218+
}
219+
220+
if ($queryString = parse_url($dsn, PHP_URL_QUERY)) {
221+
$this->queryString = $queryString;
222+
223+
$query = [];
224+
parse_str($queryString, $query);
225+
$this->query = $query;
226+
}
227+
}
228+
}

LICENSE

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
Copyright (c) 2018 Kotliar Maksym
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is furnished
9+
to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all
12+
copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
THE SOFTWARE.

README.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Enqueue. Parse DSN class
2+
3+
## Resources
4+
5+
* [Site](https://enqueue.forma-pro.com/)
6+
* [Documentation](https://github.com/php-enqueue/enqueue-dev/blob/master/docs/index.md)
7+
* [Questions](https://gitter.im/php-enqueue/Lobby)
8+
* [Issue Tracker](https://github.com/php-enqueue/enqueue-dev/issues)
9+
10+
## Developed by Forma-Pro
11+
12+
Forma-Pro is a full stack development company which interests also spread to open source development.
13+
Being a team of strong professionals we have an aim an ability to help community by developing cutting edge solutions in the areas of e-commerce, docker & microservice oriented architecture where we have accumulated a huge many-years experience.
14+
Our main specialization is Symfony framework based solution, but we are always looking to the technologies that allow us to do our job the best way. We are committed to creating solutions that revolutionize the way how things are developed in aspects of architecture & scalability.
15+
16+
If you have any questions and inquires about our open source development, this product particularly or any other matter feel free to contact at opensource@forma-pro.com
17+
18+
## License
19+
20+
It is released under the [MIT License](LICENSE).

Tests/DsnTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Enqueue\Dsn\Tests;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
class DsnTest extends TestCase
8+
{
9+
}

composer.json

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "enqueue/dsn",
3+
"type": "library",
4+
"description": "Parse DSN",
5+
"keywords": ["dsn", "parse"],
6+
"homepage": "https://enqueue.forma-pro.com/",
7+
"license": "MIT",
8+
"require": {
9+
"php": "^7.1.3"
10+
},
11+
"require-dev": {
12+
"phpunit/phpunit": "~5.4.0"
13+
},
14+
"support": {
15+
"email": "opensource@forma-pro.com",
16+
"issues": "https://github.com/php-enqueue/enqueue-dev/issues",
17+
"forum": "https://gitter.im/php-enqueue/Lobby",
18+
"source": "https://github.com/php-enqueue/enqueue-dev",
19+
"docs": "https://github.com/php-enqueue/enqueue-dev/blob/master/docs/index.md"
20+
},
21+
"autoload": {
22+
"psr-4": { "Enqueue\\Dsn\\": "" },
23+
"exclude-from-classmap": [
24+
"/Tests/"
25+
]
26+
},
27+
"minimum-stability": "dev",
28+
"extra": {
29+
"branch-alias": {
30+
"dev-master": "0.9.x-dev"
31+
}
32+
}
33+
}

phpunit.xml.dist

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="false"
12+
bootstrap="./vendor/autoload.php"
13+
>
14+
15+
<testsuites>
16+
<testsuite name="Enqueue DSN">
17+
<directory>./Tests</directory>
18+
</testsuite>
19+
</testsuites>
20+
21+
<filter>
22+
<whitelist>
23+
<directory suffix=".php">.</directory>
24+
<exclude>
25+
<directory>./vendor</directory>
26+
<directory>./Tests</directory>
27+
</exclude>
28+
</whitelist>
29+
</filter>
30+
</phpunit>

0 commit comments

Comments
 (0)