-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7a843fa
commit 8dbfd65
Showing
3 changed files
with
126 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
namespace DoubleThreeDigital\Runway\Http\Controllers; | ||
|
||
use DoubleThreeDigital\Runway\Http\Resources\ApiResource; | ||
use DoubleThreeDigital\Runway\Resource; | ||
use DoubleThreeDigital\Runway\Runway; | ||
use Facades\Statamic\API\FilterAuthorizer; | ||
use Illuminate\Support\Str; | ||
use Statamic\Exceptions\NotFoundHttpException; | ||
use Statamic\Http\Controllers\API\ApiController; | ||
|
||
class RestApiController extends ApiController | ||
{ | ||
private $config; | ||
protected $resourceConfigKey = 'runway'; | ||
protected $routeResourceKey = 'resource'; | ||
protected $resourceHandle; | ||
|
||
public function index($resource) | ||
{ | ||
$this->abortIfDisabled(); | ||
|
||
$this->resourceHandle = $resource; | ||
|
||
$resource = Runway::findResource($resource); | ||
|
||
if (! $resource) { | ||
throw new NotFoundHttpException; | ||
} | ||
|
||
$results = $this->filterSortAndPaginate($resource->model()->query()); | ||
$results->setCollection($results->getCollection()->map(fn ($model) => $this->makeResourceFromModel($resource, $model))); | ||
|
||
return ApiResource::collection($results); | ||
} | ||
|
||
public function show($resource, $id) | ||
{ | ||
$this->abortIfDisabled(); | ||
|
||
$this->resourceHandle = $resource; | ||
|
||
$resource = Runway::findResource($resource); | ||
|
||
if (! $resource) { | ||
throw new NotFoundHttpException; | ||
} | ||
|
||
if (! $model = $resource->model()->find($id)) { | ||
throw new NotFoundHttpException; | ||
} | ||
|
||
return ApiResource::make($this->makeResourceFromModel($resource, $model)); | ||
} | ||
|
||
protected function allowedFilters() | ||
{ | ||
return FilterAuthorizer::allowedForSubResources('api', $this->resourceConfigKey, $this->resourceHandle); | ||
} | ||
|
||
private function makeResourceFromModel($resource, $model) | ||
{ | ||
if (! $this->config) { | ||
$this->config = collect(config('runway.resources'))->get(get_class($model)); | ||
} | ||
|
||
return new Resource( | ||
handle: $this->resourceHandle, | ||
model: $model, | ||
name: $this->config['name'] ?? Str::title($this->resourceHandle), | ||
blueprint: $resource->blueprint(), | ||
config: $this->config ?? [], | ||
); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace DoubleThreeDigital\Runway\Http\Resources; | ||
|
||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
class ApiResource extends JsonResource | ||
{ | ||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @param \Illuminate\Http\Request | ||
* @return array | ||
*/ | ||
public function toArray($request) | ||
{ | ||
return $this->resource | ||
->toAugmentedCollection() | ||
->withShallowNesting() | ||
->toArray(); | ||
} | ||
} |
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