From dc71b82e3e1f5dbd9a4d770f5b7af4311422def2 Mon Sep 17 00:00:00 2001 From: Sami Mazouz Date: Sat, 17 Feb 2024 11:31:26 +0100 Subject: [PATCH] chore: move additions/changes to package --- .../src/Api/Endpoint/Concerns/HasHooks.php | 42 ---------------- .../src/Api/Endpoint/Concerns/SavesData.php | 17 ------- framework/core/src/Api/Endpoint/Create.php | 20 -------- framework/core/src/Api/Endpoint/Delete.php | 2 - framework/core/src/Api/Endpoint/Index.php | 50 ------------------- framework/core/src/Api/Endpoint/Show.php | 2 - framework/core/src/Api/Endpoint/Update.php | 9 ---- .../Api/Resource/AbstractDatabaseResource.php | 2 +- .../src/Api/Resource/Contracts/Resource.php | 2 +- 9 files changed, 2 insertions(+), 144 deletions(-) delete mode 100644 framework/core/src/Api/Endpoint/Concerns/HasHooks.php delete mode 100644 framework/core/src/Api/Endpoint/Concerns/SavesData.php diff --git a/framework/core/src/Api/Endpoint/Concerns/HasHooks.php b/framework/core/src/Api/Endpoint/Concerns/HasHooks.php deleted file mode 100644 index 472fdbe40..000000000 --- a/framework/core/src/Api/Endpoint/Concerns/HasHooks.php +++ /dev/null @@ -1,42 +0,0 @@ -before = $callback; - - return $this; - } - - public function after(Closure $callback): static - { - $this->after = $callback; - - return $this; - } - - protected function callBeforeHook(Context $context): void - { - if ($this->before) { - ($this->before)($context); - } - } - - protected function callAfterHook(Context $context, mixed $data): mixed - { - if ($this->after) { - return ($this->after)($context, $data); - } - - return $data; - } -} diff --git a/framework/core/src/Api/Endpoint/Concerns/SavesData.php b/framework/core/src/Api/Endpoint/Concerns/SavesData.php deleted file mode 100644 index a1dcb5a76..000000000 --- a/framework/core/src/Api/Endpoint/Concerns/SavesData.php +++ /dev/null @@ -1,17 +0,0 @@ -resource, 'mutateDataBeforeValidation')) { - return $context->resource->mutateDataBeforeValidation($context, $data, $validateAll); - } - - return $data; - } -} diff --git a/framework/core/src/Api/Endpoint/Create.php b/framework/core/src/Api/Endpoint/Create.php index 0f9654371..9d3c60470 100644 --- a/framework/core/src/Api/Endpoint/Create.php +++ b/framework/core/src/Api/Endpoint/Create.php @@ -5,30 +5,20 @@ namespace Flarum\Api\Endpoint; use Flarum\Api\Endpoint\Concerns\HasAuthorization; use Flarum\Api\Endpoint\Concerns\HasCustomRoute; use Flarum\Api\Endpoint\Concerns\HasEagerLoading; -use Flarum\Api\Endpoint\Concerns\HasHooks; -use Flarum\Api\Endpoint\Concerns\SavesData; use Illuminate\Database\Eloquent\Collection; use Psr\Http\Message\ResponseInterface; use RuntimeException; use Tobyz\JsonApiServer\Context; -use Tobyz\JsonApiServer\Endpoint\Concerns\SavesData as BaseSavesData; -use Tobyz\JsonApiServer\Endpoint\Concerns\ShowsResources; use Tobyz\JsonApiServer\Endpoint\Create as BaseCreate; use Tobyz\JsonApiServer\Exception\ForbiddenException; use Tobyz\JsonApiServer\Resource\Creatable; -use function Tobyz\JsonApiServer\has_value; use function Tobyz\JsonApiServer\json_api_response; -use function Tobyz\JsonApiServer\set_value; class Create extends BaseCreate implements Endpoint { - use BaseSavesData; - use ShowsResources; - use SavesData; use HasAuthorization; use HasEagerLoading; use HasCustomRoute; - use HasHooks; public function handle(Context $context): ?ResponseInterface { @@ -64,7 +54,6 @@ class Create extends BaseCreate implements Endpoint $this->assertFieldsValid($context, $data); $this->fillDefaultValues($context, $data); $this->deserializeValues($context, $data); - $this->mutateDataBeforeValidation($context, $data, true); $this->assertDataValid($context, $data); $this->setValues($context, $data); @@ -80,15 +69,6 @@ class Create extends BaseCreate implements Endpoint return $model; } - private function fillDefaultValues(Context $context, array &$data): void - { - foreach ($context->fields($context->resource) as $field) { - if (!has_value($data, $field) && ($default = $field->default)) { - set_value($data, $field, $default($context->withField($field))); - } - } - } - public function route(): EndpointRoute { return new EndpointRoute( diff --git a/framework/core/src/Api/Endpoint/Delete.php b/framework/core/src/Api/Endpoint/Delete.php index 615661f52..075d5e010 100644 --- a/framework/core/src/Api/Endpoint/Delete.php +++ b/framework/core/src/Api/Endpoint/Delete.php @@ -8,7 +8,6 @@ use Nyholm\Psr7\Response; use Psr\Http\Message\ResponseInterface; use RuntimeException; use Tobyz\JsonApiServer\Context; -use Tobyz\JsonApiServer\Endpoint\Concerns\FindsResources; use Tobyz\JsonApiServer\Endpoint\Delete as BaseDelete; use Tobyz\JsonApiServer\Exception\ForbiddenException; use Tobyz\JsonApiServer\Resource\Deletable; @@ -17,7 +16,6 @@ use function Tobyz\JsonApiServer\json_api_response; class Delete extends BaseDelete implements Endpoint { use HasAuthorization; - use FindsResources; use HasCustomRoute; /** {@inheritdoc} */ diff --git a/framework/core/src/Api/Endpoint/Index.php b/framework/core/src/Api/Endpoint/Index.php index 8b612b2ef..96a91309b 100644 --- a/framework/core/src/Api/Endpoint/Index.php +++ b/framework/core/src/Api/Endpoint/Index.php @@ -2,12 +2,10 @@ namespace Flarum\Api\Endpoint; -use Closure; use Flarum\Api\Endpoint\Concerns\ExtractsListingParams; use Flarum\Api\Endpoint\Concerns\HasAuthorization; use Flarum\Api\Endpoint\Concerns\HasCustomRoute; use Flarum\Api\Endpoint\Concerns\HasEagerLoading; -use Flarum\Api\Endpoint\Concerns\HasHooks; use Flarum\Http\RequestUtil; use Flarum\Search\SearchCriteria; use Flarum\Search\SearchManager; @@ -16,27 +14,20 @@ use Illuminate\Database\Eloquent\Collection; use Psr\Http\Message\ResponseInterface as Response; use RuntimeException; use Tobyz\JsonApiServer\Context; -use Tobyz\JsonApiServer\Endpoint\Concerns\IncludesData; use Tobyz\JsonApiServer\Endpoint\Index as BaseIndex; -use Tobyz\JsonApiServer\Exception\BadRequestException; use Tobyz\JsonApiServer\Exception\ForbiddenException; -use Tobyz\JsonApiServer\Exception\Sourceable; use Tobyz\JsonApiServer\Pagination\OffsetPagination; use Tobyz\JsonApiServer\Resource\Countable; use Tobyz\JsonApiServer\Resource\Listable; use Tobyz\JsonApiServer\Serializer; -use function Tobyz\JsonApiServer\apply_filters; use function Tobyz\JsonApiServer\json_api_response; -use function Tobyz\JsonApiServer\parse_sort_string; class Index extends BaseIndex implements Endpoint { use HasAuthorization; - use IncludesData; use HasEagerLoading; use HasCustomRoute; use ExtractsListingParams; - use HasHooks; public function paginate(int $defaultLimit = 20, int $maxLimit = 50): static { @@ -151,47 +142,6 @@ class Index extends BaseIndex implements Endpoint return json_api_response(compact('data', 'included', 'meta', 'links')); } - private function applySorts($query, Context $context): void - { - if (!($sortString = $context->queryParam('sort', $this->defaultSort))) { - return; - } - - $sorts = $context->collection->sorts(); - - foreach (parse_sort_string($sortString) as [$name, $direction]) { - foreach ($sorts as $field) { - if ($field->name === $name && $field->isVisible($context)) { - $field->apply($query, $direction, $context); - continue 2; - } - } - - throw (new BadRequestException("Invalid sort: $name"))->setSource([ - 'parameter' => 'sort', - ]); - } - } - - private function applyFilters($query, Context $context): void - { - if (!($filters = $context->queryParam('filter'))) { - return; - } - - if (!is_array($filters)) { - throw (new BadRequestException('filter must be an array'))->setSource([ - 'parameter' => 'filter', - ]); - } - - try { - apply_filters($query, $filters, $context->collection, $context); - } catch (Sourceable $e) { - throw $e->prependSource(['parameter' => 'filter']); - } - } - public function route(): EndpointRoute { return new EndpointRoute( diff --git a/framework/core/src/Api/Endpoint/Show.php b/framework/core/src/Api/Endpoint/Show.php index 6cdb372c0..896d90dca 100644 --- a/framework/core/src/Api/Endpoint/Show.php +++ b/framework/core/src/Api/Endpoint/Show.php @@ -6,7 +6,6 @@ use Flarum\Api\Endpoint\Concerns\ExtractsListingParams; use Flarum\Api\Endpoint\Concerns\HasAuthorization; use Flarum\Api\Endpoint\Concerns\HasCustomRoute; use Flarum\Api\Endpoint\Concerns\HasEagerLoading; -use Flarum\Api\Endpoint\Concerns\HasHooks; use Illuminate\Database\Eloquent\Collection; use Psr\Http\Message\ResponseInterface; use Tobyz\JsonApiServer\Context; @@ -24,7 +23,6 @@ class Show extends BaseShow implements Endpoint use HasEagerLoading; use HasCustomRoute; use ExtractsListingParams; - use HasHooks; public function handle(Context $context): ?ResponseInterface { diff --git a/framework/core/src/Api/Endpoint/Update.php b/framework/core/src/Api/Endpoint/Update.php index ea5a73de2..93b976c94 100644 --- a/framework/core/src/Api/Endpoint/Update.php +++ b/framework/core/src/Api/Endpoint/Update.php @@ -5,14 +5,10 @@ namespace Flarum\Api\Endpoint; use Flarum\Api\Endpoint\Concerns\HasAuthorization; use Flarum\Api\Endpoint\Concerns\HasCustomRoute; use Flarum\Api\Endpoint\Concerns\HasEagerLoading; -use Flarum\Api\Endpoint\Concerns\HasHooks; -use Flarum\Api\Endpoint\Concerns\SavesData; use Illuminate\Database\Eloquent\Collection; use Psr\Http\Message\ResponseInterface; use RuntimeException; use Tobyz\JsonApiServer\Context; -use Tobyz\JsonApiServer\Endpoint\Concerns\SavesData as BaseSavesData; -use Tobyz\JsonApiServer\Endpoint\Concerns\ShowsResources; use Tobyz\JsonApiServer\Endpoint\Update as BaseUpdate; use Tobyz\JsonApiServer\Exception\ForbiddenException; use Tobyz\JsonApiServer\Resource\Updatable; @@ -20,13 +16,9 @@ use function Tobyz\JsonApiServer\json_api_response; class Update extends BaseUpdate implements Endpoint { - use BaseSavesData; - use ShowsResources; - use SavesData; use HasAuthorization; use HasEagerLoading; use HasCustomRoute; - use HasHooks; public function handle(Context $context): ?ResponseInterface { @@ -67,7 +59,6 @@ class Update extends BaseUpdate implements Endpoint $this->assertFieldsValid($context, $data); $this->deserializeValues($context, $data); - $this->mutateDataBeforeValidation($context, $data, false); $this->assertDataValid($context, $data); $this->setValues($context, $data); diff --git a/framework/core/src/Api/Resource/AbstractDatabaseResource.php b/framework/core/src/Api/Resource/AbstractDatabaseResource.php index f0cb266ca..6b707e6d7 100644 --- a/framework/core/src/Api/Resource/AbstractDatabaseResource.php +++ b/framework/core/src/Api/Resource/AbstractDatabaseResource.php @@ -130,7 +130,7 @@ abstract class AbstractDatabaseResource extends BaseResource implements return null; } - public function mutateDataBeforeValidation(Context $context, array $data, bool $validateAll): array + public function mutateDataBeforeValidation(Context $context, array $data): array { $dirty = $context->model->getDirty(); diff --git a/framework/core/src/Api/Resource/Contracts/Resource.php b/framework/core/src/Api/Resource/Contracts/Resource.php index c33c9ab90..c2c4e4495 100644 --- a/framework/core/src/Api/Resource/Contracts/Resource.php +++ b/framework/core/src/Api/Resource/Contracts/Resource.php @@ -6,5 +6,5 @@ use Tobyz\JsonApiServer\Resource\Resource as BaseResource; interface Resource extends BaseResource { - + // }