Skip to content

Commit 497cd36

Browse files
committed
Pass models to events by reference and document Show endpoint
1 parent 9846de5 commit 497cd36

File tree

9 files changed

+29
-13
lines changed

9 files changed

+29
-13
lines changed

docs/.vuepress/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ module.exports = {
3939
collapsable: false,
4040
children: [
4141
'list',
42+
'show',
4243
'create',
4344
'update',
4445
'delete',

docs/create.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ $type->newModel(function (Context $context) {
2929
Run before the model is saved.
3030

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

4141
```php
42-
$type->onCreated(function ($model, Context $context) {
42+
$type->onCreated(function (&$model, Context $context) {
4343
$context->meta('foo', 'bar');
4444
});
4545
```

docs/delete.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ $type->deletable(function (Context $context) {
1919
Run before the model is deleted.
2020

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

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

docs/show.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Showing Resources
2+
3+
For each resource type, a `GET /{type}/{id}` endpoint is exposed to show an individual resource.
4+
5+
## Events
6+
7+
### `onShow`
8+
9+
Run after models and relationships have been retrieved, but before they are serialized into a JSON:API document.
10+
11+
```php
12+
$type->onShow(function (&$model, Context $context) {
13+
// do something
14+
});
15+
```

docs/update.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ $type->updatable(function (Context $context) {
1919
Run before the model is saved.
2020

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

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

src/Endpoint/Create.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ public function handle(Context $context): ResponseInterface
5454
$this->assertDataValid($data, $model, $context, true);
5555
$this->setValues($data, $model, $context);
5656

57-
run_callbacks($schema->getListeners('creating'), [$model, $context]);
57+
run_callbacks($schema->getListeners('creating'), [&$model, $context]);
5858

5959
$this->save($data, $model, $context);
6060

61-
run_callbacks($schema->getListeners('created'), [$model, $context]);
61+
run_callbacks($schema->getListeners('created'), [&$model, $context]);
6262

6363
return (new Show($this->api, $this->resource, $model))
6464
->handle($context)

src/Endpoint/Delete.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ public function handle(Context $context): ResponseInterface
4444
throw new ForbiddenException;
4545
}
4646

47-
run_callbacks($schema->getListeners('deleting'), [$this->model, $context]);
47+
run_callbacks($schema->getListeners('deleting'), [&$this->model, $context]);
4848

4949
if ($deleteCallback = $schema->getDeleteCallback()) {
5050
$deleteCallback($this->model, $context);
5151
} else {
5252
$this->resource->getAdapter()->delete($this->model);
5353
}
5454

55-
run_callbacks($schema->getListeners('deleted'), [$this->model, $context]);
55+
run_callbacks($schema->getListeners('deleted'), [&$this->model, $context]);
5656

5757
return new Response(204);
5858
}

src/Endpoint/Show.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function handle(Context $context): ResponseInterface
4242

4343
$this->loadRelationships([$this->model], $include, $context);
4444

45-
run_callbacks($this->resource->getSchema()->getListeners('show'), [$this->model, $context]);
45+
run_callbacks($this->resource->getSchema()->getListeners('show'), [&$this->model, $context]);
4646

4747
$serializer = new Serializer($this->api, $context);
4848
$serializer->add($this->resource, $this->model, $include);

src/Endpoint/Update.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ public function handle(Context $context): ResponseInterface
5252
$this->assertDataValid($data, $this->model, $context, false);
5353
$this->setValues($data, $this->model, $context);
5454

55-
run_callbacks($schema->getListeners('updating'), [$this->model, $context]);
55+
run_callbacks($schema->getListeners('updating'), [&$this->model, $context]);
5656

5757
$this->save($data, $this->model, $context);
5858

59-
run_callbacks($schema->getListeners('updated'), [$this->model, $context]);
59+
run_callbacks($schema->getListeners('updated'), [&$this->model, $context]);
6060

6161
return (new Show($this->api, $this->resource, $this->model))
6262
->handle($context);

0 commit comments

Comments
 (0)