Skip to content

Commit 94ae5b1

Browse files
authored
Improved Scopes Mapping (#71)
* moved scope parsing to the resources trait where it belongs * Update Controller.php * Update Parser.php * Update laravel-api-controller.php * Apply fixes from StyleCI (#72) Co-authored-by: Craig Smith <[email protected]> Co-authored-by: Craig Smith <[email protected]>
1 parent e558e4a commit 94ae5b1

File tree

4 files changed

+28
-20
lines changed

4 files changed

+28
-20
lines changed

config/laravel-api-controller.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
return [
44
// Relative path from the app directory to api controllers directory.
5-
'controllers_dir' => 'Http/Controllers/Api',
5+
'controllers_dir' => env('PHPSA_API_CONTROLLER_DIR', 'Http/Controllers/Api'),
66
// Relative path from the app directory to the api routes file.
77
'routes_file' => '../routes/api.php',
88
// Relative path from the app directory to the models directory. Typically it's either 'Models' or ''.
99
'models_base_dir' => 'Models',
1010
// Relative path from the base directory to the api controller stub.
11-
'controller_stub' => 'vendor/phpsa/laravel-api-controller/src/Generator/stubs/controller.stub',
11+
'controller_stub' => env('PHPSA_API_CONTROLLER_STUB', 'vendor/phpsa/laravel-api-controller/src/Generator/stubs/controller.stub'),
1212
// Relative path from the base directory to the route stub.
13-
'route_stub' => 'vendor/phpsa/laravel-api-controller/src/Generator/stubs/route.stub',
13+
'route_stub' => env('PHPSA_API_ROUTE_STUB', 'vendor/phpsa/laravel-api-controller/src/Generator/stubs/route.stub'),
1414

1515
'parameters' => [
1616
'include' => 'include', // which hasOnes / HasMany etc to include in the response

src/Contracts/Parser.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -193,21 +193,6 @@ protected function parseFilterParams(): void
193193
}
194194
}
195195

196-
/**
197-
* parses out custom method filters etc.
198-
*
199-
* @param mixed $request
200-
*/
201-
protected function parseMethodParams($request): void
202-
{
203-
foreach ($this->/** @scrutinizer ignore-call */ getAllowedScopes() as $scope) {
204-
if ($request->has(Helpers::snake($scope)) || $request->has(Helpers::camel($scope))) {
205-
$value = $request->has(Helpers::snake($scope)) ? $request->get(Helpers::snake($scope)) : $request->get(Helpers::camel($scope));
206-
call_user_func([$this->repository, Helpers::camel($scope)], $value);
207-
}
208-
}
209-
}
210-
211196
protected function setWhereHasClause(array $where): void
212197
{
213198
[$with, $key] = explode('.', $where['key']);

src/Http/Api/Contracts/HasResources.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Phpsa\LaravelApiController\Http\Api\Contracts;
44

5+
use Phpsa\LaravelApiController\Helpers;
56
use Phpsa\LaravelApiController\Http\Resources\ApiCollection;
67
use Phpsa\LaravelApiController\Http\Resources\ApiResource;
78

@@ -62,6 +63,28 @@ protected function getAllowedScopes(): array
6263
{
6364
$resource = $this->getResourceSingle();
6465

65-
return (method_exists($resource, 'getAllowedScopes')) ? ($resource)::getAllowedScopes() : [];
66+
$scopes = collect((method_exists($resource, 'getAllowedScopes')) ? ($resource)::getAllowedScopes() : []);
67+
68+
return $scopes->map(function ($scope) {
69+
return strpos($scope, 'scope') === 0 ? substr($scope, 5) : $scope;
70+
})->toArray();
71+
}
72+
73+
/**
74+
* parses out custom method filters etc.
75+
*
76+
* @param mixed $request
77+
*/
78+
protected function parseAllowedScopes($request): void
79+
{
80+
foreach ($this->getAllowedScopes() as $scope) {
81+
$snake = Helpers::snake($scope);
82+
$camel = Helpers::camel($scope);
83+
84+
if ($request->has($snake) || $request->has($camel)) {
85+
$value = $request->has($snake) ? $request->get($snake) : $request->get($camel);
86+
call_user_func([$this->repository, $camel], $value);
87+
}
88+
}
6689
}
6790
}

src/Http/Api/Controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ protected function handleIndexActionCommon($request, array $extraParams = [])
110110
$this->parseIncludeParams();
111111
$this->parseSortParams();
112112
$this->parseFilterParams();
113-
$this->parseMethodParams($request);
113+
$this->parseAllowedScopes($request);
114114
$this->qualifyCollectionQuery();
115115
}
116116

0 commit comments

Comments
 (0)