Download the PHP package nyholm/dsn without Composer
On this page you can find all versions of the php package nyholm/dsn. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package dsn
Short Description Parse your DSN strings in a powerful and flexible way
License MIT
Homepage http://tnyholm.se
Informations about the package dsn
DSN parser
Parse DSN strings into value objects to make them easier to use, pass around and manipulate.
Install
Via Composer
Quick usage
The DSN string format
A DSN is a string used to configure many services. A common DSN may look like a URL, other look like a file path.
Both types can have parameters, user, password. The exact definition we are using is found at the bottom of the page.
DSN Functions
A DSN may contain zero or more functions. The DSN parser supports a function syntax but not functionality itself. The function arguments must be separated with space or comma. Here are some example functions.
Parsing
There are two methods for parsing; DsnParser::parse()
and DsnParser::parseFunc()
.
The latter is for situations where DSN functions are supported.
If functions are supported (like in the Symfony Mailer component) we can use DsnParser::parseFunc()
:
When using DsnParser::parseFunc()
on a string that does not contain any DSN functions,
the parser will automatically add a default "dsn" function. This is added to provide
a consistent return type of the method.
The string redis://127.0.0.1
will automatically be converted to dsn(redis://127.0.0.1)
when using DsnParser::parseFunc()
.
Parsing invalid DSN
If you try to parse an invalid DSN string a InvalidDsnException
will be thrown.
Consuming
The result of parsing a DSN string is a DsnFunction
or Dsn
. A DsnFunction
has
a name
, argument
and may have parameters
. An argument is either a DsnFunction
or a Dsn
.
A Dsn
could be a Path
or Url
. All 3 objects has methods for getting parts of
the DSN string.
getScheme()
getUser()
getPassword()
getHost()
getPort()
getPath()
getParameters()
You may also replace parts of the DSN with the with*
methods. A DSN is immutable
and you will get a new object back.
Not supported
Smart merging of options
The current DSN is valid, but it is up to the consumer to make sure both host1 and
host2 has global_option
.
Special DSN
The following DSN syntax are not supported.
We do not support DSN strings for ODBC connections like:
However, we do support "only parameters":
Definition
There is no official DSN RFC. We have defined a DSN configuration string as using the following definition. The "URL looking" parts of a DSN is based from RFC 3986.
Example of formats that are supported:
- scheme://127.0.0.1/foo/bar?key=value
- scheme://user:[email protected]/foo/bar?key=value
- scheme:///var/local/run/memcached.socket?weight=25
- scheme://user:pass@/var/local/run/memcached.socket?weight=25
- scheme:?host[localhost]&host[localhost:12345]=3
- scheme://a
- scheme://
- server:80