Skip to content

Commit 8075253

Browse files
committed
Pass around new Context object; update docs; implement once()
1 parent fbecdd9 commit 8075253

32 files changed

+294
-952
lines changed

README.md

Lines changed: 5 additions & 671 deletions
Large diffs are not rendered by default.

docs/attributes.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@ $type->attribute('firstName')
1818
Use the `get` method to define custom retrieval logic for your attribute, instead of just reading the value straight from the model property.
1919

2020
```php
21-
use Psr\Http\Message\ServerRequestInterface as Request;
22-
use Tobyz\JsonApiServer\Schema\Attribute;
21+
use Tobyz\JsonApiServer\Context;
2322

2423
$type->attribute('firstName')
25-
->get(function ($model, Request $request, Attribute $attribute) {
24+
->get(function ($model, Context $context) {
2625
return ucfirst($model->first_name);
2726
});
2827
```

docs/create.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Optionally pass a closure that returns a boolean value.
77
```php
88
$type->creatable();
99

10-
$type->creatable(function (Request $request) {
10+
$type->creatable(function (Context $context) {
1111
return $request->getAttribute('user')->isAdmin();
1212
});
1313
```
@@ -17,7 +17,7 @@ $type->creatable(function (Request $request) {
1717
When creating a resource, an empty model is supplied by the adapter. You may wish to override this and provide a custom model in special circumstances. You can do so using the `newModel` method:
1818

1919
```php
20-
$type->newModel(function (Request $request) {
20+
$type->newModel(function (Context $context) {
2121
return new CustomModel;
2222
});
2323
```
@@ -29,7 +29,7 @@ $type->newModel(function (Request $request) {
2929
Run before the model is saved.
3030

3131
```php
32-
$type->onCreating(function ($model, Request $request) {
32+
$type->onCreating(function ($model, Context $context) {
3333
// do something
3434
});
3535
```
@@ -39,7 +39,7 @@ $type->onCreating(function ($model, Request $request) {
3939
Run after the model is saved.
4040

4141
```php
42-
$type->onCreated(function ($model, Request $request) {
43-
// do something
42+
$type->onCreated(function ($model, Context $context) {
43+
$context->meta('foo', 'bar');
4444
});
4545
```

docs/delete.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Optionally pass a closure that returns a boolean value.
77
```php
88
$type->deletable();
99

10-
$type->deletable(function (Request $request) {
10+
$type->deletable(function (Context $context) {
1111
return $request->getAttribute('user')->isAdmin();
1212
});
1313
```
@@ -19,7 +19,7 @@ $type->deletable(function (Request $request) {
1919
Run before the model is deleted.
2020

2121
```php
22-
$type->onDeleting(function ($model, Request $request) {
22+
$type->onDeleting(function ($model, Context $context) {
2323
// do something
2424
});
2525
```
@@ -29,7 +29,7 @@ $type->onDeleting(function ($model, Request $request) {
2929
Run after the model is deleted.
3030

3131
```php
32-
$type->onDeleted(function ($model, Request $request) {
32+
$type->onDeleted(function ($model, Context $context) {
3333
// do something
3434
});
3535
```

docs/filtering.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ GET /users?filter[postCount]=5..15
2424
To define filters with custom logic, or ones that do not correspond to an attribute, use the `filter` method:
2525

2626
```php
27-
$type->filter('minPosts', function ($query, $value, Request $request) {
27+
$type->filter('minPosts', function ($query, $value, Context $context) {
2828
$query->where('postCount', '>=', $value);
2929
});
3030
```

docs/index.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Introduction
22

3-
**json-api-server** is an automated [JSON:API](http://jsonapi.org) server implementation in PHP.
3+
json-api-server is a comprehensive [JSON:API](http://jsonapi.org) server implementation in PHP.
44

5-
It allows you to define your API's schema, and then use an [Adapter](adapters.md) to connect it to your application's models and database layer, without having to worry about any of the server boilerplate, routing, query parameters, or JSON:API document formatting.
5+
It allows you to define your API's schema, and then use an [adapter](adapters.md) to connect it to your application's models and database layer, without having to worry about any of the server boilerplate, routing, query parameters, or JSON:API document formatting.
66

77
Based on your schema definition, the package will serve a **complete JSON:API that conforms to the [spec](https://jsonapi.org/format/)**, including support for:
88

@@ -19,7 +19,7 @@ The schema definition is extremely powerful and lets you easily apply [permissio
1919

2020
### Example
2121

22-
The following example uses Eloquent models in a Laravel application. However, json-api-server can be used with any framework that can deal in PSR-7 Requests and Responses. Custom [Adapters](adapters.md) can be used to support other ORMs and data persistence layers.
22+
The following example uses Eloquent models in a Laravel application. However, json-api-server can be used with any framework that can deal in PSR-7 Requests and Responses. Custom [adapters](adapters.md) can be used to support other ORMs and data persistence layers.
2323

2424
```php
2525
use App\Models\{Article, Comment, User};

docs/list.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ If you want to restrict the ability to list a resource type, use the `listable`
77
```php
88
$type->notListable();
99

10-
$type->listable(function (Request $request) {
10+
$type->listable(function (Context $context) {
1111
return $request->getAttribute('user')->isAdmin();
1212
});
1313
```
@@ -19,7 +19,7 @@ $type->listable(function (Request $request) {
1919
Run before [scopes](scopes.md) are applied to the `$query` and results are retrieved.
2020

2121
```php
22-
$type->onListing(function ($query, Request $request) {
22+
$type->onListing(function ($query, Context $context) {
2323
// do something
2424
});
2525
```
@@ -29,7 +29,7 @@ $type->onListing(function ($query, Request $request) {
2929
Run after models and relationships have been retrieved, but before they are serialized into a JSON:API document.
3030

3131
```php
32-
$type->onListed(function ($models, Request $request) {
32+
$type->onListed(function ($models, Context $context) {
3333
// do something
3434
});
3535
```

docs/meta.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ You can add meta information at various levels of the document using the `meta`
44

55
## Document Meta
66

7-
To add meta information at the top-level, call `meta` on the `JsonApi` instance:
7+
To add meta information at the top-level of a document, you can call the `meta` method on the `Context` instance which is available inside any of your schema's callbacks.
8+
9+
For example, to add meta information to a resource listing, you might call this inside of an `onListed` listener:
810

911
```php
10-
$api->meta('requestTime', function (Request $request) {
11-
return new DateTime;
12+
$type->onListed(function ($models, Context $context) {
13+
$context->meta('foo', 'bar');
1214
});
1315
```
1416

@@ -17,7 +19,7 @@ $api->meta('requestTime', function (Request $request) {
1719
To add meta information at the resource-level, call `meta` on the schema builder.
1820

1921
```php
20-
$type->meta('updatedAt', function ($model, Request $request) {
22+
$type->meta('updatedAt', function ($model, Context $context) {
2123
return $model->updated_at;
2224
});
2325
```

docs/relationships.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,11 @@ Be careful when making to-many relationships includable as pagination is not sup
5050
Relationships included via the `include` query parameter are automatically [eager-loaded](https://laravel.com/docs/8.x/eloquent-relationships#eager-loading) by the adapter, and any type [scopes](scopes) are applied automatically. You can also apply additional scopes at the relationship level using the `scope` method:
5151

5252
```php
53-
use Psr\Http\Message\ServerRequestInterface as Request;
54-
use Tobyz\JsonApiServer\Schema\HasOne;
53+
use Tobyz\JsonApiServer\Context;
5554

5655
$type->hasOne('users')
5756
->includable()
58-
->scope(function ($query, Request $request, HasOne $field) {
57+
->scope(function ($query, Context $context) {
5958
$query->where('is_listed', true);
6059
});
6160
```
@@ -84,7 +83,7 @@ $api->resource('categories', new EloquentAdapter(Models\Category::class), functi
8483
$api->resource('posts', new EloquentAdapter(Models\Post::class), function (Type $type) {
8584
$type->hasOne('user') // 2
8685
->includable()
87-
->load(function (array $models, array $relationships, Request $request, HasOne $field) {
86+
->load(function (array $models, array $relationships, Context $context) {
8887
// Since this request is to the `GET /categories` endpoint, $models
8988
// will be an array of Category models, and $relationships will be
9089
// an array containing the objects [1, 2] above.
@@ -114,7 +113,7 @@ You can add meta information to a relationship using the `meta` method:
114113

115114
```php
116115
$type->hasOne('user')
117-
->meta('updatedAt', function ($model, $user, Request $request) {
116+
->meta('updatedAt', function ($model, $user, Context $context) {
118117
return $user->updated_at;
119118
});
120119
```

docs/scopes.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ This `scope` method allows you to modify the query builder object provided by th
77
For example, to make it so the authenticated user can only see their own posts:
88

99
```php
10-
$type->scope(function ($query, ServerRequestInterface $request) {
11-
$query->where('user_id', $request->getAttribute('userId'));
10+
$type->scope(function ($query, Context $context) {
11+
$query->where('user_id', $context->getRequest()->getAttribute('userId'));
1212
});
1313
```
1414

0 commit comments

Comments
 (0)