Pass models to events by reference and document Show endpoint

This commit is contained in:
Toby Zerner 2021-01-15 08:35:59 +10:30
parent 9846de5267
commit 497cd364a6
9 changed files with 29 additions and 13 deletions

View File

@ -39,6 +39,7 @@ module.exports = {
collapsable: false,
children: [
'list',
'show',
'create',
'update',
'delete',

View File

@ -29,7 +29,7 @@ $type->newModel(function (Context $context) {
Run before the model is saved.
```php
$type->onCreating(function ($model, Context $context) {
$type->onCreating(function (&$model, Context $context) {
// do something
});
```
@ -39,7 +39,7 @@ $type->onCreating(function ($model, Context $context) {
Run after the model is saved.
```php
$type->onCreated(function ($model, Context $context) {
$type->onCreated(function (&$model, Context $context) {
$context->meta('foo', 'bar');
});
```

View File

@ -19,7 +19,7 @@ $type->deletable(function (Context $context) {
Run before the model is deleted.
```php
$type->onDeleting(function ($model, Context $context) {
$type->onDeleting(function (&$model, Context $context) {
// do something
});
```
@ -29,7 +29,7 @@ $type->onDeleting(function ($model, Context $context) {
Run after the model is deleted.
```php
$type->onDeleted(function ($model, Context $context) {
$type->onDeleted(function (&$model, Context $context) {
// do something
});
```

15
docs/show.md Normal file
View File

@ -0,0 +1,15 @@
# Showing Resources
For each resource type, a `GET /{type}/{id}` endpoint is exposed to show an individual resource.
## Events
### `onShow`
Run after models and relationships have been retrieved, but before they are serialized into a JSON:API document.
```php
$type->onShow(function (&$model, Context $context) {
// do something
});
```

View File

@ -19,7 +19,7 @@ $type->updatable(function (Context $context) {
Run before the model is saved.
```php
$type->onUpdating(function ($model, Context $context) {
$type->onUpdating(function (&$model, Context $context) {
// do something
});
```
@ -29,7 +29,7 @@ $type->onUpdating(function ($model, Context $context) {
Run after the model is saved.
```php
$type->onUpdated(function ($model, Context $context) {
$type->onUpdated(function (&$model, Context $context) {
// do something
});
```

View File

@ -54,11 +54,11 @@ class Create
$this->assertDataValid($data, $model, $context, true);
$this->setValues($data, $model, $context);
run_callbacks($schema->getListeners('creating'), [$model, $context]);
run_callbacks($schema->getListeners('creating'), [&$model, $context]);
$this->save($data, $model, $context);
run_callbacks($schema->getListeners('created'), [$model, $context]);
run_callbacks($schema->getListeners('created'), [&$model, $context]);
return (new Show($this->api, $this->resource, $model))
->handle($context)

View File

@ -44,7 +44,7 @@ class Delete
throw new ForbiddenException;
}
run_callbacks($schema->getListeners('deleting'), [$this->model, $context]);
run_callbacks($schema->getListeners('deleting'), [&$this->model, $context]);
if ($deleteCallback = $schema->getDeleteCallback()) {
$deleteCallback($this->model, $context);
@ -52,7 +52,7 @@ class Delete
$this->resource->getAdapter()->delete($this->model);
}
run_callbacks($schema->getListeners('deleted'), [$this->model, $context]);
run_callbacks($schema->getListeners('deleted'), [&$this->model, $context]);
return new Response(204);
}

View File

@ -42,7 +42,7 @@ class Show
$this->loadRelationships([$this->model], $include, $context);
run_callbacks($this->resource->getSchema()->getListeners('show'), [$this->model, $context]);
run_callbacks($this->resource->getSchema()->getListeners('show'), [&$this->model, $context]);
$serializer = new Serializer($this->api, $context);
$serializer->add($this->resource, $this->model, $include);

View File

@ -52,11 +52,11 @@ class Update
$this->assertDataValid($data, $this->model, $context, false);
$this->setValues($data, $this->model, $context);
run_callbacks($schema->getListeners('updating'), [$this->model, $context]);
run_callbacks($schema->getListeners('updating'), [&$this->model, $context]);
$this->save($data, $this->model, $context);
run_callbacks($schema->getListeners('updated'), [$this->model, $context]);
run_callbacks($schema->getListeners('updated'), [&$this->model, $context]);
return (new Show($this->api, $this->resource, $this->model))
->handle($context);