Skip to content

Commit 3f0f096

Browse files
committed
add advanced usage docs
1 parent 2085a6d commit 3f0f096

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

README.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,112 @@ $factory->create('retry(smtp://kevin:p4ssword:localhost)');
250250
$factory->create('retry(round+robin(mailchimp://key@default postmark://key@default)?strategy=sequential)?times=3');
251251
```
252252

253+
## Advanced Usage
254+
255+
Under the hood `Zenstruck\Dsn::parse()` uses a parsing system for converting DSN
256+
strings to the packaged [DSN objects](#parsing-dsns). You can create your own
257+
parsers by having them implement the `Zenstruck\Dsn\Parser` interface.
258+
259+
> **Note**
260+
> `Zenstruck\Dsn::parse()` is a utility function that only uses the
261+
> [core parsers](#core-parsers). In order to add your [own parsers](#custom-parsers),
262+
> you'll need to manually wire up a [chain parser](#chainparser) that includes them
263+
> and use this for parsing DSNs.
264+
265+
### Core Parsers
266+
267+
#### `UriParser`
268+
269+
Converts url-looking strings to [`Zenstruck\Uri`](#uri) objects.
270+
271+
#### `MailtoParser`
272+
273+
Converts mailto-looking strings to [`Zenstruck\Uri\Mailto`](#mailto) objects.
274+
275+
#### `WrappedParser`
276+
277+
Converts dsn-function-looking strings to [`Zenstruck\Dsn\Decorated`](#decorated) or
278+
[`Zenstruck\Dsn\Group`](#group) objects.
279+
280+
### Utility Parsers
281+
282+
#### `ChainParser`
283+
284+
Wraps a chain of parsers, during `parse()` it loops through these and
285+
attempts to find one that successfully parses a DSN string. It is considered
286+
successful if a `\Stringable` object is returned. If the parser throws a
287+
`Zenstruck\Dsn\Exception\UnableToParse` exception, the next parser in the
288+
chain is tried. Finally, if all the parsers throw `UnableToParse`, this is
289+
thrown.
290+
291+
```php
292+
$parser = new Zenstruck\Dsn\Parser\ChainParser([$customParser1, $customParser1]);
293+
294+
$parser->parse('some-dsn'); // \Stringable object
295+
```
296+
297+
> **Note**
298+
> This parser always contains the [core parsers](#core-parsers) as the last items in
299+
> the chain. [Custom parsers](#custom-parsers) you add to the constructor are attempted
300+
> before these.
301+
302+
#### `CacheParser`
303+
304+
Wraps another parser and an instance of one of these cache interfaces:
305+
306+
- `Symfony\Contracts\Cache\CacheInterface` (Symfony cache)
307+
- `Psr\Cache\CacheItemPoolInterface` (PSR-6 cache)
308+
- `Psr\SimpleCache\CacheInterface` (PSR-16 cache)
309+
310+
The parsed object is cached (keyed by the DSN string) and subsequent
311+
parsing of the same string are retrieved from the cache. This gives a
312+
bit of a performance boost especially for [complex DSNs](#complex-dsns).
313+
314+
```php
315+
/** @var SymfonyCache|Psr6Cache|Psr16Cache $cache */
316+
/** @var Zenstruck\Dsn\Parser $inner */
317+
318+
$parser = new \Zenstruck\Dsn\Parser\CacheParser($parser, $cache);
319+
320+
$parser->parse('some-dsn'); // \Stringable (caches this object)
321+
322+
$parser->parse('some-dsn'); // \Stringable (retrieved from cache)
323+
```
324+
325+
### Custom Parsers
326+
327+
You can create your own parser by creating an object that implements
328+
`Zenstruck\Dsn\Parser`:
329+
330+
```php
331+
use Zenstruck\Dsn\Exception\UnableToParse;
332+
use Zenstruck\Dsn\Parser;
333+
334+
class MyParser implements Parser
335+
{
336+
public function parse(string $dsn): \Stringable
337+
{
338+
// determine if $dsn is parsable and return a \Stringable DSN object
339+
340+
throw UnableToParse::value($dsn); // important when using in a chain parser
341+
}
342+
}
343+
```
344+
345+
Usage:
346+
347+
```php
348+
// standalone
349+
$parser = new MyParser();
350+
351+
$parser->parse('some-dsn');
352+
353+
// add to ChainParser
354+
$parser = new Zenstruck\Dsn\Parser\ChainParser([new MyParser()]);
355+
356+
$parser->parse('some-dsn');
357+
```
358+
253359
## Symfony Bundle
254360

255361
A Symfony Bundle is provided that adds an autowireable `Zenstruck\Dsn\Parser` service.

0 commit comments

Comments
 (0)