|
22 | 22 | class ApiResource implements ExtenderInterface
|
23 | 23 | {
|
24 | 24 | private array $endpoints = [];
|
| 25 | + private array $endpointsBefore = []; |
| 26 | + private array $endpointsAfter = []; |
25 | 27 | private array $removeEndpoints = [];
|
26 | 28 | private array $endpoint = [];
|
27 | 29 | private array $fields = [];
|
@@ -55,6 +57,45 @@ public function endpoints(callable|string $endpoints): self
|
55 | 57 | return $this;
|
56 | 58 | }
|
57 | 59 |
|
| 60 | + /** |
| 61 | + * Add endpoints to the resource before a certain endpoint. |
| 62 | + * |
| 63 | + * @param string $before the name of the endpoint to add the new endpoints before. |
| 64 | + * @param callable|class-string $endpoints must be a callable that returns an array of objects that implement \Flarum\Api\Endpoint\Endpoint. |
| 65 | + */ |
| 66 | + public function endpointsBefore(string $before, callable|string $endpoints): self |
| 67 | + { |
| 68 | + $this->endpointsBefore[] = [$before, $endpoints]; |
| 69 | + |
| 70 | + return $this; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Add endpoints to the resource after a certain endpoint. |
| 75 | + * |
| 76 | + * @param string $after the name of the endpoint to add the new endpoints after. |
| 77 | + * @param callable|class-string $endpoints must be a callable that returns an array of objects that implement \Flarum\Api\Endpoint\Endpoint. |
| 78 | + */ |
| 79 | + public function endpointsAfter(string $after, callable|string $endpoints): self |
| 80 | + { |
| 81 | + $this->endpointsAfter[] = [$after, $endpoints]; |
| 82 | + |
| 83 | + return $this; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Add endpoints to the resource before all other endpoints. |
| 88 | + * |
| 89 | + * @param string $after the name of the endpoint to add the new endpoints after. |
| 90 | + * @param callable|class-string $endpoints must be a callable that returns an array of objects that implement \Flarum\Api\Endpoint\Endpoint. |
| 91 | + */ |
| 92 | + public function endpointsBeforeAll(callable|string $endpoints): self |
| 93 | + { |
| 94 | + $this->endpointsBefore[] = [0, $endpoints]; |
| 95 | + |
| 96 | + return $this; |
| 97 | + } |
| 98 | + |
58 | 99 | /**
|
59 | 100 | * Remove endpoints from the resource.
|
60 | 101 | *
|
@@ -214,6 +255,31 @@ function (array $endpoints, Resource $resource) use ($container): array {
|
214 | 255 | $endpoints = array_merge($endpoints, $newEndpointsCallback());
|
215 | 256 | }
|
216 | 257 |
|
| 258 | + foreach ($this->endpointsBefore as [$before, $newEndpointsCallback]) { |
| 259 | + $newEndpointsCallback = ContainerUtil::wrapCallback($newEndpointsCallback, $container); |
| 260 | + |
| 261 | + if ($before === 0) { |
| 262 | + array_unshift($endpoints, ...$newEndpointsCallback()); |
| 263 | + } else { |
| 264 | + $newEndpoints = $newEndpointsCallback(); |
| 265 | + $beforeIndex = array_search($before, array_column($endpoints, 'name')); |
| 266 | + |
| 267 | + if ($beforeIndex !== false) { |
| 268 | + array_splice($endpoints, $beforeIndex, 0, $newEndpoints); |
| 269 | + } |
| 270 | + } |
| 271 | + } |
| 272 | + |
| 273 | + foreach ($this->endpointsAfter as [$after, $newEndpointsCallback]) { |
| 274 | + $newEndpointsCallback = ContainerUtil::wrapCallback($newEndpointsCallback, $container); |
| 275 | + $newEndpoints = $newEndpointsCallback(); |
| 276 | + $afterIndex = array_search($after, array_column($endpoints, 'name')); |
| 277 | + |
| 278 | + if ($afterIndex !== false) { |
| 279 | + array_splice($endpoints, $afterIndex + 1, 0, $newEndpoints); |
| 280 | + } |
| 281 | + } |
| 282 | + |
217 | 283 | foreach ($this->removeEndpoints as $removeEndpointClass) {
|
218 | 284 | [$endpointsToRemove, $condition] = $removeEndpointClass;
|
219 | 285 |
|
|
0 commit comments