Skip to content

Commit 5084c53

Browse files
committed
Finish up docs for v3
- Finish migration guide - Add docs on the caching decorator - Fix links + other minor alterations Signed-off-by: George Steel <george@net-glue.co.uk>
1 parent f868138 commit 5084c53

File tree

5 files changed

+139
-10
lines changed

5 files changed

+139
-10
lines changed

docs/book/v3/application-integration/stand-alone.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ foreach ($paginator as $item) {
9090
}
9191
```
9292

93-
Retrieving the [current status data of the paginator](../usage.md#listing-of-properties):
93+
Retrieving the [current status data of the paginator](../usage.md#inspecting-paginator-state):
9494

9595
```php
9696
var_dump($paginator->getPages()->previous); // 1

docs/book/v3/caching.md

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# Caching paginator pages
2+
3+
It is possible to cache pagination results by wrapping adapters in the shipped `CachingAdapter`.
4+
The `CachingAdapter` requires a pagination adapter, a PSR cache item pool and a unique cache-key prefix.
5+
6+
```php
7+
use DateInterval;
8+
use Laminas\Paginator\Adapter\AdapterInterface;
9+
use Psr\Cache\CacheItemPoolInterface;
10+
11+
final readonly class CachingAdapter implements AdapterInterface
12+
{
13+
/**
14+
* @param AdapterInterface<TKey, TValue> $adapter
15+
* @param non-empty-string $cacheKeyPrefix
16+
*/
17+
public function __construct(
18+
private AdapterInterface $adapter,
19+
private string $cacheKeyPrefix,
20+
private CacheItemPoolInterface $cache,
21+
private DateInterval|null $ttl,
22+
) {
23+
}
24+
25+
// ...
26+
}
27+
```
28+
29+
To _manually_ create a paginator that will cache results as they are retrieved, the following would be required:
30+
31+
```php
32+
use Laminas\Paginator\Adapter\AdapterInterface;
33+
use Laminas\Paginator\Adapter\CachingAdapter;
34+
use Laminas\Paginator\Paginator;
35+
use Psr\Cache\CacheItemPoolInterface;
36+
37+
$adapter = new \SpecialCustomAdapter(/** With some data that needs to be cached */);
38+
39+
// Assume we have some ready-prepared objects:
40+
assert($cachePool instanceof CacheItemPoolInterface);
41+
assert($adapter instanceof AdapterInterface);
42+
43+
$paginator = new Paginator(
44+
new CachingAdapter(
45+
$adapter,
46+
'my-unique-prefix',
47+
$cachePool,
48+
new DateInterval('PT30M'),
49+
),
50+
10,
51+
10,
52+
'Sliding',
53+
);
54+
```
55+
56+
The above example assumes that `\SpecialCustomAdapter` is an adapter that makes expensive database calls or other I/O where caching is a useful performance improvement.
57+
58+
The cache pool argument can be any [PSR-6](https://www.php-fig.org/psr/psr-6/) implementation.
59+
[laminas-cache](https://docs.laminas.dev/laminas-cache/v4/psr6/) provides a PSR-6 implementation along with [many other libraries](https://packagist.org/providers/psr/cache-implementation).
60+
61+
Paginator assumes that you will likely use a single cache pool for all paginators, therefore a unique key prefix is required to prevent cache-key collisions with other paginator instances used elsewhere in your application.
62+
63+
Finally, you can also provide an optional TTL in the form of a [`DateInterval`](https://www.php.net/manual/class.dateinterval.php) object.
64+
65+
## `PaginatorFactory` integration and setting defaults
66+
67+
The shipped `PaginatorFactory` class is capable of decorating adapters for you, providing some configuration is in-place, thereby reducing the previous example to:
68+
69+
```php
70+
use Laminas\Paginator\PaginatorFactory;
71+
use Psr\Container\ContainerInterface;
72+
73+
assert($container instanceof ContainerInterface);
74+
75+
$adapter = new \SpecialCustomAdapter(/** With some data that needs to be cached */);
76+
77+
$factory = $container->get(PaginatorFactory::class);
78+
$paginator = $factory->withCachingAdapter($adapter, 'my-unique-prefix');
79+
```
80+
81+
Using the factory assumes that a single cache item pool will be used for all paginators _(Which is why the unique prefix is necessary)_.
82+
To take advantage of the paginator factory, you must ensure that you configure the necessary services.
83+
The following example assumes that you are using [Laminas Service Manager](https://docs.laminas.dev/laminas-servicemanager) for dependency injection.
84+
85+
```php
86+
return [
87+
// Laminas Service Manager Configuration:
88+
'dependencies' => [
89+
'factories' => [
90+
'PaginatorCachePool' => \SomeFactoryThatWillProduceACacheItemPoolInterface::class,
91+
],
92+
],
93+
94+
// Telling the paginator factory which "Service" to pull from the container:
95+
'paginator' => [
96+
'defaultCache' => 'PaginatorCachePool',
97+
'defaultCacheTTL' => 'PT30M',
98+
],
99+
];
100+
```
101+
102+
Further information:
103+
104+
- [Custom Paginator Adapters](./advanced.md#custom-data-source-adapters)
105+
- [More information on writing factories with Laminas Service Manager](https://docs.laminas.dev/laminas-servicemanager/v4/configuring-the-service-manager/#factories)
106+
- [Using Laminas Cache as a PSR-6 Cache Pool](https://docs.laminas.dev/laminas-cache/v4/psr6/)
107+
- [The PSR-6 Specification](https://www.php-fig.org/psr/psr-6/)

docs/book/v3/migration/from-v2-to-v3.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Version 3 of Laminas Paginator contains several backward incompatible changes.
1010

1111
### Re-worked, Optional Caching with `psr/cache`
1212

13-
With the removal of caching from the paginator itself, it is now possible to decorate adapters as needed with the `CachingAdapter`:
13+
With the [removal of caching](#caching) from the paginator itself, it is now possible to decorate adapters as needed with the `CachingAdapter`:
1414

1515
```php
1616
use Laminas\Paginator\Adapter\ArrayAdapter;
@@ -26,7 +26,7 @@ $paginator = new Paginator(
2626
);
2727
```
2828

29-
[Further Documentation](CachingDocs)
29+
[Further Documentation](../caching.md)
3030

3131
## Bug Fixes & General Improvements
3232

@@ -100,7 +100,7 @@ $json = json_encode($paginator->getCurrentItems());
100100
### Removal of "Global Config" via Static Properties
101101

102102
The method `Laminas\Paginator\Paginator::setGlobalConfig()` has been removed.
103-
To create pagers with consistent and centralised configuration defaults, use the [newly introduced `PaginatorFactory`](PagerFactory).
103+
To create pagers with consistent and centralised configuration defaults, use the [newly introduced `PaginatorFactory`](../configuration.md#the-paginator-factory-service).
104104

105105
### Removal of Unused Exception Types
106106

@@ -118,7 +118,4 @@ It is possible to use the shipped `ConfigProvider` to ensure that the library is
118118
### Removal of the `Factory` class
119119

120120
The class `Laminas\Paginator\Factory` has been removed.
121-
Its conceptual functionality is replaced with the `Laminas\Paginator\PaginatorFactory` class which is [documented in detail here](PagerFactory).
122-
123-
[PagerFactory]: ../does-not-exist-yet.md
124-
[CachingDocs]: ../does-not-exist-yet.md
121+
Its conceptual functionality is replaced with the `Laminas\Paginator\PaginatorFactory` class which is [documented in detail here](../configuration.md#the-paginator-factory-service).

docs/book/v3/usage.md

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ return [
4848
];
4949
```
5050

51-
With the above route (and using [laminas-mvc](https://docs.laminas.dev/laminas-mvc/) controllers), you might set the current page number in your controller action like so:
51+
With the above route, in a [Mezzio](https://docs.mezzio.dev) application, you might set the current page number in your middleware like so:
5252

5353
```php
54-
$paginator->setCurrentPageNumber($this->params()->fromRoute('page'));
54+
$paginator->setCurrentPageNumber((int) $request->getAttribute('page'));
5555
```
5656

5757
Now we have a paginator filled with data, and the current page number is known, we can iterate over the paginated items:
@@ -277,3 +277,27 @@ $paginator->getPages(new ScrollingStyle());
277277
```
278278

279279
For further details on the other available options; see the [Configuration chapter](configuration.md).
280+
281+
## Inspecting paginator state
282+
283+
The paginator keeps track of its current position and provides a number of methods to retrieve information about its state:
284+
285+
```php
286+
assert($pager instanceof Laminas\Paginator\Paginator);
287+
288+
printf("The current page number is %d\n", $pager->getCurrentPageNumber());
289+
printf("The current page contains %d items\n", $pager->getCurrentItemCount());
290+
printf("In total there are %d items\n", $pager->getTotalItemCount());
291+
292+
$pagesResult = $pager->getPages();
293+
294+
printf("The first page number is %d\n", $pagesResult->first);
295+
printf("The first page number in range is %d\n", $pagesResult->firstPageInRange);
296+
printf("The last page number is %d\n", $pagesResult->last);
297+
printf("The last page number in range is %d\n", $pagesResult->lastPageInRange);
298+
printf("The next page number is %s\n", $pagesResult->next ?? 'NONE');
299+
printf("The previous page number is %s\n", $pagesResult->previous ?? 'NONE');
300+
printf("The total number of pages is %d\n", $pagesResult->pageCount);
301+
```
302+
303+
The `Laminas\Paginator\Pages` value object retrieved from `$pager->getPages()` provides all the information needed to create a page navigation user interface.

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ nav:
88
- Usage: v3/usage.md
99
- Configuration: v3/configuration.md
1010
- "Advanced Usage": v3/advanced.md
11+
- "Caching Paged Result Sets": v3/caching.md
1112
- 'Application Integration':
1213
- 'Stand-Alone': v3/application-integration/stand-alone.md
1314
- 'Migration':

0 commit comments

Comments
 (0)