fix: drop bc layer

This commit is contained in:
Sami Mazouz 2024-02-26 14:46:06 +01:00
parent f6cd055dbe
commit 8b7f3c3d6d
No known key found for this signature in database
6 changed files with 42 additions and 33 deletions

View File

@ -132,11 +132,24 @@ class TagResource extends AbstractDatabaseResource
];
}
protected function newSavingEvent(Context $context, array $data): ?object
public function creating(object $model, Context $context): ?object
{
return $context->endpoint instanceof Endpoint\Create
? new Creating($context->model, $context->getActor(), $data)
: new Saving($context->model, $context->getActor(), $data);
$this->events->dispatch(
new Creating($model, $context->getActor(), $context->body())
);
return $model;
}
public function saving(object $model, Context $context): ?object
{
if (! $context->endpoint instanceof Endpoint\Create) {
$this->events->dispatch(
new Saving($model, $context->getActor(), $context->body())
);
}
return $model;
}
public function deleting(object $model, Context $context): void

View File

@ -127,11 +127,6 @@ abstract class AbstractDatabaseResource extends BaseResource implements
//
}
protected function newSavingEvent(Context $context, array $data): ?object
{
return null;
}
public function dispatchEventsFor(mixed $entity, User $actor = null): void
{
if (method_exists($entity, 'releaseEvents')) {
@ -141,21 +136,6 @@ abstract class AbstractDatabaseResource extends BaseResource implements
public function mutateDataBeforeValidation(Context $context, array $data): array
{
$dirty = $context->model->getDirty();
$savingEvent = $this->newSavingEvent($context, Arr::get($context->body(), 'data', []));
if ($savingEvent) {
$this->events->dispatch($savingEvent);
$dirtyAfterEvent = $context->model->getDirty();
// Unlike 1.0, the saving events in 2.0 do not allow modifying the model.
if ($dirtyAfterEvent !== $dirty) {
throw new RuntimeException('You should modify the model through the saving event. Please use the resource extenders instead.');
}
}
return $data;
}

View File

@ -338,8 +338,12 @@ class DiscussionResource extends AbstractDatabaseResource
);
}
protected function newSavingEvent(\Tobyz\JsonApiServer\Context $context, array $data): ?object
public function saving(object $model, \Tobyz\JsonApiServer\Context $context): ?object
{
return new Saving($context->model, $context->getActor(), $data);
$this->events->dispatch(
new Saving($model, $context->getActor(), Arr::get($context->body(), 'data', []))
);
return $model;
}
}

View File

@ -8,9 +8,9 @@ use Flarum\Api\Sort\SortColumn;
use Flarum\Group\Event\Deleting;
use Flarum\Group\Event\Saving;
use Flarum\Group\Group;
use Flarum\Http\RequestUtil;
use Flarum\Locale\TranslatorInterface;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Arr;
use Tobyz\JsonApiServer\Context;
class GroupResource extends AbstractDatabaseResource
@ -106,9 +106,13 @@ class GroupResource extends AbstractDatabaseResource
return $name;
}
protected function newSavingEvent(Context $context, array $data): ?object
public function saving(object $model, Context $context): ?object
{
return new Saving($context->model, RequestUtil::getActor($context->request), $data);
$this->events->dispatch(
new Saving($model, $context->getActor(), Arr::get($context->body(), 'data', []))
);
return $model;
}
public function deleting(object $model, Context $context): void

View File

@ -284,8 +284,12 @@ class PostResource extends AbstractDatabaseResource
);
}
protected function newSavingEvent(\Tobyz\JsonApiServer\Context $context, array $data): ?object
public function saving(object $model, \Tobyz\JsonApiServer\Context $context): ?object
{
return new Saving($context->model, $context->getActor(), $data);
$this->events->dispatch(
new Saving($model, $context->getActor(), Arr::get($context->body(), 'data', []))
);
return $model;
}
}

View File

@ -341,9 +341,13 @@ class UserResource extends AbstractDatabaseResource
);
}
protected function newSavingEvent(\Tobyz\JsonApiServer\Context $context, array $data): ?object
public function saving(object $model, \Tobyz\JsonApiServer\Context $context): ?object
{
return new Saving($context->model, $context->getActor(), $data);
$this->events->dispatch(
new Saving($model, $context->getActor(), Arr::get($context->body(), 'data', []))
);
return $model;
}
private function applyToken(User $user, RegistrationToken $token): void