Skip to content

Commit ad4b4e5

Browse files
committed
updated Book tutorial
Signed-off-by: bidi <[email protected]>
1 parent 44fc4a2 commit ad4b4e5

File tree

3 files changed

+91
-11
lines changed

3 files changed

+91
-11
lines changed

docs/book/v7/tutorials/api-evolution.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@ API evolution: Updating an API while keeping it compatible for existing consumer
44

55
## How it works
66

7-
In Dotkernel API we can mark an entire endpoint or a single method as deprecated using attributes on handlers.
7+
In Dotkernel API we can mark an endpoint as deprecated using attributes on handlers.
88
We use response headers to inform the consumers about the future changes by using two new headers:
99

1010
- `Link` - it's a link to the official documentation pointing out the changes that will take place.
1111
- `Sunset` - this header is a date, indicating when the deprecated resource will potentially become unresponsive.
1212

13-
**Both headers are independent, you can use them separately.**
13+
**The above headers are independent, so you can use them separately.**
1414

15-
> Make sure you have the `DeprecationMiddleware:class` piped in your `pipeline` list.
15+
> Make sure you have the `DeprecationMiddleware:class` added to your `pipeline` list.
1616
> In our case it's `config/pipeline.php`.
1717
18-
## Marking an entire endpoint as deprecated
18+
## Marking an endpoint as deprecated
1919

20-
When you want to mark an entire resource as deprecated, you have to use the `ResourceDeprecation` attribute.
20+
When you want to mark a resource as deprecated, you have to use the `ResourceDeprecation` attribute.
2121

2222
```php
2323
...
@@ -33,7 +33,7 @@ class HomeHandler implements RequestHandlerInterface
3333
}
3434
```
3535

36-
In the example above, the `ResourceDeprecation` attribute is attached to the class, marking the entire `/` (home) endpoint as deprecated starting from `2038-01-01`.
36+
In the example above, the `ResourceDeprecation` attribute is attached to the class, marking the `/` (home) endpoint as deprecated starting from `2038-01-01`.
3737

3838
Running the following curl will print out the response headers where we can see the **Sunset** and **Link** headers.
3939

@@ -62,4 +62,4 @@ Vary: Origin
6262
6363
> Deprecations can only be attached to handler classes that implement `RequestHandlerInterface`.
6464
65-
> The `rel` and `type` arguments are optional, they default to `sunset` and `text/html` if no value was provided and are `Link` related parts.
65+
> The `rel` and `type` arguments are optional, they default to `sunset` and `text/html` if no value is provided and are `Link` related parts.

docs/book/v7/tutorials/create-book-module-via-dot-maker.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,25 @@ class Book extends AbstractEntity
175175
return $this;
176176
}
177177

178+
/**
179+
* @return array{
180+
* id: non-empty-string,
181+
* name: non-empty-string,
182+
* author: non-empty-string,
183+
* releaseDate: DateTimeImmutable|null,
184+
* created: DateTimeImmutable|null,
185+
* updated: DateTimeImmutable|null,
186+
* }
187+
*/
178188
public function getArrayCopy(): array
179189
{
180190
return [
181191
'id' => $this->id->toString(),
182192
'name' => $this->getName(),
183193
'author' => $this->getAuthor(),
184194
'releaseDate' => $this->getReleaseDate(),
195+
'created' => $this->created,
196+
'updated' => $this->updated,
185197
];
186198
}
187199
}
@@ -306,6 +318,14 @@ use Api\Book\InputFilter\Input\NameInput;
306318
use Api\Book\InputFilter\Input\ReleaseDateInput;
307319
use Core\App\InputFilter\AbstractInputFilter;
308320

321+
/**
322+
* @phpstan-type CreateBookDataType array{
323+
* name: non-empty-string,
324+
* author: non-empty-string,
325+
* name: DateTimeImmutable|null,
326+
* }
327+
* @extends AbstractInputFilter<CreateBookDataType>
328+
*/
309329
class CreateBookInputFilter extends AbstractInputFilter
310330
{
311331
public function __construct()

docs/book/v7/tutorials/create-book-module.md

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,25 @@ class Book extends AbstractEntity
151151
return $this;
152152
}
153153

154+
/**
155+
* @return array{
156+
* id: non-empty-string,
157+
* name: non-empty-string,
158+
* author: non-empty-string,
159+
* releaseDate: DateTimeImmutable|null,
160+
* created: DateTimeImmutable|null,
161+
* updated: DateTimeImmutable|null,
162+
* }
163+
*/
154164
public function getArrayCopy(): array
155165
{
156166
return [
157167
'id' => $this->id->toString(),
158168
'name' => $this->getName(),
159169
'author' => $this->getAuthor(),
160170
'releaseDate' => $this->getReleaseDate(),
171+
'created' => $this->created,
172+
'updated' => $this->updated,
161173
];
162174
}
163175
}
@@ -181,6 +193,10 @@ use Dot\DependencyInjection\Attribute\Entity;
181193
#[Entity(name: Book::class)]
182194
class BookRepository extends AbstractRepository
183195
{
196+
/**
197+
* @param array<non-empty-string, mixed> $params
198+
* @param array<non-empty-string, mixed> $filters
199+
*/
184200
public function getBooks(array $params, array $filters = []): QueryBuilder
185201
{
186202
return $this
@@ -214,6 +230,9 @@ interface BookServiceInterface
214230

215231
public function saveBook(array $data): Book;
216232

233+
/**
234+
* @param array<non-empty-string, mixed> $params
235+
*/
217236
public function getBooks(array $params = []): QueryBuilder;
218237
}
219238

@@ -253,6 +272,7 @@ class BookService implements BookServiceInterface
253272

254273
/**
255274
* @throws Exception
275+
* @param array<non-empty-string, mixed> $data
256276
*/
257277
public function saveBook(array $data): Book
258278
{
@@ -267,6 +287,9 @@ class BookService implements BookServiceInterface
267287
return $book;
268288
}
269289

290+
/**
291+
* @param array<non-empty-string, mixed> $params
292+
*/
270293
public function getBooks(array $params = []): QueryBuilder
271294
{
272295
$filters = $params['filters'] ?? [];
@@ -289,7 +312,7 @@ class BookService implements BookServiceInterface
289312

290313
```
291314

292-
When creating or updating a book, we will need some validators, so we will create input filters that will be used to validate the data received in the request
315+
When creating or updating a book, we will need some validators, so we will create input filters that will be used to validate the data received in the request.
293316

294317
* `src/Book/src/InputFilter/Input/AuthorInput.php`
295318

@@ -415,6 +438,14 @@ use Api\Book\InputFilter\Input\NameInput;
415438
use Api\Book\InputFilter\Input\ReleaseDateInput;
416439
use Core\App\InputFilter\AbstractInputFilter;
417440

441+
/**
442+
* @phpstan-type CreateBookDataType array{
443+
* name: non-empty-string,
444+
* author: non-empty-string,
445+
* name: DateTimeImmutable|null,
446+
* }
447+
* @extends AbstractInputFilter<CreateBookDataType>
448+
*/
418449
class CreateBookInputFilter extends AbstractInputFilter
419450
{
420451
public function __construct()
@@ -619,6 +650,14 @@ use Dot\DependencyInjection\Factory\AttributedServiceFactory;
619650
use Mezzio\Application;
620651
use Mezzio\Hal\Metadata\MetadataMap;
621652

653+
/**
654+
* @phpstan-import-type MetadataType from AppConfigProvider
655+
* @phpstan-type DependenciesType array{
656+
* delegators: array<class-string, array<class-string>>,
657+
* factories: array<class-string, class-string>,
658+
* aliases: array<class-string, class-string>,
659+
* }
660+
*/
622661
class ConfigProvider
623662
{
624663
public function __invoke(): array
@@ -650,6 +689,9 @@ class ConfigProvider
650689
];
651690
}
652691

692+
/**
693+
* @return MetadataType[]
694+
*/
653695
private function getHalConfig(): array
654696
{
655697
return [
@@ -718,13 +760,31 @@ use Core\Book\Repository\BookRepository;
718760
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
719761
use Dot\DependencyInjection\Factory\AttributedRepositoryFactory;
720762

763+
/**
764+
* @phpstan-type ConfigType array{
765+
* dependencies: DependenciesType,
766+
* doctrine: DoctrineConfigType,
767+
* resultCacheLifetime: int,
768+
* }
769+
* @phpstan-type DoctrineConfigType array{
770+
* driver: array{
771+
* orm_default: array{
772+
* class: class-string<MappingDriver>,
773+
* },
774+
* },
775+
* }
776+
* @phpstan-type DependenciesType array{
777+
* factories: array<class-string|non-empty-string, class-string|non-empty-string>,
778+
* }
779+
*/
721780
class ConfigProvider
722781
{
723782
public function __invoke(): array
724783
{
725784
return [
726-
'dependencies' => $this->getDependencies(),
727-
'doctrine' => $this->getDoctrineConfig(),
785+
'dependencies' => $this->getDependencies(),
786+
'doctrine' => $this->getDoctrineConfig(),
787+
'resultCacheLifetime' => 600,
728788
];
729789
}
730790

@@ -777,7 +837,7 @@ Open `config/autoload/authorization.global.php` and append the below route names
777837
* `book::view-book`
778838
* `book::create-book`
779839

780-
> Make sure you read and understand the rbac [documentation](https://docs.dotkernel.org/dot-rbac-guard/v4/configuration/).
840+
> Make sure you read and understand the [rbac documentation](https://docs.dotkernel.org/dot-rbac-guard/v4/configuration/).
781841
782842
## Migrations
783843

0 commit comments

Comments
 (0)