1. Go to this page and download the library: Download zenstruck/dsn library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
$dsn = Zenstruck\Dsn::parse($someDsnString); // throws Zenstruck\Dsn\Exception\UnableToParse on failure
switch (true) {
case $dsn instanceof Zenstruck\Uri:
// do something with the Uri object
case $dsn instanceof Zenstruck\Uri\Mailto:
// do something with the Mailto object
case $dsn instanceof Decorated:
// do something with the Decorated object (see api below)
case $dsn instanceof Group:
// do something with the Group object (see api below)
}
use Zenstruck\Dsn\Decorated;
use Zenstruck\Dsn\Group;
use Zenstruck\Uri;
class TransportFactory
{
public function create(\Stringable $dsn): TransportInterface
{
if ($dsn instanceof Uri && $dsn->scheme()->equals('smtp')) {
return new SmtpTransport(
host: $dsn->host()->toString(),
user: $dsn->user(),
password: $dsn->pass(),
port: $dsn->port(),
);
}
if ($dsn instanceof Uri && $dsn->scheme()->equals('mailchimp')) {
return new MailchimpTransport(apiKey: $dsn->user());
}
if ($dsn instanceof Uri && $dsn->scheme()->equals('postmark')) {
return new PostmarkTransport(apiKey: $dsn->user());
}
if ($dsn instanceof Decorated && $dsn->scheme()->equals('retry')) {
return new RetryTransport(
transport: $this->create($dsn->inner()), // recursively build inner transport
times: $dsn->query()->getInt('times', 5), // default to 5 retries if not set
);
}
if ($dsn instanceof Group && $dsn->scheme()->equals('round+robin')) {
return new RoundRobinTransport(
transports: array_map(fn($dsn) => $this->create($dsn), $dsn->children()), // recursively build inner transports
strategy: $dsn->query()->get('strategy', 'random'), // default to "random" strategy if not set
);
}
throw new \LogicException("Unable to parse transport DSN: {$dsn}.");
}
}
$parser = new Zenstruck\Dsn\Parser\ChainParser([$customParser1, $customParser1]);
$parser->parse('some-dsn'); // \Stringable object
/** @var SymfonyCache|Psr6Cache|Psr16Cache $cache */
/** @var Zenstruck\Dsn\Parser $inner */
$parser = new \Zenstruck\Dsn\Parser\CacheParser($parser, $cache);
$parser->parse('some-dsn'); // \Stringable (caches this object)
$parser->parse('some-dsn'); // \Stringable (retrieved from cache)
use Zenstruck\Dsn\Exception\UnableToParse;
use Zenstruck\Dsn\Parser;
class MyParser implements Parser
{
public function parse(string $dsn): \Stringable
{
// determine if $dsn is parsable and return a \Stringable DSN object
throw UnableToParse::value($dsn); // important when using in a chain parser
}
}
// standalone
$parser = new MyParser();
$parser->parse('some-dsn');
// add to ChainParser
$parser = new Zenstruck\Dsn\Parser\ChainParser([new MyParser()]);
$parser->parse('some-dsn');