Skip to content

Commit 1ad9fd0

Browse files
committed
docs: added logger section
1 parent 0cdd868 commit 1ad9fd0

1 file changed

Lines changed: 115 additions & 14 deletions

File tree

README.md

Lines changed: 115 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,12 @@ class YourApi extends Api
4848
$this->setBaseUrl('https://api.example.com/v1');
4949
}
5050

51-
public function getRecords(int $page = 1): string
51+
public function getPosts(int $page = 1): string
5252
{
53+
// GET https://api.example.com/v1/posts?page=1
5354
return $this->request(
5455
method: 'GET',
55-
path: '/records',
56+
path: '/posts',
5657
query: [
5758
'page' => $page
5859
]
@@ -461,9 +462,12 @@ This means that it will automatically find and install a well-known PSR-18 clien
461462
use ProgrammatorDev\Api\Builder\ClientBuilder;
462463

463464
new ClientBuilder(
464-
?ClientInterface $client = null, // a PSR-18 client
465-
?RequestFactoryInterface $requestFactory = null, // a PSR-17 request factory
466-
?StreamFactoryInterface $streamFactory = null // a PSR-17 stream factory
465+
// a PSR-18 client
466+
?ClientInterface $client = null,
467+
// a PSR-17 request factory
468+
?RequestFactoryInterface $requestFactory = null,
469+
// a PSR-17 stream factory
470+
?StreamFactoryInterface $streamFactory = null
467471
);
468472
```
469473

@@ -498,7 +502,11 @@ class YourApi extends Api
498502
$requestFactory = $streamFactory = new Psr17Factory();
499503

500504
$this->setClientBuilder(
501-
new ClientBuilder($client, $requestFactory, $streamFactory)
505+
new ClientBuilder(
506+
client: $client,
507+
requestFactory: $requestFactory,
508+
streamFactory: $streamFactory
509+
)
502510
);
503511
}
504512
}
@@ -513,7 +521,11 @@ $client = new Psr18Client();
513521
$requestFactory = $streamFactory = new Psr17Factory();
514522

515523
$api->setClientBuilder(
516-
new ClientBuilder($client, $requestFactory, $streamFactory)
524+
new ClientBuilder(
525+
client: $client,
526+
requestFactory: $requestFactory,
527+
streamFactory: $streamFactory
528+
)
517529
);
518530
```
519531

@@ -587,7 +599,7 @@ $api->getClientBuilder()->addPlugin(
587599
> The methods in this section are all public.
588600
> The purpose for that is to allow the end user to configure their own cache adapter.
589601
590-
This library allows configuring the cache-layer of the client for making API requests.
602+
This library allows configuring the cache layer of the client for making API requests.
591603
It uses a standard PSR-6 implementation and provides methods to fine-tune how HTTP caching behaves:
592604
- [PSR-6 compatible implementations](https://packagist.org/providers/psr/cache-implementation)
593605

@@ -596,10 +608,15 @@ use ProgrammatorDev\Api\Builder\CacheBuilder;
596608
use Psr\Cache\CacheItemPoolInterface;
597609

598610
new CacheBuilder(
599-
CacheItemPoolInterface $pool, // a PSR-6 cache adapter
600-
?int $ttl = 60, // default lifetime (in seconds) of cache items
601-
$methods = ['GET', 'HEAD'], // An array of HTTP methods for which caching should be applied
602-
$responseCacheDirectives = ['no-cache', 'max-age'] // An array of cache directives to be compared with the headers of the HTTP response, in order to determine cacheability
611+
// a PSR-6 cache adapter
612+
CacheItemPoolInterface $pool,
613+
// default lifetime (in seconds) of cache items
614+
?int $ttl = 60,
615+
// An array of HTTP methods for which caching should be applied
616+
$methods = ['GET', 'HEAD'],
617+
// An array of cache directives to be compared with the headers of the HTTP response,
618+
// in order to determine cacheability
619+
$responseCacheDirectives = ['no-cache', 'max-age']
603620
);
604621
```
605622

@@ -632,7 +649,10 @@ class YourApi extends Api
632649

633650
// file-based cache adapter with a 1-hour default cache lifetime
634651
$this->setClientBuilder(
635-
new CacheBuilder($pool, 3600)
652+
new CacheBuilder(
653+
pool: $pool,
654+
ttl: 3600
655+
)
636656
);
637657
}
638658

@@ -658,7 +678,88 @@ $api = new YourApi();
658678
$pool = new FilesystemAdapter();
659679

660680
$api->setCacheBuilder(
661-
new CacheBuilder($pool, 3600)
681+
new CacheBuilder(
682+
pool: $pool,
683+
ttl: 3600
684+
)
685+
);
686+
```
687+
688+
### Logger (PSR-3)
689+
690+
> [!IMPORTANT]
691+
> The methods in this section are all public.
692+
> The purpose for that is to allow the end user to configure their own logger adapter.
693+
694+
This library allows configuring a logger to save data for making API requests.
695+
It uses a standard PSR-3 implementation and provides methods to fine-tune how logging behaves:
696+
- [PSR-3 compatible implementations](https://packagist.org/providers/psr/log-implementation)
697+
698+
```php
699+
use ProgrammatorDev\Api\Builder\LoggerBuilder;
700+
use Psr\Log\LoggerInterface;
701+
use Http\Message\Formatter;
702+
use Http\Message\Formatter\SimpleFormatter;
703+
704+
new LoggerBuilder(
705+
// a PSR-3 logger adapter
706+
LoggerInterface $logger,
707+
// determines how the log entries will be formatted when they are written by the logger
708+
// if no formatter is provided, it will default to a SimpleFormatter instance
709+
?Formatter $formatter = null
710+
);
711+
```
712+
713+
```php
714+
use ProgrammatorDev\Api\Builder\LoggerBuilder;
715+
716+
$this->setLoggerBuilder(LoggerBuilder $loggerBuilder): self;
717+
```
718+
719+
```php
720+
use ProgrammatorDev\Api\Builder\LoggerBuilder;
721+
722+
$this->getLoggerBuilder(): LoggerBuilder;
723+
```
724+
725+
As an example:
726+
727+
```php
728+
use ProgrammatorDev\Api\Api;
729+
use ProgrammatorDev\Api\Builder\LoggerBuilder;
730+
use Monolog\Logger;
731+
use Monolog\Handler\StreamHandler;
732+
733+
class YourApi extends Api
734+
{
735+
public function __construct()
736+
{
737+
// ...
738+
739+
$logger = new Logger('api');
740+
$logger->pushHandler(new StreamHandler('/logs/api.log'));
741+
742+
$this->setClientBuilder(
743+
new LoggerBuilder(
744+
logger: $logger
745+
)
746+
);
747+
}
748+
}
749+
```
750+
751+
The same for the end user:
752+
753+
```php
754+
$api = new YourApi();
755+
756+
$logger = new Logger('api');
757+
$logger->pushHandler(new StreamHandler('/logs/api.log'));
758+
759+
$api->setClientBuilder(
760+
new LoggerBuilder(
761+
logger: $logger
762+
)
662763
);
663764
```
664765

0 commit comments

Comments
 (0)