diff --git a/.gitignore b/.gitignore index 5555fda..b2a97eb 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ _ide_helper.php .phpstorm.meta.php coverage coverage.xml -composer.lock \ No newline at end of file +composer.lock +\.phpunit\.result\.cache diff --git a/config/api-core.php b/config/api-core.php new file mode 100644 index 0000000..7a3cdb1 --- /dev/null +++ b/config/api-core.php @@ -0,0 +1,12 @@ + env('API_CORE_KEY', 'api_key'), +]; diff --git a/src/Auth/ApiGuard.php b/src/Auth/ApiGuard.php index 18b90ab..5666659 100644 --- a/src/Auth/ApiGuard.php +++ b/src/Auth/ApiGuard.php @@ -13,13 +13,14 @@ class ApiGuard extends TokenGuard { /** * @param UserProvider $provider - * @param Request $request + * @param Request $request + * @param $storageKey */ - public function __construct(UserProvider $provider, Request $request) + public function __construct(UserProvider $provider, Request $request, $storageKey) { parent::__construct($provider, $request); - $this->storageKey = 'api_key'; + $this->storageKey = $storageKey; } /** diff --git a/src/Auth/AuthServiceProvider.php b/src/Auth/AuthServiceProvider.php index e3c1c7c..d41b9af 100644 --- a/src/Auth/AuthServiceProvider.php +++ b/src/Auth/AuthServiceProvider.php @@ -16,7 +16,14 @@ class AuthServiceProvider extends ServiceProvider public function register() { Auth::extend('api', function ($app, $name, array $config) { - return new ApiGuard(Auth::createUserProvider($config['provider']), $app['request']); + return new ApiGuard(Auth::createUserProvider($config['provider']), $app['request'], $app['config']->get('api-core.api-key')); }); } + + public function boot() + { + $this->publishes([ + __DIR__ . '/../config/api-core.php' => config_path('api-core.php'), + ]); + } } diff --git a/src/Requests/ApiRequest.php b/src/Requests/ApiRequest.php index 41e8b1c..8df92ae 100644 --- a/src/Requests/ApiRequest.php +++ b/src/Requests/ApiRequest.php @@ -2,13 +2,14 @@ namespace Napp\Core\Api\Requests; -use Illuminate\Foundation\Http\FormRequest; use Illuminate\Contracts\Validation\Validator; +use Illuminate\Foundation\Http\FormRequest; +use Illuminate\Support\Str; +use Napp\Core\Api\Exceptions\Exceptions\ApiInternalCallValidationException; +use Napp\Core\Api\Exceptions\Exceptions\InvalidFieldException; +use Napp\Core\Api\Exceptions\Exceptions\ValidationException; use Napp\Core\Api\Transformers\ApiTransformer; use Napp\Core\Api\Transformers\TransformerInterface; -use Napp\Core\Api\Exceptions\Exceptions\ValidationException; -use Napp\Core\Api\Exceptions\Exceptions\InvalidFieldException; -use Napp\Core\Api\Exceptions\Exceptions\ApiInternalCallValidationException; /** * Class ApiRequest. @@ -57,7 +58,7 @@ protected function validateInputFields(): void $rules = $this->rules(); if (false === empty(array_diff_key($input, $rules))) { $exception = new InvalidFieldException(); - $exception->statusMessage = $exception->statusMessage . ': ' . implode(',', array_keys(array_diff_key($input, $rules))); + $exception->statusMessage = $exception->statusMessage.': '.implode(',', array_keys(array_diff_key($input, $rules))); throw $exception; } @@ -72,7 +73,7 @@ protected function transformInput(): array * Remove input fields like _method, _token, etc. */ $input = array_filter($this->input(), function ($key) { - return ! starts_with($key, '_'); + return ! Str::startsWith($key, '_'); }, ARRAY_FILTER_USE_KEY); return $this->getTransformer()->transformInput($input); @@ -113,7 +114,7 @@ protected function handleApiCallFailedValidation(Validator $validator) { $message = $validator->messages()->first(); $exception = new ValidationException(); - $exception->statusMessage = $exception->statusMessage . ': ' . $message; + $exception->statusMessage = $exception->statusMessage.': '.$message; $exception->validation = $this->transformValidationOutput($validator); throw $exception; diff --git a/src/Requests/Provider/RequestServiceProvider.php b/src/Requests/Provider/RequestServiceProvider.php index 7d80d04..e5eb0a9 100644 --- a/src/Requests/Provider/RequestServiceProvider.php +++ b/src/Requests/Provider/RequestServiceProvider.php @@ -19,8 +19,4 @@ public function boot() }); } } - - public function register() - { - } } diff --git a/src/Router/Provider/RouterServiceProvider.php b/src/Router/Provider/RouterServiceProvider.php index 10143d9..08bdd95 100644 --- a/src/Router/Provider/RouterServiceProvider.php +++ b/src/Router/Provider/RouterServiceProvider.php @@ -2,7 +2,6 @@ namespace Napp\Core\Api\Router\Provider; -use Napp\Core\Api\Router\Router; use Illuminate\Support\ServiceProvider as BaseProvider; /** @@ -12,14 +11,6 @@ class RouterServiceProvider extends BaseProvider { public function register() { - } - - public function boot() - { - $this->app->singleton('internalrouter', function () { - $app = app(); - - return new Router($app, $app['request'], $app['router']); - }); + $this->app->singleton('internalrouter'); } } diff --git a/src/Transformers/ApiTransformer.php b/src/Transformers/ApiTransformer.php index 893a816..e6d5fe6 100644 --- a/src/Transformers/ApiTransformer.php +++ b/src/Transformers/ApiTransformer.php @@ -2,12 +2,13 @@ namespace Napp\Core\Api\Transformers; -use Illuminate\Support\Str; -use Illuminate\Support\Collection; -use Illuminate\Pagination\Paginator; -use Illuminate\Database\Eloquent\Model; use Illuminate\Contracts\Support\Arrayable; +use Illuminate\Database\Eloquent\Model; +use Illuminate\Pagination\AbstractPaginator; use Illuminate\Pagination\LengthAwarePaginator; +use Illuminate\Pagination\Paginator; +use Illuminate\Support\Collection; +use Illuminate\Support\Str; /** * Class ApiTransformer. @@ -69,7 +70,7 @@ public function transformOutput($data): array { $output = []; - if (true === $data instanceof LengthAwarePaginator || true === $data instanceof Paginator) { + if (true === ($data instanceof AbstractPaginator)) { return $this->transformPaginatedOutput($data); } @@ -110,7 +111,7 @@ public function transformOutputKeys(array $data): array protected function transformAttributes(array $output, array $data): array { foreach ($data as $key => $value) { - if (true === $this->strict && ! $this->isMapped($key)) { + if (true === $this->strict && !$this->isMapped($key)) { continue; } @@ -131,7 +132,7 @@ protected function transformRelationships(array $output, Model $data): array /** @var Model $data */ $relationships = $data->getRelations(); foreach ($relationships as $relationshipName => $relationship) { - if (true === $this->strict && ! $this->isMapped($relationshipName)) { + if (true === $this->strict && !$this->isMapped($relationshipName)) { continue; } @@ -249,7 +250,7 @@ protected function convertValueType(string $key, $value) ? $this->apiMapping[$key]['dataType'] : 'unknown'; - foreach (static::normalizeType($type) as [$method, $parameters]) { + foreach (static::normalizeType($type) as list($method, $parameters)) { if (true === empty($method)) { return $value; } @@ -285,7 +286,7 @@ protected static function parseStringDataType($type): array // easy {data-type}:{parameters} formatting convention. For instance the // data-type "float:3" states that the value will be converted to a float with 3 decimals. if (mb_strpos($type, ':') !== false) { - [$dataType, $parameter] = explode(':', $type, 2); + list($dataType, $parameter) = explode(':', $type, 2); $parameters = static::parseParameters($parameter); }