Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: drop the need for a json-api-server fork #3986

Merged
merged 8 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@
"symfony/postmark-mailer": "^6.3",
"symfony/translation": "^6.3",
"symfony/yaml": "^6.3",
"flarum/json-api-server": "^1.0.0",
"flarum/json-api-server": "^0.1.0",
"wikimedia/less.php": "^4.1"
},
"require-dev": {
Expand Down
4 changes: 2 additions & 2 deletions extensions/likes/extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@
->fields(PostResourceFields::class)
->endpoint(
[Endpoint\Index::class, Endpoint\Show::class, Endpoint\Create::class, Endpoint\Update::class],
function (Endpoint\Index|Endpoint\Show|Endpoint\Create|Endpoint\Update $endpoint): Endpoint\EndpointInterface {
function (Endpoint\Index|Endpoint\Show|Endpoint\Create|Endpoint\Update $endpoint): Endpoint\Endpoint {
return $endpoint->addDefaultInclude(['likes']);
}
),

(new Extend\ApiResource(Resource\DiscussionResource::class))
->endpoint(Endpoint\Show::class, function (Endpoint\Show $endpoint): Endpoint\EndpointInterface {
->endpoint(Endpoint\Show::class, function (Endpoint\Show $endpoint): Endpoint\Endpoint {
return $endpoint->addDefaultInclude(['posts.likes']);
}),

Expand Down
2 changes: 1 addition & 1 deletion extensions/likes/src/Api/PostResourceFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function __invoke(): array
Schema\Relationship\ToMany::make('likes')
->type('users')
->includable()
->constrain(function (Builder $query, Context $context) {
->scope(function (Builder $query, Context $context) {
$actor = $context->getActor();
$grammar = $query->getQuery()->getGrammar();

Expand Down
4 changes: 2 additions & 2 deletions extensions/mentions/extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@

(new Extend\ApiResource(Resource\PostResource::class))
->fields(PostResourceFields::class)
->endpoint([Endpoint\Index::class, Endpoint\Show::class], function (Endpoint\Index|Endpoint\Show $endpoint): Endpoint\EndpointInterface {
->endpoint([Endpoint\Index::class, Endpoint\Show::class], function (Endpoint\Index|Endpoint\Show $endpoint): Endpoint\Endpoint {
return $endpoint->addDefaultInclude(['mentionedBy', 'mentionedBy.user', 'mentionedBy.discussion']);
})
->endpoint(Endpoint\Index::class, function (Endpoint\Index $endpoint): Endpoint\Index {
Expand Down Expand Up @@ -137,7 +137,7 @@
}),

(new Extend\ApiResource(Resource\PostResource::class))
->endpoint([Endpoint\Index::class, Endpoint\Show::class], function (Endpoint\Index|Endpoint\Show $endpoint): Endpoint\EndpointInterface {
->endpoint([Endpoint\Index::class, Endpoint\Show::class], function (Endpoint\Index|Endpoint\Show $endpoint): Endpoint\Endpoint {
return $endpoint->eagerLoad(['mentionsTags']);
}),
]),
Expand Down
2 changes: 1 addition & 1 deletion extensions/mentions/src/Api/PostResourceFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __invoke(): array
Schema\Relationship\ToMany::make('mentionedBy')
->type('posts')
->includable()
->constrain(fn (Builder $query) => $query->oldest('id')->limit(static::$maxMentionedBy)),
->scope(fn (Builder $query) => $query->oldest('id')->limit(static::$maxMentionedBy)),
Schema\Relationship\ToMany::make('mentionsPosts')
->type('posts'),
Schema\Relationship\ToMany::make('mentionsUsers')
Expand Down
3 changes: 1 addition & 2 deletions extensions/sticky/src/Api/DiscussionResourceFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
namespace Flarum\Sticky\Api;

use Flarum\Api\Context;
use Flarum\Api\Endpoint\Update;
use Flarum\Api\Schema;
use Flarum\Discussion\Discussion;
use Flarum\Sticky\Event\DiscussionWasStickied;
Expand All @@ -23,7 +22,7 @@ public function __invoke(): array
return [
Schema\Boolean::make('isSticky')
->writable(function (Discussion $discussion, Context $context) {
return $context->endpoint instanceof Update
return $context->updating()
&& $context->getActor()->can('sticky', $discussion);
})
->set(function (Discussion $discussion, bool $isSticky, Context $context) {
Expand Down
2 changes: 1 addition & 1 deletion framework/core/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
"symfony/translation": "^6.3",
"symfony/translation-contracts": "^2.5",
"symfony/yaml": "^6.3",
"flarum/json-api-server": "^1.0.0",
"flarum/json-api-server": "^0.1.0",
"wikimedia/less.php": "^4.1"
},
"require-dev": {
Expand Down
9 changes: 4 additions & 5 deletions framework/core/src/Api/ApiServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Flarum\Api;

use Flarum\Api\Endpoint\EndpointInterface;
use Flarum\Api\Endpoint\Endpoint;
use Flarum\Foundation\AbstractServiceProvider;
use Flarum\Foundation\ErrorHandling\JsonApiFormatter;
use Flarum\Foundation\ErrorHandling\Registry;
Expand All @@ -22,7 +22,6 @@
use Illuminate\Contracts\Container\Container;
use Laminas\Stratigility\MiddlewarePipe;
use ReflectionClass;
use Tobyz\JsonApiServer\Endpoint\Endpoint;

class ApiServiceProvider extends AbstractServiceProvider
{
Expand Down Expand Up @@ -53,7 +52,7 @@ public function register(): void
$api->container($container);

foreach ($resources as $resourceClass) {
/** @var \Flarum\Api\Resource\AbstractResource|\Flarum\Api\Resource\AbstractDatabaseResource $resource */
/** @var \Flarum\Api\Resource\AbstractResource $resource */
$resource = $container->make($resourceClass);
$api->resource($resource->boot($api));
}
Expand Down Expand Up @@ -189,7 +188,7 @@ protected function populateRoutes(RouteCollection $routes, Container $container)
*
* We avoid dependency injection here to avoid early resolution.
*
* @var \Flarum\Api\Resource\AbstractResource|\Flarum\Api\Resource\AbstractDatabaseResource $resource
* @var \Flarum\Api\Resource\AbstractResource $resource
*/
$resource = (new ReflectionClass($resourceClass))->newInstanceWithoutConstructor();

Expand All @@ -199,7 +198,7 @@ protected function populateRoutes(RouteCollection $routes, Container $container)
* None of the injected dependencies should be directly used within
* the `endpoints` method. Encourage using callbacks.
*
* @var array<Endpoint&EndpointInterface> $endpoints
* @var array<Endpoint> $endpoints
*/
$endpoints = $resource->resolveEndpoints(true);

Expand Down
79 changes: 79 additions & 0 deletions framework/core/src/Api/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,18 @@
use Flarum\Http\RequestUtil;
use Flarum\Search\SearchResults;
use Flarum\User\User;
use Psr\Http\Message\ServerRequestInterface;
use Tobyz\JsonApiServer\Context as BaseContext;
use Tobyz\JsonApiServer\Resource\Resource;
use Tobyz\JsonApiServer\Schema\Field\Field;
use WeakMap;

class Context extends BaseContext
{
private WeakMap $fields;

public int|string|null $modelId = null;
public ?array $requestIncludes = null;
protected ?SearchResults $search = null;

/**
Expand All @@ -29,6 +37,34 @@ class Context extends BaseContext
*/
protected array $parameters = [];

public function __construct(\Tobyz\JsonApiServer\JsonApi $api, ServerRequestInterface $request)
{
$this->fields = new WeakMap();

parent::__construct($api, $request);
}

/**
* Get the fields for the given resource, keyed by name.
*
* @return array<string, Field>
*/
public function fields(Resource $resource): array
{
if (isset($this->fields[$resource])) {
return $this->fields[$resource];
}

$fields = [];

// @phpstan-ignore-next-line
foreach ($resource->resolveFields() as $field) {
$fields[$field->name] = $field;
}

return $this->fields[$resource] = $fields;
}

public function withSearchResults(SearchResults $search): static
{
$new = clone $this;
Expand Down Expand Up @@ -96,4 +132,47 @@ public function listing(string|null $resource = null): bool
{
return $this->endpoint instanceof Endpoint\Index && (! $resource || is_a($this->collection, $resource));
}

public function withRequest(ServerRequestInterface $request): static
{
$new = parent::withRequest($request);
$new->requestIncludes = null;

return $new;
}

public function withModelId(int|string|null $id): static
{
$new = clone $this;
$new->modelId = $id;

return $new;
}

public function withRequestIncludes(array $requestIncludes): static
{
$new = clone $this;
$new->requestIncludes = $requestIncludes;

return $new;
}

public function extractIdFromPath(BaseContext $context): ?string
{
/** @var Endpoint\Endpoint $endpoint */
$endpoint = $context->endpoint;

$currentPath = trim($context->path(), '/');
$path = trim($context->collection->name().$endpoint->path, '/');

if (! str_contains($path, '{id}')) {
return null;
}

$segments = explode('/', $path);
$idSegmentIndex = array_search('{id}', $segments);
$currentPathSegments = explode('/', $currentPath);

return $currentPathSegments[$idSegmentIndex] ?? null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
namespace Flarum\Api\Endpoint\Concerns;

use Closure;
use Flarum\Api\Resource\AbstractResource;
use Flarum\Http\RequestUtil;
use Tobyz\JsonApiServer\Context;
use Tobyz\JsonApiServer\Resource\AbstractResource;
use Tobyz\JsonApiServer\Schema\Sort;

trait ExtractsListingParams
Expand Down Expand Up @@ -110,11 +110,13 @@ public function defaultExtracts(Context $context): array

public function getAvailableSorts(Context $context): array
{
if (! $context->collection instanceof AbstractResource) {
$collection = $context->collection;

if (! $collection instanceof AbstractResource) {
return [];
}

$asc = collect($context->collection->resolveSorts())
$asc = collect($collection->resolveSorts())
->filter(fn (Sort $field) => $field->isVisible($context))
->pluck('name')
->toArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@
use Flarum\User\Exception\NotAuthenticatedException;
use Flarum\User\Exception\PermissionDeniedException;
use Tobyz\JsonApiServer\Context;
use Tobyz\JsonApiServer\Schema\Concerns\HasVisibility;

trait HasAuthorization
{
use HasVisibility {
isVisible as parentIsVisible;
}

protected bool|Closure $authenticated = false;

protected null|string|Closure $ability = null;
Expand Down Expand Up @@ -86,6 +91,6 @@ public function isVisible(Context $context): bool
$actor->assertCan($ability, $context->model);
}

return parent::isVisible($context);
return $this->parentIsVisible($context);
}
}
4 changes: 4 additions & 0 deletions framework/core/src/Api/Endpoint/Concerns/HasCustomHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

trait HasCustomHooks
{
use HasHooks {
resolveCallable as protected resolveHookCallable;
}

protected function resolveCallable(callable|string $callable, Context $context): callable
{
if (is_string($callable)) {
Expand Down
Loading
Loading