-
-
Notifications
You must be signed in to change notification settings - Fork 835
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow adding endpoints before/after others
- Loading branch information
Showing
3 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,8 @@ | |
class ApiResource implements ExtenderInterface | ||
{ | ||
private array $endpoints = []; | ||
private array $endpointsBefore = []; | ||
private array $endpointsAfter = []; | ||
private array $removeEndpoints = []; | ||
private array $endpoint = []; | ||
private array $fields = []; | ||
|
@@ -55,6 +57,45 @@ public function endpoints(callable|string $endpoints): self | |
return $this; | ||
} | ||
|
||
/** | ||
* Add endpoints to the resource before a certain endpoint. | ||
* | ||
* @param string $before the name of the endpoint to add the new endpoints before. | ||
* @param callable|class-string $endpoints must be a callable that returns an array of objects that implement \Flarum\Api\Endpoint\Endpoint. | ||
*/ | ||
public function endpointsBefore(string $before, callable|string $endpoints): self | ||
{ | ||
$this->endpointsBefore[] = [$before, $endpoints]; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Add endpoints to the resource after a certain endpoint. | ||
* | ||
* @param string $after the name of the endpoint to add the new endpoints after. | ||
* @param callable|class-string $endpoints must be a callable that returns an array of objects that implement \Flarum\Api\Endpoint\Endpoint. | ||
*/ | ||
public function endpointsAfter(string $after, callable|string $endpoints): self | ||
{ | ||
$this->endpointsAfter[] = [$after, $endpoints]; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Add endpoints to the resource before all other endpoints. | ||
* | ||
* @param string $after the name of the endpoint to add the new endpoints after. | ||
* @param callable|class-string $endpoints must be a callable that returns an array of objects that implement \Flarum\Api\Endpoint\Endpoint. | ||
*/ | ||
public function endpointsBeforeAll(callable|string $endpoints): self | ||
Check failure on line 92 in framework/core/src/Extend/ApiResource.php GitHub Actions / run / PHPStan PHP 8.2
Check failure on line 92 in framework/core/src/Extend/ApiResource.php GitHub Actions / run / PHPStan PHP 8.4
|
||
{ | ||
$this->endpointsBefore[] = [0, $endpoints]; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Remove endpoints from the resource. | ||
* | ||
|
@@ -214,6 +255,31 @@ function (array $endpoints, Resource $resource) use ($container): array { | |
$endpoints = array_merge($endpoints, $newEndpointsCallback()); | ||
} | ||
|
||
foreach ($this->endpointsBefore as [$before, $newEndpointsCallback]) { | ||
$newEndpointsCallback = ContainerUtil::wrapCallback($newEndpointsCallback, $container); | ||
|
||
if ($before === 0) { | ||
array_unshift($endpoints, ...$newEndpointsCallback()); | ||
} else { | ||
$newEndpoints = $newEndpointsCallback(); | ||
$beforeIndex = array_search($before, array_column($endpoints, 'name')); | ||
|
||
if ($beforeIndex !== false) { | ||
array_splice($endpoints, $beforeIndex, 0, $newEndpoints); | ||
} | ||
} | ||
} | ||
|
||
foreach ($this->endpointsAfter as [$after, $newEndpointsCallback]) { | ||
$newEndpointsCallback = ContainerUtil::wrapCallback($newEndpointsCallback, $container); | ||
$newEndpoints = $newEndpointsCallback(); | ||
$afterIndex = array_search($after, array_column($endpoints, 'name')); | ||
|
||
if ($afterIndex !== false) { | ||
array_splice($endpoints, $afterIndex + 1, 0, $newEndpoints); | ||
} | ||
} | ||
|
||
foreach ($this->removeEndpoints as $removeEndpointClass) { | ||
[$endpointsToRemove, $condition] = $removeEndpointClass; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters