Skip to content

Commit 46c42bc

Browse files
Roman3349f3l1x
authored andcommitted
docs: update documentation to use PHP attributes
Signed-off-by: Roman Ondráček <mail@romanondracek.cz>
1 parent 646a84a commit 46c42bc

File tree

7 files changed

+115
-166
lines changed

7 files changed

+115
-166
lines changed

.docs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Apitte
22

3-
An opinionated and enjoyable API framework based on Nette Framework. Supporting content negotiation, debugging, middlewares, attributes, annotations and loving openapi/swagger.
3+
An opinionated and enjoyable API framework based on Nette Framework. Supporting content negotiation, debugging, middlewares, attributes and loving openapi/swagger.
44

55
Need to start with Apitte
66
- [Setup](index.md)

.docs/decorators.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ class RequestAuthenticationDecorator implements IRequestDecorator
125125

126126
**Tip**
127127

128-
You could also authenticate only some endpoints thanks to [tags](endpoints.md#list-of-annotations) and [metadata](router.md#request-attributes) from `SimpleRouter`.
128+
You could also authenticate only some endpoints thanks to [tags](endpoints.md#list-of-attributes) and [metadata](router.md#request-attributes) from `SimpleRouter`.
129129

130130
```php
131131
use Apitte\Core\Http\RequestAttributes;

.docs/endpoints.md

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,16 @@ namespace App\Api\V1\Controllers;
1616
use Apitte\Core\Annotation\Controller\Path;
1717
use Apitte\Core\UI\Controller\IController;
1818

19-
/**
20-
* @Path("/api/v1")
21-
*/
19+
#[Path("/api/v1")]
2220
abstract class BaseV1Controller implements IController
2321
{
2422
}
2523
```
2624

2725
Create an endpoint
2826

29-
- Controller must have annotation `@Path()` and be registered as service
30-
- Method must have annotations `@Path()` and `@Method()`
27+
- Controller must have attribute `#[Path()]` and be registered as service
28+
- Method must have attributess `#[Path()]` and `#[Method()]`
3129

3230
```yaml
3331
services:
@@ -43,16 +41,12 @@ use Apitte\Core\Http\ApiRequest;
4341
use Apitte\Core\Http\ApiResponse;
4442
use Nette\Utils\Json;
4543

46-
/**
47-
* @Path("/users")
48-
*/
44+
#[Path("/users")]
4945
class UsersController extends BaseV1Controller
5046
{
5147

52-
/**
53-
* @Path("/")
54-
* @Method("GET")
55-
*/
48+
#[Path("/")]
49+
#[Method("GET")]
5650
public function index(ApiRequest $request, ApiResponse $response): ApiResponse
5751
{
5852
// This is an endpoint
@@ -80,48 +74,46 @@ class UsersController extends BaseV1Controller
8074
}
8175
```
8276

83-
**Tip** Use the `@Path("/")` annotation on a Controller and its method to target the homepage, e.q. `example.com/`.
77+
**Tip** Use the `#[Path("/")]` attribute on a Controller and its method to target the homepage, e.q. `example.com/`.
8478

85-
### List of annotations / attributes
79+
### List of attributes
8680

87-
> You can use seamless PHP 8 attributes.
88-
89-
`@Id`
81+
`#[Id]`
9082
- Must consist only of following characters: `a-z`, `A-Z`, `0-9`, `_`
9183
- Not used by Apitte for anything, it may just help you identify, group, etc. your endpoints
9284

93-
`@Path`
85+
`#[Path]`
9486
- See example controllers above
9587
- Must consist only of following characters: `a-z`, `A-Z`, `0-9`, `-_/`
96-
- The `@Path` annotation can be used on:
88+
- The `#[Path]` attribute can be used on:
9789
- abstract controller to define a group path for multiple controllers (e.g. `example.com/v1/...`)
9890
- final controller to define a path for that particular controller (e.g. `example.com/v1/users`)
9991
- method to define a path for a specific endpoint
10092
- This hierarchy is then used to build the schema and make routing possible.
10193

102-
`@Method`
94+
`#[Method]`
10395
- Allowed HTTP method for endpoint
10496
- GET, POST, PUT, OPTION, DELETE, HEAD
105-
- `@Method("GET")`
106-
- `@Method({"POST", "PUT"})`
97+
- `#[Method("GET")]`
98+
- `#[Method({"POST", "PUT"})]`
10799
- Defined on method
108100

109-
`@Tag`
101+
`#[Tag]`
110102
- Used by [OpenApi](schema.md#openapi)
111103
- Could by also used by your custom logic
112-
- `@Tag(name="name")`
113-
- `@Tag(name="string", value="string|null")`
104+
- `#[Tag(name="name")]`
105+
- `#[Tag(name="string", value="string|null")]`
114106
- Defined on class and method
115107

116108
Mapping
117109
- Validate and map data from request and map data to response
118-
- `@RequestParameter`, `@RequestParameters`
119-
- `@RequestBody`
110+
- `#[RequestParameter]`
111+
- `#[RequestBody]`
120112
- See [mapping](mapping.md) chapter for more info.
121113

122114
Negotiations
123115
- Response transformations
124-
- `@Negotiation`, `@Negotiations`
116+
- `#[Negotiation]`
125117
- See [negotiation](negotiation.md) chapter for details.
126118

127119
### Automatic controllers registration

.docs/mapping.md

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,17 @@ namespace App\Api\V1\Controllers;
2626

2727
use Apitte\Core\Annotation\Controller\Method;
2828
use Apitte\Core\Annotation\Controller\Path;
29-
use Apitte\Core\Annotation\Controller\RequestParameters;
3029
use Apitte\Core\Annotation\Controller\RequestParameter;
3130
use Apitte\Core\Http\ApiRequest;
3231
use Apitte\Core\Http\ApiResponse;
3332

34-
/**
35-
* @Path("/users")
36-
*/
33+
#[Path("/users")]
3734
class UsersController extends BaseV1Controller
3835
{
3936

40-
/**
41-
* @Path("/{id}")
42-
* @Method("GET")
43-
* @RequestParameters({
44-
* @RequestParameter(name="id", type="int", description="My favourite user ID")
45-
* })
46-
*/
37+
#[Path("/{id}")]
38+
#[Method("GET")]
39+
#[RequestParameter(name: "id", type: "int", description: "My favourite user ID")]
4740
public function detail(ApiRequest $request): ApiResponse
4841
{
4942
/** @var int $id Perfectly valid integer */
@@ -57,7 +50,7 @@ class UsersController extends BaseV1Controller
5750

5851
### Options
5952

60-
`@RequestParameter()` have few available options.
53+
`#[RequestParameter()]` have few available options.
6154

6255
- `name="nameOfParameter"` - same as name of parameter in path, query...
6356
- `type="string|int|float|bool|datetime"` - data type, see [data types](#data-types)
@@ -144,7 +137,7 @@ class MyEmailTypeMapper implements ITypeMapper
144137
## RequestBody
145138

146139
Imagine you have a data grid with many filter options. You can describe all options manually or
147-
use value object, entity, for it. And it leads us to `@RequestBody`.
140+
use value object, entity, for it. And it leads us to `#[RequestBody]`.
148141

149142
We have an entity with described fields.
150143

@@ -165,15 +158,17 @@ final class UserFilter extends BasicEntity
165158
}
166159
```
167160

168-
And some endpoint with `@RequestBody` annotation. There's a method `ApiRequest::getEntity()`, it gets
161+
And some endpoint with `#[RequestBody]` attribute. There's a method `ApiRequest::getEntity()`, it gets
169162
the entity from request attributes. So simple, right?
170163

171164
```php
172-
/**
173-
* @Path("/filter")
174-
* @Method("GET")
175-
* @RequestBody(entity="App\Api\Entity\Request\UserFilter")
176-
*/
165+
use Apitte\Core\Annotation\Controller\Method;
166+
use Apitte\Core\Annotation\Controller\Path;
167+
use Apitte\Core\Annotation\Controller\RequestParameter;
168+
169+
#[Path("/filter")]
170+
#[Method("GET")]
171+
#[RequestBody(entity: App\Api\Entity\Request\UserFilter::class)]
177172
public function filter(ApiRequest $request)
178173
{
179174
/** @var UserFilter $entity */

.docs/negotiation.md

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,18 @@ Instead of writing data into response body use `$response->withEntity($entity)`
2121
```php
2222
namespace App\Api\V1\Controllers;
2323

24-
use Apitte\Core\Annotation\Controller\ControllerPath;
2524
use Apitte\Core\Annotation\Controller\Method;
2625
use Apitte\Core\Annotation\Controller\Path;
2726
use Apitte\Core\Http\ApiRequest;
2827
use Apitte\Core\Http\ApiResponse;
2928
use Apitte\Negotiation\Http\ArrayEntity;
3029

31-
/**
32-
* @ControllerPath("/users")
33-
*/
30+
#[Path("/users")]
3431
class UsersController extends BaseV1Controller
3532
{
3633

37-
/**
38-
* @Path("/")
39-
* @Method("GET")
40-
*/
34+
#[Path("/")]
35+
#[Method("GET")]
4136
public function index(ApiRequest $request, ApiResponse $response): ApiResponse
4237
{
4338
$entity = ArrayEntity::from([
@@ -87,7 +82,7 @@ Handle request and based on path suffix or request headers call appropriate tran
8782
`DefaultNegotiator`
8883

8984
- called when none other transform
90-
- require annotation `@Negotiation(default = true, suffix = "json")` defined on endpoint - transformer for given suffix is looked for
85+
- require attribute `#[Negotiation(default = true, suffix = "json")]` defined on endpoint - transformer for given suffix is looked for
9186

9287
`FallbackNegotiator`
9388

0 commit comments

Comments
 (0)